2
\$\begingroup\$

I had a question regarding my code which downloads a file from S3 with the highest (most recent) timedated filename format:

YYYYMMDDHHMMSS.zip

from boto.s3.connection import S3Connection
from boto.s3.key import Key
import sys
conn = S3Connection('____________________', '________________________________________')
bucket = conn.get_bucket('bucketname')
rs = bucket.list("FamilyPhotos")

for key in rs:
    print key.name

keys = [(key,datetime.strptime(key.name,'%Y%m%d%H%M%S.zip')) for key in rs]
sorted(keys, key = lambda element : element[1])
latest = keys[0][1]
latest.get_contents_to_filename()

I havent done a lot of python before so I would really appreciate some feedback.

\$\endgroup\$
2
  • \$\begingroup\$ What are you looking for specifically? \$\endgroup\$ Dec 18, 2012 at 12:16
  • 1
    \$\begingroup\$ The part of the script that works out the most recently dated filename. I am sure there is a better way of doing it. \$\endgroup\$
    – Jimmy
    Dec 18, 2012 at 12:34

1 Answer 1

3
\$\begingroup\$

The names will properly compare lexically even without converting them to datetimes, and you can just use the max function:

from boto.s3.connection import S3Connection
from boto.s3.key import Key

conn = S3Connection(XXX, YYY)
bucket = conn.get_bucket('bucketname')
rs = bucket.list("FamilyPhotos")
latest = max(rs, key=lambda k: k.name)
latest.get_contents_to_filename()
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.