How to set metadata in S3 using boto?

谁说胖子不能爱 提交于 2021-02-10 12:17:48

问题


I am trying to set metadata during pushing a file to S3.

This is how it looks like :

def pushFileToBucket(fileName, bucket, key_name, metadata):
    full_key_name = os.path.join(fileName, key_name)
    k = bucket.new_key(full_key_name)
    k.set_metadata('my_key', 'value')
    k.set_contents_from_filename(fileName)

For some reason this throws error at set_metadata saying :

boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden
<?xml version="1.0" encoding="UTF-8"?><Error><Code>SignatureDoesNotMatch</Code></Error>

And when I remove this set_metadata part, the file is getting stored correctly. Not sure what I am doing wrong. If the access key was invalid, then it wouldn't have saved the file anyway!


回答1:


Another approach for someone using upload_file:

s3 = boto3.client('s3')
path = 'foo/bar.json'
file = 'bar.json'
bucket_name = 'foobar_bucket'
extra_args = {'CacheControl': 'max-age=86400'}
s3.upload_file(path, bucket_name, file_name, extra_args)

This would set the Cache-Control header on the file.




回答2:


Got this fixed. Apparently we cannot have an underscore in the metadata key name.



来源:https://stackoverflow.com/questions/34415925/how-to-set-metadata-in-s3-using-boto

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