如何在谷歌云存储 (Python) 中获取 blob 的 URI

ANI*_*mes 5 google-cloud-storage google-cloud-platform

如果我有一个 Blob 对象,我如何获得 URI (gs://...)?

文档说我可以使用 self_link 属性来获取 URI,但它返回 https URL(https://googleapis.com..)我正在使用 python 客户端库进行云存储。

谢谢

Chr*_*s32 7

由于您没有与我们分享您究竟是如何实现这一目标的,所以我用Python编写了一个快速脚本来获取此信息

blob 中没有像gs://Python 中那样获取 URI 的特定方法,但您可以尝试使用以下命令编写脚本:path_helper

def get_blob_URI():
    """Prints out a bucket's labels."""
    # bucket_name = 'your-bucket-name'
    storage_client = storage.Client()
    bucket_name = 'YOUR_BUCKET'
    blob_name = 'YOUR_OBJECT_NAME'
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(blob_name)
    link = blob.path_helper(bucket_name, blob_name)
    pprint.pprint('gs://' + link)
Run Code Online (Sandbox Code Playgroud)

如果您想使用该gsutil工具,您还可以gs://使用以下命令获取存储桶的所有 Urisgsutil ls gs://bucket

  • 此方法在存储桶名称和 blob 名称之间添加一个奇怪的“/o/”。它还用 % 替换 '/'。 (3认同)