Set metadata in Google Cloud Storage using gcloud-python

北城余情 提交于 2020-01-12 08:13:29

问题


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.


回答1:


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.

Accessing metadata via blob.metadata is read-only. Thus when mutating that result via

blob.metadata['Color'] = 'Pink'

it doesn't actually change the metadata stored on blob.

The current "fix" is to just build up

metadata = {'Color': 'Pink'}
blob.metadata = metadata


来源:https://stackoverflow.com/questions/33145047/set-metadata-in-google-cloud-storage-using-gcloud-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!