Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markschwarz/0c21df4d0ca14b748a315bb7c4ca2352 to your computer and use it in GitHub Desktop.
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
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