Skip to content

Instantly share code, notes, and snippets.

@fim
Created November 13, 2014 17:33
Show Gist options
  • Save fim/7536eb4230b6f063751f to your computer and use it in GitHub Desktop.
Save fim/7536eb4230b6f063751f to your computer and use it in GitHub Desktop.
Get specific version of file from versioned bucket
#!/usr/bin/env python
import boto
import sys
conn = boto.connect_s3()
bid = -1
print "Select bucket to restore from:"
while bid < 0:
for i,b in enumerate(conn.get_all_buckets()):
print "%d\t%s" % ( i, b.name )
try:
bid = int(raw_input("Enter bucket id: "))
if bid > i: raise Exception
except:
print "Incorrect id. Please enter an id from the list:"
bid = -1
mb = conn.get_all_buckets()[bid]
fpath = raw_input("Enter full path for the file to restore: ")
flist = mb.list_versions(prefix=fpath)
count = 0
for f in flist:
if hasattr(f, "DeleteMarker"):
continue
print "%s\t%s" % (f.version_id, f.last_modified)
count += 1
if count == 0:
print "No files matching the pattern \"%s\" were found! Exiting..." % fpath
sys.exit(1)
version_id = raw_input("Pick a restoration date by id: ")
ritem = mb.get_key(key_name=fpath, version_id=version_id)
print "\nThe url for your file is:\n"
print ritem.generate_url(expires_in=600)
print "\nThis url will expire in 600 seconds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment