0
votes

I want to create the function in python the that helps to enumerate the meta-data using the Google Cloud Storage Client Library Functions

I already created the function that list the bucket I just wanna to display the metadata of the buckets as shown here https://cloud.google.com/storage/docs/json_api/v1/buckets#resource


     for bucket in self.client.list_buckets():
         print(bucket)
#  this list the all the buckets 

I want something like this

  def meta(self):
      bucket_name="sample"
    //It should print the metadata of the bucket not the metadata of object inside the bucket 
2

2 Answers

4
votes

There's a "protected" member of google.cloud.storage.Client that is a map of bucket properties, closely matching the API representation document you're looking for. It's subject to change since it is not in the exposed API, but you can get a clear idea what is available right now. Here's a snippet:

#! /usr/bin/env python

from pprint import pprint
from typing import Dict

from google.cloud import storage

BUCKET_NAME = "your-bucket-here"

def get_bucket_metadata(bucket_name: str, gcs: storage.Client) -> Dict[str, str]:
    bucket = gcs.get_bucket(bucket_name)
    return bucket._properties

def main():
    gcs = storage.Client()
    metadata = get_bucket_metadata(BUCKET_NAME, gcs)
    pprint(metadata)

if __name__ == "__main__":
    main()

I found this by running print(dir(bucket)) and examining the available methods and properties. You may find others that interest you that way.

Here's an example of the output:

{'etag': 'CAE=',
 'iamConfiguration': {'bucketPolicyOnly': {'enabled': False}},
 'id': 'your-bucket-here',
 'kind': 'storage#bucket',
 'location': 'US-WEST1',
 'metageneration': '1',
 'name': 'your-bucket-here',
 'projectNumber': '264682420247',
 'selfLink': 'https://www.googleapis.com/storage/v1/b/your-bucket-here',
 'storageClass': 'REGIONAL',
 'timeCreated': '2019-02-20T21:53:30.130Z',
 'updated': '2019-02-20T21:53:30.130Z'}

HTH.

0
votes

You can find a list of methods that can be called on the class google.cloud.storage.bucket.Bucket here.

For example:

from google.cloud import storage

storage_client = storage.Client()
bucket_name = 'my-bucket'
bucket = storage_client.get_bucket(bucket_name)

print(bucket.name)
print(bucket.time_created)
print(bucket.project_number)
print(bucket.cors)

...