gpt4 book ai didi

python - 使用 Python urllib2 将 zip 文件直接上传到 AWS S3

转载 作者:太空宇宙 更新时间:2023-11-03 17:56:59 26 4
gpt4 key购买 nike

我尝试使用 Python 脚本将 zip 文件直接上传到 S3,但遇到一些 Unicode 解码错误。

我所做的是生成预签名的 S3 链接,然后将数据上传到其中。我知道该链接工作正常,因为当我使用curl 执行以下操作时上传工作正常:

curl -v -H "Content-Type: application/zip" -T /Path/To/Local/File.zip https://MySignedAWSS3Link

但是,当我使用下面的代码在 Python 中尝试执行此操作时,出现错误。

infile2 = open('/Path/To/Local/File.zip', 'rb')
filedata2 = infile2.read()
request2 = urllib2.Request("https://MySignedAWSS3Link",data=filedata2)
request2.add_header('Content-Type', 'application/zip')
request2.get_method = lambda: 'PUT'
url2 = opener.open(request2)

我在 Python 中收到以下错误/回溯:

> Traceback (most recent call last):
File "putFiles.py", line 44, in <module>
url2 = opener.open(request2)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 404, in open
response = self._open(req, data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 422, in _open
'_open', req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1222, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1181, in do_open
h.request(req.get_method(), req.get_selector(), req.data, headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 973, in request
self._send_request(method, url, body, headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1007, in _send_request
self.endheaders(body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 969, in endheaders
self._send_output(message_body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 827, in _send_output
msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0xca in position 10: ordinal not in range(128)

我在这里做错了什么?

最佳答案

根据您的回溯,添加到您的请求中的一些字符串是 unicode。但是,根据您的脚本示例,所有字符串都是 ascii 编码的(因为您使用的是 Python2.7)。

奇怪的事情会发生在你身上,除非你将 Python 默认编码设置为 UTF-8(或者操作系统为你设置)。

我希望这对您有用(将所有字符串转换为 ascii):

infile2 = open('/Path/To/Local/File.zip', 'rb')
filedata2 = infile2.read()
request2 = urllib2.Request("https://MySignedAWSS3Link".encode('utf-8'),data=filedata2)
request2.add_header(str('Content-Type'), str('application/zip'))
request2.get_method = lambda: str('PUT')
url2 = opener.open(request2)

更新:This q/a也可能对你有帮助

关于python - 使用 Python urllib2 将 zip 文件直接上传到 AWS S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28329178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com