4

I've multiple files under a specific folder in s3 bucket , I want to create a single zip file of all those files. Is there a way that I can do loop across all the files in s3 bucket and make zip? I am able to do one file but I want to do all files at a time and make one zip out of it (all files in a folder)

Any help is appreciated.

s3 = boto3.client('s3')
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zipper:
     infile_object = s3.get_object(Bucket=bucket, Key=object_key)
     infile_content = infile_object['Body'].read()
     zipper.writestr(file_name, infile_content)
s3.put_object(Bucket=bucket, Key=PREFIX + "test.zip", Body=zip_buffer.getvalue())
  • this will help https://stackoverflow.com/questions/49772151/download-a-folder-from-s3-using-boto3 – deadshot Mar 24 '22 at 07:49
  • [One solution](https://gist.github.com/Q726kbXuN/2a385620bf2cfcc5112b0b397aebaee5), there are many others, this is an often asked question. – Anon Coward Mar 24 '22 at 15:39

0 Answers0