Jimmy
Jimmy

Reputation: 12527

Boto - Grab most recent from S3

I have got a bit out my depth. I want to store backups on S3 and then I am trying to download a script when downloads the most lastest modified file within a specific "folder" of an S3 bucket.

I got this far:

import boto
s3conn = boto.connect_s3()
bucket = s3conn.lookup('my_bucket_name')
for key in bucket:
  print k.name, k.last_modified

key.getfile()

So far, this is designed to to get the name and last modified of each file in the bucket. This is where I get stuck really, because I need to get the most recent and then download that, and that is where I am stuck.

Would anyone be able to offer a helping hand?

Upvotes: 2

Views: 2577

Answers (1)

khagler
khagler

Reputation: 4056

You need to sort your list of keys by last_modifed, then the last item of the list will be the file you want. Something like this:

key_list = bucket.list()
key_list.sort(cmp = lambda x, y:
    cmp(x.last_modified, y.last_modified))
key_list[-1].get_file(destination_fp)

Here's another approach to sorting you could try:

key_list.sort(key=lambda x: x.last_modified)

Upvotes: 1

Related Questions