如何使用boto设置S3对象生命周期?

TH3*_*339 4 python amazon-s3 boto

我使用boto将几个文件上传到Amazon S3 .但是,我无法使用语句设置生命周期(我知道这可以使用AWS管理控制台完成,但我需要允许每个用户决定保留文件的时间长度).

S3boto API参考正确记录了configure_lifecycle(lifecycle_config,headers = None)作为解决方案,但我无法配置它.谁能纠正我的代码?

谢谢!

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)
Run Code Online (Sandbox Code Playgroud)

gar*_*aat 5

您没有在此示例中显示"lifecycle_config"的来源.但是,您应该做的是创建一个Lifecycle对象,如下所示:

import boto.s3.lifecycle
lifecycle_config = boto.s3.lifecycle.Lifecycle()
lifecycle_config.add_rule('lc_rule_1', '/', 'Enabled', 1)
Run Code Online (Sandbox Code Playgroud)

有关Lifecycle对象的详细信息以及上述参数的含义,请参阅类boto.s3.lifecycle.

一旦有了Lifecycle对象,就可以在configure_lifecycle()的调用中使用它,如下所示:

bucket.configure_lifecycle(lifecycle_config)
Run Code Online (Sandbox Code Playgroud)