问题描述
gsutil以其自定义键值格式生成stdout输出,这非常不便于解析。是否可以使gsutil生成JSON,例如为此命令:
module.ts
解决方法
输出将采用更接近yaml格式的格式。如official GitHub repository上的代码中所述,并且作为gsutil
的一部分,无法修改输出格式。
输出将类似于:
gs://bucket/ :
Storage class: STANDARD
Location constraint: US
Versioning enabled: False
Logging configuration: None
Website configuration: None
CORS configuration: None
Lifecycle configuration: None
Requester Pays enabled: True
Labels: None
Default KMS key: None
Time created: Thu,14 Jan 2016 19:25:17 GMT
Time updated: Thu,08 Jun 2017 21:17:59 GMT
Metageneration: 1
Bucket Policy Only enabled: False
ACL:
[
{
"entity": "project-owners-867489160491","projectTeam": {
"projectNumber": "867489160491","team": "owners"
},"role": "OWNER"
}
]
Default ACL:
[
{
"entity": "project-owners-867489160491","role": "OWNER"
}
]
要以JSON格式获取,我发现了这个go repository可以将输出编码为JSON
git clone https://github.com/fedir/json_encode.git
cd json_encode
go build
gsutil ls -L gs://bucket-name | ./json_encode
,
似乎用空格替换制表符足以使pyyaml库解析输出:
import re,subprocess,yaml
output = subprocess.check_output('gsutil ls -Lb gs://some-bucket-name'.split()).decode('utf-8')
bucket = yaml.safe_load(re.sub('\t',' ',output))