gsutil命令行工具支持JSON输出吗?

4

gsutil以其自定义的键值格式产生stdout输出,很难解析。是否可以使gsutil生成JSON,例如对于此命令:

可以将gsutil生成的输出格式改为JSON吗?例如对于这个命令:
gsutil ls -L gs://bucket-name/relative/path

输出格式为yaml,它的解析难易程度与其他格式相当。 - Soni Sol
@JoséSoní,您能提供一个说明输出结果为yaml的链接吗?我在文档中找不到它。此外,许多语言中都有JSON解析器作为内置库,而对于YAML,则需要添加第三方依赖项。 - mechatroner
顺便说一下,我在 gsutil 存储库中打开了一个功能请求。链接 - mechatroner
2个回答

1
输出结果将更接近yaml格式。正如官方GitHub存储库中的代码所述,作为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",
                  "projectTeam": {
                    "projectNumber": "867489160491",
                    "team": "owners"
                  },
                  "role": "OWNER"
                }
              ]

为了将其作为JSON获取,我找到了转到存储库,可以用于将输出编码为JSON。
git clone https://github.com/fedir/json_encode.git
cd json_encode
go build

gsutil ls -L gs://bucket-name  | ./json_encode


1

似乎将制表符替换为空格就足以使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))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接