Created
June 20, 2017 19:16
-
-
Save consolewitch/ae8ce8c44137e8cba1d8824bcff2e88a to your computer and use it in GitHub Desktop.
untangle files that have been accidentally overwritten in a versioning-enabled s3 bucket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this code will untangle files that have been accidentally overwritten in a versioning-enabled s3 bucket | |
# contact [email protected] for assistance | |
import boto.s3.connection, sys | |
from collections import defaultdict | |
accessKey="XXXXXXXX/XXXXXX.XXXX" | |
secretKey="XXXXXXX/XXXXXXXXXXX.XXXXXXXXX.XXXXXXXXXX" | |
srcKeyPrefix="path/to/key" #no leading or trailing slash | |
srcBucket="destination-bucket-name-here" | |
dstBucket="destination-bucket-name-here" | |
offline_list = defaultdict(list) #we have to save the s3 structure to a local var to deal with paging in the API | |
print "opening connection" | |
scon = boto.s3.connection.S3Connection(accessKey, secretKey) | |
print "opening bucket" | |
bucket = scon.get_bucket(srcBucket) | |
print "storing list of bucket contents locally" | |
bucket_list = bucket.list(prefix=srcKeyPrefix) | |
for i in bucket_list: | |
for j in bucket.list_versions(prefix=i.name): | |
offline_list[i.name].append(j.version_id) | |
print "copying all versioned files into a flat directory" | |
for q in offline_list: | |
count=0 | |
while len(offline_list[q])>0: | |
key_version = offline_list[q].pop() | |
nameParts = q.rsplit('.', 1) | |
key_to_copy = bucket.get_key(q, version_id=key_version) | |
print "cp: " + key_to_copy.name + " v: " + key_to_copy.version_id + " to: " + nameParts[0] + chr(len(offline_list[q])+65) + '.' + nameParts[1] | |
print "mod: " + key_to_copy.last_modified | |
key_to_copy.copy(dstBucket, nameParts[0] + chr(len(offline_list[q])+65) + '.' + nameParts[1]) | |
count+=1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment