Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating AWS S3 expiration time

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.

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.

I have tried this using the AWS SDK to no avail.

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

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.

I then tried a variation on the above based on this link (added setStorageClass): https://alestic.com/2013/09/s3-lifecycle-extend/

Any help is appreciated.

like image 794
redboy Avatar asked May 19 '15 16:05

redboy


1 Answers

It would appear that you are trying to achieve the following:

  • Define a bucket with a Lifecycle Rule that will permanently delete objects after 90 days
  • Upload objects into the bucket
  • For certain specified objects, restart the 90-day timeout

As per the alestic blog post you referenced, this can be done by copying the object on top of itself. In fact, it doesn't even seem to need the change of storage class.

I uploaded an object to a bucket, then ran this Python code:

import boto
conn = boto.s3.connect_to_region('ap-southeast-2')
mybucket = conn.get_bucket('BUCKET-NAME')
mybucket.copy_key('OBJECT-NAME', 'BUCKET-NAME', 'OBJECT-NAME')

This code copied the object "to itself" and the Management Console showed a new last modified date. I did not test the lifecycle rule, but I would assume this is the date used to determine the start of the 90-day period.

like image 123
John Rotenstein Avatar answered Sep 28 '22 09:09

John Rotenstein