设置Expires头使用AWS Java SDK的现有S3对象 [英] Set Expires header for an existing S3 object using AWS Java SDK
问题描述
我更新现有对象在亚马逊S3存储设置一些元数据。我想每个对象的HTTP 过期
头设置为更好地处理HTTP / 1.0客户端。
I'm updating existing objects in an Amazon S3 bucket to set some metadata. I'd like to set the HTTP Expires
header for each object to better handle HTTP/1.0 clients.
我们正在使用 AWS Java SDK的,这使得元数据更改的对象,而无需重新上传对象内容。为此,我们使用<一个href="http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/model/CopyObjectRequest.html">CopyObjectRequest到对象复制到自身。该<一href="http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/model/ObjectMetadata.html">ObjectMetadata类允许我们设置的Cache-Control
,内容类型
和其它几个头。而不是过期
头。
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 CopyObjectRequest to copy an object to itself. The ObjectMetadata class allows us to set the Cache-Control
, Content-Type
and several other headers. But not the Expires
header.
我知道,S3存储服务于过期
的对象头PUT使用REST API。有没有办法从Java SDK做到这一点?
I know that S3 stores and serves the Expires
header for objects PUT using the REST API. Is there a way to do this from the Java SDK?
更新,以表明我们使用 CopyObjectRequest
推荐答案
要改变元 Amazon S3的对象>,您需要将对象复制到自身,并提供在飞行所需要的新的元数据,请参见<一href="http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3Client.html#copyObject%28com.amazonaws.services.s3.model.CopyObjectRequest%29">copyObject():
To change the metadata of an existing Amazon S3 object, you need to copy the object to itself and provide the desired new metadata on the fly, see copyObject():
默认情况下,源对象的所有对象元数据被复制到 新的目标对象,除非在新的对象元数据 提供指定CopyObjectRequest。
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.
这可以实现像这样大约(片段从我的头顶,所以要小心):
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);
请注意以下容易错过,但很重要的<一个href="http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3Client.html#copyObject%28com.amazonaws.services.s3.model.CopyObjectRequest%29">copyObject()约束:
Please be aware of the following easy to miss, but important copyObject() constraint:
亚马逊S3 Acccess控制列表(ACL)中的不可以复制到新的 目的。新对象将有默认的Amazon S3的访问控制列表, CannedAccessControlList.Private,除非是明确规定 指定CopyObjectRequest。
The Amazon S3 Acccess Control List (ACL) is not copied 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.
这是不可以占我的code片段呢!
This is not accounted for in my code fragment yet!
祝你好运!
这篇关于设置Expires头使用AWS Java SDK的现有S3对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!