gsutil produces stdout output in it's custom key-value format which is quite inconvenient to parse. Is it possible to make gsutil generate JSON instead e.g. for this command:
gsutil ls -L gs://bucket-name/relative/path
gsutil produces stdout output in it's custom key-value format which is quite inconvenient to parse. Is it possible to make gsutil generate JSON instead e.g. for this command:
gsutil ls -L gs://bucket-name/relative/path
It appears as though replacing tabs with spaces is sufficient for pyyaml library to parse the output:
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))
The output will be in something that is closer to be yaml format. as mentioned in the code on the official GitHub repository and as part of gsutil the output format cannot be modified.
the output will be something like:
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"
}
]
To get this as JSON i found this go repository that can work for encode the output as a JSON
git clone https://github.com/fedir/json_encode.git
cd json_encode
go build
gsutil ls -L gs://bucket-name | ./json_encode