Python 如何更改 Amazon S3 中对象的元数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4754383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change metadata on an object in Amazon S3
提问by natevw
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 beforethe object is created as it just effects a local dictionary.
如果您已经将对象上传到 Amazon S3 存储桶,您如何使用 API 更改元数据?可以在 AWS 管理控制台中执行此操作,但尚不清楚如何以编程方式执行此操作。具体来说,我在 Python 中使用 boto API,从阅读源代码可以看出,使用 key.set_metadata 仅在创建对象之前有效,因为它只会影响本地字典。
采纳答案by natevw
It appears you need to overwrite the object with itself, using a "PUT Object (Copy)" with an x-amz-metadata-directive: REPLACEheader in addition to the metadata. In boto, this can be done like this:
x-amz-metadata-directive: REPLACE除了元数据之外,您似乎还需要使用带有标题的“PUT 对象(复制)”来覆盖对象本身。在 boto 中,这可以像这样完成:
k = k.copy(k.bucket.name, k.name, {'myKey':'myValue'}, preserve_acl=True)
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:
请注意,您未包含在旧字典中的任何元数据都将被删除。因此,要保留旧属性,您需要执行以下操作:
k.metadata.update({'myKey':'myValue'})
k2 = k.copy(k.bucket.name, k.name, k.metadata, preserve_acl=True)
k2.metadata = k.metadata # boto gives back an object without *any* metadata
k = k2;
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 对象的内容处理
回答by John Bachir
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?
您可以使用 copy 命令更改元数据,而无需重新加载对象。请参阅此问题:是否可以在不下载整个对象的情况下更改 S3 对象上的标头?
回答by kartik
In order to set metadata on S3 files,just don't provide target location as only source information is enough to set metadata.
为了在 S3 文件上设置元数据,不要提供目标位置,因为只有源信息就足以设置元数据。
final ObjectMetadata metadata = new ObjectMetadata();
metadata.addUserMetadata(metadataKey, value);
final CopyObjectRequest request = new CopyObjectRequest(bucketName, keyName, bucketName, keyName)
.withSourceBucketName(bucketName)
.withSourceKey(keyName)
.withNewObjectMetadata(metadata);
s3.copyObject(request);`
回答by Ted
For the first answer it's a good idea to include the original content type in the metadata, for example:
对于第一个答案,最好在元数据中包含原始内容类型,例如:
key.set_metadata('Content-Type', key.content_type)
回答by eddd
If you want your metadata stored remotely use set_remote_metadata
如果您希望远程存储元数据,请使用 set_remote_metadata
Example:
key.set_remote_metadata({'to_be': 'added'}, ['key', 'to', 'delete'], {True/False})
例子:
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
实现在这里:https: //github.com/boto/boto/blob/66b360449812d857b4ec6a9834a752825e1e7603/boto/s3/key.py#L1875
回答by Jefin Stephan
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
在 Java 中,您可以将对象复制到同一位置。这里元数据在复制对象时不会复制。您必须获取原始元数据并设置为复制请求。更推荐使用此方法插入或更新 Amazon S3 对象的元数据
ObjectMetadata metadata = amazonS3Client.getObjectMetadata(bucketName, fileKey);
ObjectMetadata metadataCopy = new ObjectMetadata();
metadataCopy.addUserMetadata("yourKey", "updateValue");
metadataCopy.addUserMetadata("otherKey", "newValue");
metadataCopy.addUserMetadata("existingKey", metadata.getUserMetaDataOf("existingValue"));
CopyObjectRequest request = new CopyObjectRequest(bucketName, fileKey, bucketName, fileKey)
.withSourceBucketName(bucketName)
.withSourceKey(fileKey)
.withNewObjectMetadata(metadataCopy);
amazonS3Client.copyObject(request);
回答by Rogelio Blanco
here is the code that worked for me. I'm using aws-java-sdk-s3 version 1.10.15
这是对我有用的代码。我使用的是 aws-java-sdk-s3 版本 1.10.15
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(fileExtension.getMediaType());
s3Client.putObject(new PutObjectRequest(bucketName, keyName, tempFile)
.withMetadata(metadata));

