【问题标题】:GCP - get full information about bucketGCP - 获取有关存储桶的完整信息
【发布时间】:2023-03-18 13:12:01
【问题描述】:

我需要获取存储在 Google Bucket 中的文件信息。 文件大小、存储类、上次修改时间、类型等信息。我搜索了谷歌文档,但可以通过 curl 或控制台方法完成。我需要从 Python API 获取该信息,例如下载 blob、将 blob 上传到存储桶。示例代码或任何帮助表示赞赏!

【问题讨论】:

    标签: python-3.x google-cloud-platform google-cloud-storage


    【解决方案1】:

    到get the object metadata可以使用以下代码:

    from google.cloud import storage
    
    def object_metadata(bucket_name, blob_name):
        """Prints out a blob's metadata."""
        storage_client = storage.Client()
        bucket = storage_client.get_bucket(bucket_name)
        blob = bucket.get_blob(blob_name)
    
        print('Blob: {}'.format(blob.name))
        print('Bucket: {}'.format(blob.bucket.name))
        print('Storage class: {}'.format(blob.storage_class))
        print('ID: {}'.format(blob.id))
        print('Size: {} bytes'.format(blob.size))
        print('Updated: {}'.format(blob.updated))
        print('Generation: {}'.format(blob.generation))
        print('Metageneration: {}'.format(blob.metageneration))
        print('Etag: {}'.format(blob.etag))
        print('Owner: {}'.format(blob.owner))
        print('Component count: {}'.format(blob.component_count))
        print('Crc32c: {}'.format(blob.crc32c))
        print('md5_hash: {}'.format(blob.md5_hash))
        print('Cache-control: {}'.format(blob.cache_control))
        print('Content-type: {}'.format(blob.content_type))
        print('Content-disposition: {}'.format(blob.content_disposition))
        print('Content-encoding: {}'.format(blob.content_encoding))
        print('Content-language: {}'.format(blob.content_language))
        print('Metadata: {}'.format(blob.metadata))
    
    object_metadata('bucketName', 'objectName')
    

    【讨论】:

      【解决方案2】:

      使用Cloud Storage client library,并在the docs 上检查存储桶,您可以这样做来获取存储类:

      from google.cloud import storage
      client = storage.Client()
      bucket = client.get_bucket('YOUR_BUCKET')
      print(bucket.storage_class)
      

      至于文件的大小和最后修改的文件(至少这是我从你的问题中理解的),这些属于文件本身。您可以迭代存储桶中的 blob 列表并检查:

      for blob in bucket.list_blobs():
          print(blob.size)
          print(blob.updated)
      

      【讨论】:

        猜你喜欢
        • 2017-08-13
        • 2021-05-17
        • 1970-01-01
        • 2020-07-15
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多