[英]Why doesn't the password open my zip file in s3 when passed as a bytes object in python?
I have a small but mysterious and unsolvable problem using python to open a password protected file in an AWS S3 bucket.我有一个小但神秘且无法解决的问题,使用 python 在 AWS S3 存储桶中打开受密码保护的文件。
The password I have been given is definitely correct and I can download the zip to Windows and extract it to reveal the csv data I need.我得到的密码绝对正确,我可以将 zip 下载到 Windows 并将其解压缩以显示我需要的 csv 数据。 However I need to code up a process to load this data into a database regularly.
但是我需要编写一个过程来定期将此数据加载到数据库中。
The password has a pattern like this (includes mixed case letters, numbers and a single "@"):-密码具有这样的模式(包括混合大小写字母、数字和单个“@”):-
ABCD@Efghi12324567890
The code below works with other zip files I place in the location with the same password:-下面的代码适用于我放置在具有相同密码的位置的其他 zip 文件:-
import boto3
import pyzipper
from io import BytesIO
s3_resource = boto3.resource('s3', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
zip_obj = s3_resource.Object(bucket_name=my_bucket, key=my_folder + my_zip)
buffer = BytesIO(zip_obj.get()["Body"].read())
z = pyzipper.ZipFile(buffer)
my_newfile=z.namelist()[0]
s3_resource.meta.client.upload_fileobj(
z.open(my_newfile, pwd=b"ABCD@Efghi12324567890"), #HERE IS THE OPEN COMMAND
Bucket=my_bucket,
Key=my_folder + my_newfile)
I am told the password is incorrect:-我被告知密码不正确:-
RuntimeError: Bad password for file 'ThisIsTheFileName.csv'
I resorted to using pyzipper rather than zipfile, since zipfile didn't support the compression method of the file in question:-我求助于使用 pyzipper 而不是 zipfile,因为 zipfile 不支持相关文件的压缩方法:-
That compression method is not supported
In 7-zip I can see the following for the zip file:-在 7-zip 中,我可以看到 zip 文件的以下内容:-
Method: AES-256 Deflate
Characteristics: WzAES: Encrypt
Host OS: FAT
So to confirm:-所以要确认:-
-The password is definitely correct (can open it manually) - 密码绝对正确(可以手动打开)
-The code seems ok - it opens my zip files with the same password - 代码看起来没问题 - 它用相同的密码打开我的 zip 文件
What is the issue here please and how do I fix it?请问这里有什么问题,我该如何解决?
You would have my sincere thanks!你会得到我真诚的感谢!
Phil菲尔
With some help from a colleague and a useful article, I now have this working.在一位同事和一篇有用的文章的帮助下,我现在可以进行这项工作。
Firstly as per the compression type, I have found it necessary to use the AESZipFile() method of pyzipper (although this method also seemed to work on other compression types).首先,根据压缩类型,我发现有必要使用 pyzipper 的 AESZipFile() 方法(尽管此方法似乎也适用于其他压缩类型)。
Secondly the AESZipFile() method apparently accepts a BytesIO object as well as a file path, presumably because this is what it sees when it opens the file.其次 AESZipFile() 方法显然接受一个 BytesIO 对象以及一个文件路径,大概是因为这是它打开文件时看到的。 Therefore the zip file can be extracted in situ without having to download it first.
因此,无需先下载即可原位提取 zip 文件。
This method creates the pyzipper object which you can then read by specifying the file name and the password.此方法创建 pyzipper 对象,然后您可以通过指定文件名和密码来读取该对象。
The final code looks like this:-最终代码如下所示:-
import pyzipper
import boto3
from io import BytesIO
my_bucket = ''
my_folder = ''
my_zip = ''
my_password = b''
aws_access_key_id=''
aws_secret_access_key=''
s3 = boto3.client('s3', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
s3_file = s3.get_object(Bucket=my_bucket, Key=my_folder + my_zip)
s3_iodata = BytesIO(s3_file['Body'].read())
f = pyzipper.AESZipFile(s3_iodata)
my_file = f.namelist()[0]
file_content = f.read(my_file, pwd = my_password)
response = s3.put_object(Body=file_content, Bucket=my_bucket, Key=my_folder + my_file)
Here is an article that was useful:-这是一篇有用的文章:-
https://www.linkedin.com/pulse/extract-files-from-zip-archives-in-situ-aws-s3-using-python-tom-reid https://www.linkedin.com/pulse/extract-files-from-zip-archives-in-situ-aws-s3-using-python-tom-reid
I hope this is helpful to someone,我希望这对某人有帮助
Phil菲尔
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.