使用gcloud-python在Google Cloud Storage中设置元数据 [英] Set metadata in Google Cloud Storage using gcloud-python

查看:128
本文介绍了使用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中设置元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆