Last active
August 31, 2018 20:23
-
-
Save markschwarz/0c21df4d0ca14b748a315bb7c4ca2352 to your computer and use it in GitHub Desktop.
S3 Versioning - Get all versions for S3 file, saved with correct OS modified timestaps
This file contains 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
import boto3 | |
s3 = boto3.resource('s3') | |
import os | |
file_root = '<your_root_pathname_here>' | |
bucket = s3.Bucket('<your_bucket_name_here>') | |
prefix = '<your_prefix_aka_folder_name_here' | |
last_version = None | |
for version in bucket.object_versions.filter(Prefix=prefix): | |
filename = version.key + '_' + version.id | |
full_path = file_root + filename | |
bucket.download_file(version.key, file_root + filename, ExtraArgs={'VersionId': version.id} ) | |
os.utime(full_path, (version.last_modified.timestamp(), version.last_modified.timestamp())) | |
print('Saved {} and set modified time {}'.format(full_path, version.last_modified)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment