Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the S3 object lifecycle using boto?

I have uploaded several files to Amazon S3 using boto. However, I failed to set a lifecycle using statement (I know this can be done using the AWS Management Console, but I need to allow each user to decide how long want to keep the file).

The boto API reference for S3 properly documents configure_lifecycle(lifecycle_config, headers=None) as the solution, but I'm unable to configure this. Can anyone correct my code?

Thanks!

key='key'
secretkey='secretkey'

#build the connection
conn = S3Connection(key, secretkey)
bucket = conn.create_bucket('przm')
k=Key(bucket)

#select and upload the file
name1='run1'
k.key=name1
k.set_contents_from_filename('RUN')
link1='https://s3.amazonaws.com/przm/'+name1
#allow anyone can download this file
k.set_acl('public-read-write')

#delete this file after one day. Can anyone give me some help here?
configure_lifecycle(lifecycle_config, headers=None)
like image 248
TTT Avatar asked May 11 '12 16:05

TTT


People also ask

How do I create a lifecycle policy for an S3 bucket?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to create a lifecycle rule for. Choose the Management tab, and choose Create lifecycle rule. In Lifecycle rule name, enter a name for your rule.

What is the lifecycle of object stored in S3?

An S3 Lifecycle configuration is an XML file that consists of a set of rules with predefined actions that you want Amazon S3 to perform on objects during their lifetime.

Does S3 lifecycle apply to existing objects?

S3 bucket lifecycle rules apply to both existing and new data in S3. In addiiton to Mircea answer: New lifecycle rules can take up to 48 hours to complete the first run. If there are millions of objects for the rule, then it might take a few runs for all the objects to be transitioned into another storage class.


1 Answers

You aren't showing where "lifecycle_config" comes from in this example. However, what you should do is create a Lifecycle object, like this:

import boto.s3.lifecycle
lifecycle_config = boto.s3.lifecycle.Lifecycle()
lifecycle_config.add_rule('lc_rule_1', '/', 'Enabled', 1)

See class boto.s3.lifecycle for details about the Lifecycle object and what the above parameters mean.

Once you have a Lifecycle object, you can then use that in the call to configure_lifecycle(), like this:

bucket.configure_lifecycle(lifecycle_config)
like image 157
garnaat Avatar answered Oct 11 '22 18:10

garnaat