【问题标题】:How to get the metdata of the Google Cloud Storage Bucket using Google Cloud Storage Client如何使用 Google Cloud Storage Client 获取 Google Cloud Storage Bucket 的元数据
【发布时间】:2019-06-19 01:39:18
【问题描述】:

我想在python 中创建有助于使用 Google Cloud Storage 客户端库函数枚举元数据的函数

我已经创建了列出存储桶的函数我只想显示存储桶的元数据,如下所示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 

我想要这样的东西

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

【问题讨论】:

    标签: google-cloud-platform google-cloud-storage


    【解决方案1】:

    google.cloud.storage.Client 的“受保护”成员是存储桶属性的映射,与您正在寻找的 API 表示文档非常匹配。它可能会发生变化,因为它不在公开的 API 中,但您可以清楚地了解现在可用的内容。这是一个sn-p:

    #! /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()
    

    我通过运行print(dir(bucket)) 并检查可用的方法和属性发现了这一点。您可能会以这种方式找到其他对您感兴趣的人。

    这是一个输出示例:

    {'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。

    【讨论】:

      【解决方案2】:

      您可以找到可以在类google.cloud.storage.bucket.Buckethere 上调用的方法列表。

      例如:

      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)
      
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-13
        • 2017-11-08
        • 2020-06-13
        • 2020-03-26
        • 2019-12-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多