Last active
June 19, 2020 19:39
-
-
Save kouk/0f8b19979c0bc636c604cc4f2a664b54 to your computer and use it in GitHub Desktop.
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
import io | |
import boto3 | |
import datetime | |
from zipfile import ZipFile | |
from dateutil.tz import tzutc | |
from botocore.config import Config | |
outfile = sys.argv[1] | |
fromdate = datetime.datetime(2020, 5, 7, 0, 0, 0, tzinfo=tzutc()) | |
bucket = 'endlesspit' | |
prefix = 'somefiles/' | |
class Buf(io.BytesIO): | |
def getAndReset(self): | |
try: | |
return self.getvalue() | |
finally: | |
self.seek(0) | |
self.truncate() | |
b = boto3.resource('s3', config=Config( | |
retries = { | |
'max_attempts': 10, | |
'mode': 'adaptive' | |
} | |
) | |
).Bucket(bucket) | |
buf = Buf() | |
with ZipFile(outfile, 'w') as zf: | |
for i in b.objects.filter(Prefix=prefix): | |
if i.last_modified >= fromdate: | |
i.Object().download_fileobj(buf) | |
member = ZipInfo(i.key, i.last_modified.timetuple()) | |
zf.writestr(member, buf.getAndReset()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example:
aws-vault exec -d 24h myprofile -- python3 zip-some-s3-files.py output.zip