如何更改 Amazon S3 中对象的元数据
如果您已将对象上传到 Amazon S3 存储桶,如何使用 API 更改元数据?可以在 AWS 管理控制台中执行此操作,但尚不清楚如何以编程方式完成此操作。具体来说,我在 Python 中使用 boto API,从阅读源代码可以清楚地看出,使用 key.set_metadata 仅在创建对象之前有效,因为它只会影响本地字典。
If you have already uploaded an object to an Amazon S3 bucket, how do you change the metadata using the API? It is possible to do this in the AWS Management Console, but it is not clear how it could be done programmatically. Specifically, I'm using the boto API in Python and from reading the source it is clear that using key.set_metadata only works before the object is created as it just effects a local dictionary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
看来您需要使用“PUT 对象(副本)”和
x-amz-metadata-directive: REPLACE
标头以及元数据来覆盖对象本身。在 boto 中,可以这样完成:请注意,旧字典中未包含的任何元数据都将被删除。因此,为了保留旧属性,您需要执行以下操作:
我几乎错过了这个解决方案,该解决方案在标题错误的问题的介绍中暗示,该问题实际上与此问题有关不同的问题: 更改现有 S3 对象的内容处置
It appears you need to overwrite the object with itself, using a "PUT Object (Copy)" with an
x-amz-metadata-directive: REPLACE
header in addition to the metadata. In boto, this can be done like this:Note that any metadata you do not include in the old dictionary will be dropped. So to preserve old attributes you'll need to do something like:
I almost missed this solution, which is hinted at in the intro to an incorrectly-titled question that's actually about a different problem than this question: Change Content-Disposition of existing S3 object
为了在 S3 文件上设置元数据,只需不提供目标位置,因为只有源信息足以设置元数据。
In order to set metadata on S3 files,just don't provide target location as only source information is enough to set metadata.
如果您希望远程存储元数据,请使用
set_remote_metadata
示例:
key.set_remote_metadata({'to_be': 'added'}, ['key', 'to', 'delete'], {True/False})
实现如下:
https://github.com/boto/boto/blob/66b360449812d857b4ec6a9834a7528 25e1e7603/博托/ s3/key.py#L1875
If you want your metadata stored remotely use
set_remote_metadata
Example:
key.set_remote_metadata({'to_be': 'added'}, ['key', 'to', 'delete'], {True/False})
Implementation is here:
https://github.com/boto/boto/blob/66b360449812d857b4ec6a9834a752825e1e7603/boto/s3/key.py#L1875
您可以使用复制命令更改元数据,而无需重新加载对象。请参阅此问题: 是否可以在不下载整个对象的情况下更改 S3 对象上的标头?
You can change the metadata without re-uloading the object by using the copy command. See this question: Is it possible to change headers on an S3 object without downloading the entire object?
对于第一个答案,最好将原始内容类型包含在元数据中,例如:
For the first answer it's a good idea to include the original content type in the metadata, for example:
在Java中,您可以将对象复制到同一位置。
这里复制对象时不会复制元数据。
您必须获取原始元数据并设置为复制请求。
更推荐使用此方法来插入或更新 Amazon S3 对象的元数据
In Java, You can copy object to the same location.
Here metadata will not copy while copying an Object.
You have to get metadata of original and set to copy request.
This method is more recommended to insert or update metadata of an Amazon S3 object
这是对我有用的代码。我正在使用 aws-java-sdk-s3 版本 1.10.15
here is the code that worked for me. I'm using aws-java-sdk-s3 version 1.10.15