更新 AWS S3 过期时间 [英] Updating AWS S3 expiration time
问题描述
我想知道如何延长密钥的到期时间.我指的不是访问对象的签名 URL,而是密钥本身.
I would like to know how to EXTEND a key's expiration. I am not referring to the signed URL that accesses the object but the key itself.
我为 AWS 设置了一条规则,即存储桶中的所有对象都应在 90 天内过期.这就是我希望在大多数情况下发生的事情.有时,我需要将单个密钥的生命周期再延长 90 天.事实上,重置倒计时很好.
I setup AWS to have a rule that all objects within the bucket should expire in 90 days. This is what I want to happen in most cases. Now and then I need to extend an individual key's lifetime by up to an additional 90 days. In fact resetting the countdown is fine.
我已经使用 AWS 开发工具包进行了尝试,但无济于事.
I have tried this using the AWS SDK to no avail.
我的第一次尝试是使用 copyObject 更改元数据:
My First attempt was to alter metadata using copyObject:
ObjectMetadata meta = s3client.getObjectMetadata( bucket, key );
meta.setExpirationTime( getFutureDate( 90*DAYS ) );
CopyObjectRequest copyObjectRequest = new CopyObjectRequest( bucket, key, bucket, key );
copyObjectRequest.setNewObjectMetadata( meta );
copyObjectRequest.setStorageClass( StorageClass.Standard );
s3client.copyObject( copyObjectRequest );
以上方法无效.JavaDoc 甚至告诉我原因(尝试后阅读).
The above did not work. The JavaDoc even clued me in as to why (read it after attempting).
void com.amazonaws.services.s3.model.ObjectMetadata.setExpirationTime(Date expirationTime)
For internal use only. This will *not* set the object's expiration time, and is only used to set the value in the object after receiving the value in a response from S3.
然后我尝试了基于此链接的上述变体(添加了 setStorageClass):https://alestic.com/2013/09/s3-lifecycle-extend/一个>
I then tried a variation on the above based on this link (added setStorageClass): https://alestic.com/2013/09/s3-lifecycle-extend/
感谢任何帮助.
推荐答案
我确实有正确的答案.下面的 Python 代码可能会执行将 BUCKET/KEY 复制到自身上的相同技巧,并且还会重置到期时间.AWS S3 基于 24 小时时钟将到期时间向上舍入到同一时间,因此在同一天完成的任何复制都不会引起注意.我让密钥在 90 天刷新后放置一周左右以进行测试,看起来上次修改时间"+ ExpirationRuleDays =到期日期"确实成立.
I indeed have the right answer. The below Python code likely does the same trick of copying the BUCKET/KEY onto itself and would also reset the expiration. AWS S3 rounds up the expiry time to the same time based on a 24hr clock so any copying done on the same day is not something that will be noticed. I let keys sit for a week or so on a 90 day refresh to do testing and it does seem like the "Last Modified" + ExpirationRuleDays = "Expiry Date" holds.
我创建了两个密钥,在运行以下代码后,我从 S3 管理控制台获得了以下信息.
I have two keys created and after running the below code I got the following form the S3 Management Console.
旧 - 1
Last Modified: Thu Jun 04 15:48:58 GMT-700 2015
Expiry Date: Wed Sep 02 17:00:00 GMT-700 2015
Expiration Rule: Auto-Expire after 90 days
新 - 1
Last Modified: Mon Jun 08 15:37:01 GMT-700 2015
Expiry Date: Sun Sep 06 17:00:00 GMT-700 2015
Expiration Rule: Auto-Expire after 90 days
旧 - 2
Last Modified: Thu Jun 04 13:34:36 GMT-700 2015
Expiry Date: Wed Sep 02 17:00:00 GMT-700 2015
Expiration Rule: Auto-Expire after 90 days
新 - 2
Last Modified: Mon Jun 08 15:41:19 GMT-700 2015
Expiry Date: Sun Sep 06 17:00:00 GMT-700 2015
Expiration Rule: Auto-Expire after 90 days
代码:
public void refreshURLTTL( String messageId ) {
AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider(configFileName, profileName));
ObjectMetadata meta = s3client.getObjectMetadata(BUCKETNAME, KEY);
ObjectMetadata newmeta = meta.clone();
Date oldExpiration = meta.getExpirationTime();
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(BUCKETNAME, KEY, BUCKETNAME, KEY);
copyObjectRequest.setNewObjectMetadata(newmeta);
s3client.copyObject(copyObjectRequest);
ObjectMetadata finalmeta = s3client.getObjectMetadata(BUCKETNAME, KEY);
Date currentExpiration = finalmeta.getExpirationTime();
System.out.println("OLD: "+oldExpiration);
System.out.println("NEW: "+currentExpiration);
}
这篇关于更新 AWS S3 过期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!