I am trying to create a script which grabs the most recently dated file from an S3 amazon bucket. This is the code I am working with at the moment:
#!/usr/bin/env python
import boto
import boto.s3
from boto.s3.key import Key
# Define variables
AWS_ACCESS_KEY_ID = ''
AWS_SECRET_ACCESS_KEY = ''
BUCKET_NAME = 'My Bucket'
BUCKET = conn.get_bucket(bucket_name)
CONN = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
for key in bucket.list():
print key.name
I havent had chance to sign up to amazon to test this yet, but I found this code on the internet and it should list all the files in the bucket.
Each of my files are dated with the following name format:
NOW = datetime.datetime.now().strftime("%Y-%m-%d__%H-%M")
Example: 1999-01-30__10-30 (10:30 on the 30th Jan 1999)
My question is, if I wanted to work out which is the most recent file based on its dated filename, what is a good way of going about this?
Just sort the filenames; the most recent file will be the last one: