如何更改 Amazon S3 中对象的元数据

发布于 2024-10-12 19:15:07 字数 170 浏览 2 评论 0原文

如果您已将对象上传到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

打小就很酷 2024-10-19 19:15:07

看来您需要使用“PUT 对象(副本)”和 x-amz-metadata-directive: REPLACE 标头以及元数据来覆盖对象本身。在 boto 中,可以这样完成:

k = k.copy(k.bucket.name, k.name, {'myKey':'myValue'}, preserve_acl=True)

请注意,旧字典中未包含的任何元数据都将被删除。因此,为了保留旧属性,您需要执行以下操作:

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;

我几乎错过了这个解决方案,该解决方案在标题错误的问题的介绍中暗示,该问题实际上与此问题有关不同的问题: 更改现有 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:

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

飘过的浮云 2024-10-19 19:15:07

为了在 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);`

In order to set metadata on S3 files,just don't provide target location as only source information is enough to set metadata.

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);`
勿忘初心 2024-10-19 19:15:07

如果您希望远程存储元数据,请使用 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

攒眉千度 2024-10-19 19:15:07

您可以使用复制命令更改元数据,而无需重新加载对象。请参阅此问题: 是否可以在不下载整个对象的情况下更改 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?

眼泪都笑了 2024-10-19 19:15:07

对于第一个答案,最好将原始内容类型包含在元数据中,例如:

key.set_metadata('Content-Type', key.content_type) 

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) 
一梦等七年七年为一梦 2024-10-19 19:15:07

在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);

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

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);
半枫 2024-10-19 19:15:07

这是对我有用的代码。我正在使用 aws-java-sdk-s3 版本 1.10.15

ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(fileExtension.getMediaType());

s3Client.putObject(new PutObjectRequest(bucketName, keyName, tempFile)
                    .withMetadata(metadata));

here is the code that worked for me. I'm using aws-java-sdk-s3 version 1.10.15

ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(fileExtension.getMediaType());

s3Client.putObject(new PutObjectRequest(bucketName, keyName, tempFile)
                    .withMetadata(metadata));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文