python - S3 Object Expiration using boto -
python - S3 Object Expiration using boto -
i trying figure out way clean s3 bucket. want delete keys older x days ( in case x 30 days).
i couldn't figure out way delete objects in s3.
i used next approaches, none of worked (by worked, mean tried getting object after x days, , s3 still serving object. expecting "object not found" or "object expired" message
approach 1:
k = key(bucket) k.key = my_key_name expires = datetime.utcnow() + timedelta(seconds=(10)) expires = expires.strftime("%a, %d %b %y %h:%m:%s gmt") k.set_contents_from_filename(filename,headers={'expires':expires})
approach 2:
k = key(bucket) k.key = "event_" + str(key_name) + "_report" expires = datetime.utcnow() + timedelta(seconds=(10)) expires = expires.strftime("%a, %d %b %y %h:%m:%s gmt") k.set_meta_data('expires', expires) k.set_contents_from_filename(filename)
if can share code working them, deletes s3 objects, great
you can utilize lifecycle policies delete objects s3 older x days. example, suppose have these objects:
logs/first logs/second logs/third otherfile.txt
to expire under logs/ after 30 days, you'd say:
import boto boto.s3.lifecycle import lifecycle, expiration lifecycle = lifecycle() lifecycle.add_rule('rulename', prefix='logs/', status='enabled', expiration=expiration(days=30)) s3 = boto.connect_s3() bucket = s3.get_bucket('boto-lifecycle-test') bucket.configure_lifecycle(lifecycle)
you can retrieve lifecycle configuration:
>>> config = bucket.get_lifecycle_config() >>> print(config[0]) <rule: ruleid> >>> print(config[0].prefix) logs/ >>> print(config[0].expiration) <expiration: in: 30 days>
python amazon-s3 boto
Comments
Post a Comment