0

I am trying to upload in-memory zip file to S3 bucket with zipfile Python package but the setpassword() is not working

import csv
import zipfile
import io

#S3 connection with credential

s3 = boto3.resource('s3', endpoint_url='',
  aws_access_key_id = '',
  aws_secret_access_key = '')

#write csv in buffer, df dataframe

buffer_string_csv = io.StringIO()
df.to_csv(buffer_string_csv)

#write zip in buffer

buffer_zip = io.BytesIO()
zip_file = zipfile.ZipFile(buffer_zip, 'w')
zip_file.setpassword(b'123')
zip_file.writestr('foobar.csv', buffer_string_csv.getvalue())
zip_file.close()
s3.Bucket('QAS').put_object(Body = buffer_zip.getvalue(),  Key = 'foobar.zip')

Is there any other way to solve this problem?

  • The [documentation on setpassword](https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile.setpassword) is pretty clear it's only for reading. – Anon Coward Jan 02 '23 at 16:19
  • Does this answer your question? [zipfile: how to set a password for a Zipfile?](https://stackoverflow.com/questions/43439979/zipfile-how-to-set-a-password-for-a-zipfile) – Mehmet Güngören Jan 02 '23 at 16:58

1 Answers1

0

Instead of zipfile package I used pyzipper package and it solved my problem.

zip_file = pyzipper.AESZipFile(buffer_zip, 'w',
                         compression=pyzipper.ZIP_LZMA,
                         encryption=pyzipper.WZ_AES)