使用 AWS Java SDK 为现有 S3 对象设置 Expires 标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9797276/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:43:56  来源:igfitidea点击:

Set Expires header for an existing S3 object using AWS Java SDK

javaamazon-s3http-headersamazon-web-services

提问by bkirkbri

I'm updating existing objects in an Amazon S3 bucket to set some metadata. I'd like to set the HTTP Expiresheader for each object to better handle HTTP/1.0 clients.

我正在更新 Amazon S3 存储桶中的现有对象以设置一些元数据。我想Expires为每个对象设置 HTTP标头以更好地处理 HTTP/1.0 客户端。

We're using the AWS Java SDK, which allows for metadata changes to an object without re-uploading the object content. We do this using CopyObjectRequestto copy an object to itself. The ObjectMetadataclass allows us to set the Cache-Control, Content-Typeand several other headers. But not the Expiresheader.

我们正在使用AWS Java SDK,它允许在不重新上传对象内容的情况下更改对象的元数据。我们使用CopyObjectRequest将对象复制到自身。该ObjectMetadata类允许我们设置的Cache-ControlContent-Type和其他几个头。但不是Expires标题。

I know that S3 stores and serves the Expiresheader for objects PUT using the REST API. Is there a way to do this from the Java SDK?

我知道 S3Expires使用 REST API存储和提供对象 PUT的标头。有没有办法从 Java SDK 中做到这一点?

Updated to indicate that we are usingCopyObjectRequest

更新以表明我们正在使用CopyObjectRequest

采纳答案by Steffen Opel

To change the metadataof an existing Amazon S3object, you need to copy the object to itself and provide the desired new metadata on the fly, see copyObject():

要更改现有Amazon S3对象的元数据,您需要将对象复制到自身并即时提供所需的新元数据,请参阅copyObject()

By default, all object metadata for the source object are copied to the new destination object, unless new object metadata in the specified CopyObjectRequest is provided.

默认情况下,源对象的所有对象元数据都被复制到新的目标对象,除非在指定的 CopyObjectRequest 中提供了新的对象元数据。

This can be achieved like so approximately (fragment from the top of my head, so beware):

这可以大致实现(来自我头顶的片段,所以要小心):

AmazonS3 s3 = new AmazonS3Client();

String bucketName = "bucketName ";
String key = "key.txt";
ObjectMetadata newObjectMetadata = new ObjectMetadata();
// ... whatever you desire, e.g.:
newObjectMetadata.setHeader("Expires", "Thu, 21 Mar 2042 08:16:32 GMT");

CopyObjectRequest copyObjectRequest = new CopyObjectRequest()
                 .WithSourceBucketName(bucketName)
                 .WithSourceKey(key)
                 .WithDestinationBucket(bucketName)
                 .WithDestinationKey(key)
                 .withNewObjectMetadata(newObjectMetadata);

s3.copyObject(copyObjectRequest);

Please be aware of the following easy to miss, but important copyObject()constraint:

请注意以下容易错过但重要的copyObject()约束:

The Amazon S3 Acccess Control List (ACL) is notcopied to the new object. The new object will have the default Amazon S3 ACL, CannedAccessControlList.Private, unless one is explicitly provided in the specified CopyObjectRequest.

Amazon S3 访问控制列表 (ACL)不会复制到新对象。除非在指定的 CopyObjectRequest 中明确提供,否则新对象将具有默认的 Amazon S3 ACL CannedAccessControlList.Private。

This is notaccounted for in my code fragment yet!

我的代码片段中还没有考虑到这一点!

Good luck!

祝你好运!

回答by Anoop Halgeri

We were looking for a similar solution and eventually settled for max-age cache-control directive. And we eventually realized that hte cache-control overrides the Expires even if expires is more restrictive. And anyways cache-control met our requirement as well.

我们一直在寻找类似的解决方案,最终选择了 max-age cache-control 指令。我们最终意识到即使 expires 更具限制性,hte cache-control 也会覆盖 Expires。无论如何,缓存控制也满足了我们的要求。