Huawei Cloud OBS Statistics
Overview
Produce a one-shot read-only aggregated statistics report for Huawei Cloud OBS (Object Storage Service). Given a scope — the whole account, a single bucket, and/or a key prefix — the skill enumerates buckets and objects and aggregates the results locally, reporting:
- bucket count
- object count
- total capacity — the sum of object
Size(bytes), an estimate, not a billing figure - distribution by storage class (Standard / Warm / Cold / Deep Archive …)
- distribution by region
Primary execution path is the KooCLI obsutil-backed OBS module
(hcloud OBS ls — the read-only ListBuckets / ListObjects operations); when the
hcloud binary is absent the script falls back to the official
huaweicloudsdkobs v1 SDK (ObsClient.list_buckets / list_objects).
Architecture:
agent → python3 scripts/collect_obs_stats.py
→ hcloud OBS ls [-limit=N] [-e=obs.<region>.myhuaweicloud.com] (buckets)
→ hcloud OBS ls obs://<bucket>/[<prefix>/] [-limit=N] [-e=...] (objects)
→ local aggregation → table / json / md
(hcloud binary missing) → fallback: huaweicloudsdkobs v1
ObsClient.list_buckets / list_objects
Capability boundary: this skill aggregates objects/buckets into summary metrics. It does not export per-object inventories and does not count directories/folders. Capacity is estimated from object
Size, not a billing statement.
Out of scope: every mutating OBS action (bucket create/delete, object upload/download/copy/delete, ACL/policy/encryption/lifecycle changes) — these are blocked by the write-intent guard; a write-capable skill with explicit user confirmation would be required.
Prerequisites
- KooCLI (
hcloud) installed with the obsutil component — see references/cli-installation-guide.md. Alternative:pip install huaweicloudsdkcore huaweicloudsdkobsto enable the SDK fallback path (or runscripts/ensure_env.py). - obsutil credentials configured out-of-band — the
hcloud OBSmodule reads AK/SK/endpoint from the obsutil config file (~/.obsutilconfig), separate from the KooCLI account profile. Never paste AK/SK into chat; the script never reads, echoes, or stores secrets itself. - IAM permission — read-only
obs:bucket:ListBucket,obs:object:List(or theOBS ReadOnlyAccesssystem policy); least-privilege policy in references/iam-policies.md. - Python 3.8+ (standard library only — argparse / json / subprocess / re).
- Outbound HTTPS access to
obs.<region>.myhuaweicloud.com.
Workflow
- Confirm the scope — a single
--bucket(recommended for large accounts) or the whole account; optionally a--prefixto limit the object scan. - Confirm hcloud/obsutil readiness (optional, read-only):
hcloud OBS ls -limit=1. - Run the script with the desired output mode:
python3 scripts/collect_obs_stats.py --bucket <bucket> --prefix <prefix>. - Read the result — table for humans,
--jsonfor downstream tooling,--output mdfor a Markdown report. - Handle errors per the Error Handling table below.
Core Commands
# Whole account: aggregate every bucket (table output)
python3 scripts/collect_obs_stats.py --region cn-north-4
# Single bucket, narrowed by prefix
python3 scripts/collect_obs_stats.py --bucket my-bucket --prefix logs/
# Structured JSON (for downstream automation)
python3 scripts/collect_obs_stats.py --bucket my-bucket --json
# Markdown report
python3 scripts/collect_obs_stats.py --bucket my-bucket --output md
# Force the SDK fallback path
python3 scripts/collect_obs_stats.py --bucket my-bucket --executor sdk
Equivalent direct KooCLI commands (the script wraps exactly these calls):
hcloud OBS ls -limit=1000 -e=obs.cn-north-4.myhuaweicloud.com
hcloud OBS ls obs://my-bucket/logs/ -limit=1000 -e=obs.cn-north-4.myhuaweicloud.com
Parameter Confirmation
| Parameter | Required | Description | Example |
|---|---|---|---|
--bucket / -b |
No | Statistics scope: one bucket (default: all account buckets) | my-bucket |
--prefix / -p |
No | Limit the object scan to a key prefix | logs/ |
--region / -r |
No | Huawei Cloud region (default cn-north-4); selects the OBS endpoint |
cn-south-1 |
--output / -o |
No | Output mode: table (default) / json / md |
--output json |
--json |
No | Alias for --output json |
— |
--md |
No | Alias for --output md |
— |
--executor |
No | auto (default, CLI first) / cli / sdk |
--executor sdk |
--max-objects |
No | Per-bucket object cap (default 100000); results are marked truncated when reached | 50000 |
--endpoint |
No | Override the OBS endpoint (default obs.<region>.myhuaweicloud.com) |
obs.custom.example.com |
--operation |
No | hcloud OBS operation name; only the read-only ls is allowed |
ls |
Output-mode precedence: --md → --json → --output → table (default).
Write-Intent Guard
- The script scans all user arguments plus the
--operationvalue for mutating vocabulary (create,delete,upload,download,copy,remove,move,restore,abort,sync,modify,update,overwrite, and OBS operation names such asCreateBucket,DeleteBucket,PutObject). - Any hit raises
GuardError, prints[错误] 检测到写操作意图 ... 已被守卫拦截, and exits with code 1 before a single hcloud command is executed. - Only
hcloud OBS ls(read-only ListBuckets / ListObjects) — or the SDKlist_buckets/list_objectscalls in the fallback path — is ever constructed; there is no code path that can issue a mutating OBS API call.
KooCLI Command Format Standard
hcloud <Service> <Operation> [--key=value ...]
- Service name — exact KooCLI title-case service name:
OBS. - Operation name — the
hcloud OBSmodule is obsutil-backed: operations are lowercase obsutil commands, not PascalCase API names. Listing account buckets islswith nocloud_url; listing objects islswith acloud_urlof the formobs://<bucket>/[<prefix>/]. There is nohcloud OBS ListBuckets/ListObjectsV2command. - Region / endpoint — the OBS module does not accept
--cli-region; the region is expressed through the endpoint option-e=obs.<region>.myhuaweicloud.com(the script derives it from--region). - Simple parameter —
-key=value, e.g.-limit=1000,-e=obs.cn-north-4.myhuaweicloud.com. - REST paths (official SDK) —
GET /(ListBuckets) andGET /?prefix=&marker=&max-keys=(ListObjects), invoked byhuaweicloudsdkobsv1; the endpoint is resolved from the requested region.
Error Handling
| Scenario | Behavior |
|---|---|
| Empty scope (no buckets / no objects) | Exit 0, zeroed summary with a friendly table |
| Missing AK/SK / obsutil config | Exit 1, 缺少 AK/SK 凭证 hint to run hcloud obs config out-of-band (never in chat) |
| Invalid / insufficient credentials (403 / InvalidAccessKeyId) | Exit 1, 无权限访问 OBS,请检查 IAM 权限 hint |
| hcloud not installed | SDK fallback; if the SDK is also unavailable, exit 1 with an install hint |
| Network / API failure | Exit 1 with the hcloud/SDK error excerpt |
KooCLI usage error ([USE_ERROR] ...) |
Exit 1, explicit hcloud 参数错误 hint pointing to hcloud OBS ls --help |
Object cap reached (--max-objects) |
Exit 0, truncated=true, note that totals are a lower bound |
| Write intent detected | Exit 1, GuardError message, no command executed |
Exit codes: 0 success (including empty scopes); 1 config, auth, guard, or
API error.
Edge Cases
| Scenario | Handling |
|---|---|
| Account has no buckets | Zeroed report, exit 0 (not an error) |
| Large objects / many objects | --prefix narrows the scan; --max-objects caps it and sets truncated=true |
obsutil ls returns no bucket region |
Region distribution is merged under the requested --region, with an explicit warning |
| Storage class absent on an object | Treated as STANDARD |
| hcloud absent, SDK installed | Transparent fallback, same output modes |
| User asks to create/delete/upload | Out of scope — blocked by the write-intent guard |
Testing
Offline unit tests (mocked hcloud subprocess and SDK; no credentials, no network):
python3 -m unittest discover -s tests -v
Coverage: write-intent guard, command construction, obsutil bucket/object
parsing, storage-class normalization, failure classification, aggregation
(bucket/object/size/storage-class/region distributions, empty scope), SDK
pagination and truncation, SDK fallback wiring, rendering (table / Markdown)
and all main() output modes.
Reference Documents
| Document | Content |
|---|---|
| references/iam-policies.md | Least-privilege read-only IAM policy |
| references/cli-installation-guide.md | KooCLI + obsutil installation and credential configuration |
| references/verification-method.md | How the skill and its tests were verified |
| references/dataflow-diagram.md | Mermaid data flow diagram |
| references/acceptance-criteria.md | Acceptance criteria checklist |
