Set metadata in Google Cloud Storage using gcloud-python(使用 gcloud-python 在 Google Cloud Storage 中设置元数据)
问题描述
我正在尝试使用 gcloud-python
将文件上传到 Google Cloud Storage 并设置一些自定义元数据属性.为了尝试这个,我创建了一个简单的脚本.
I am trying to upload a file to Google Cloud Storage using gcloud-python
and set some custom metadata properties. To try this I have created a simple script.
import os
from gcloud import storage
client = storage.Client('super secret app id')
bucket = client.get_bucket('super secret bucket name')
blob = bucket.get_blob('kirby.png')
blob.metadata = blob.metadata or {}
blob.metadata['Color'] = 'Pink'
with open(os.path.expanduser('~/Pictures/kirby.png'), 'rb') as img_data:
blob.upload_from_file(img_data)
我可以上传文件内容.上传文件后,我可以从开发者控制台手动设置元数据并检索它.
I am able to upload the file contents. After uploading the file I am able to manually set metadata from the developer console and retrieve it.
我不知道如何以编程方式上传元数据.
I can't figure out how to upload the metadata programmatically.
推荐答案
我们讨论过 在问题跟踪器上,它在实现中出现了一个错误",或者至少是让用户措手不及的东西.
We discussed on the issue tracker and it surfaced a "bug" in the implementation, or at the very least something which catches users off guard.
通过 blob.metadata
访问 metadata
是只读的.因此,当通过
Accessing metadata
via blob.metadata
is read-only. Thus when mutating that result via
blob.metadata['Color'] = 'Pink'
它实际上并没有改变存储在 blob
上的元数据.
it doesn't actually change the metadata stored on blob
.
目前的修复"是建立起来
The current "fix" is to just build up
metadata = {'Color': 'Pink'}
blob.metadata = metadata
这篇关于使用 gcloud-python 在 Google Cloud Storage 中设置元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 gcloud-python 在 Google Cloud Storage 中设置元数据


基础教程推荐
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01