google-cloud-platform - 如何使用谷歌云存储客户端获取谷歌云存储桶的元数据
问题描述
我想python
使用 Google Cloud Storage Client Library Functions 创建有助于枚举元数据的函数
我已经创建了列出存储桶的函数,我只想显示存储桶的元数据,如下所示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.storage.Client
是存储桶属性的映射,与您正在寻找的 API 表示文档非常匹配。它可能会发生变化,因为它不在公开的 API 中,但您可以清楚地了解现在可用的内容。这是一个片段:
#! /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。
推荐阅读
- windows - 使用 WinDBG 和 VMware 进行离线内核模式驱动程序调试 - 我缺少哪些符号文件?
- java - 我正在使用 Libgdx 构建手机游戏。有人可以告诉我如何将屏幕设置为一种尺寸吗?
- python - 从远程PowerShell运行时Python权限被拒绝
- sql - 获取SQL中计数列的总和
- python - 确保测验不会两次询问相同的问题
- php - 如何在 Laravel 中存储数组?
- dart - 在 null 上调用了 getter 'storeNumber'(接收方:null)
- arrays - 如何将两个(或更多)tableViewCells 添加在一起以形成“总数”
- java - 如何在编译时未知的索引处从局部变量加载引用?
- ajax - 为什么我在 ajax 调用中更改数据时会出错