如何使用Python请求在Amazon S3上执行“PUT”

如何使用Python请求在Amazon S3上执行“PUT”,第1张

概述我正在尝试使用 Python请求将文件上传到Amazon S3(Python是v2.7.9,请求是v2.7).遵循完美运行的curl命令: curl --request PUT --upload-file img.png https://mybucket-dev.s3.amazonaws.com/6b89e187-26fa-11e5-a04f-a45e60d45b53?Signature=Ow%3 我正在尝试使用 Python请求将文件上传到Amazon S3(Python是v2.7.9,请求是v2.7).遵循完美运行的curl命令:

curl --request PUT --upload-file img.png https://mybucket-dev.s3.amazonaws.com/6b89e187-26fa-11e5-a04f-a45e60d45b53?Signature=Ow%3D&Expires=1436595966&AWSAccessKeyID=AQ

但是当我对请求做同样的事情时,它就失败了.这是我尝试过的:

url = https://mybucket-dev.s3.amazonaws.com/6b89e187-26fa-11e5-a04f-a45e60d45b53?Signature=Ow%3D&Expires=1436595966&AWSAccessKeyID=AQrequests.put(url,files={'file': base64_encoded_image})requests.put(url,files={'upload_file': base64_encoded_image})

它失败了403,我得到的回应是:

<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provIDed. Check your key and signing method.</Message>

然后我以详细模式运行curl:

* Hostname was NOT found in DNS cache*   Trying 54.231.168.134...* Connected to mybucket-dev.s3.amazonaws.com (54.231.168.134) port 443 (#0)* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA* Server certificate: *.s3.amazonaws.com* Server certificate: VeriSign Class 3 Secure Server CA - G3* Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5> PUT /6b89e187-26fa-11e5-a04f-a45e60d45b53?Signature=Ow%3D&Expires=1436595966&AWSAccessKeyID=AQ http/1.1> User-Agent: curl/7.37.1> Host: mybucket-dev.s3.amazonaws.com> Accept: */*> Content-Length: 52369> Expect: 100-continue>< http/1.1 100 Continue* We are completely uploaded and fine< http/1.1 200 OK< x-amz-ID-2: 5lLCQ3FVrTBg2vkyk44E+MecQJb2OGiloO0+2pKePtxPgZptKECNlUyYN43sl4LBNe9f8IDh/cc=< x-amz-request-ID: 636A24D53DEB5215< Date: Fri,10 Jul 2015 12:04:44 GMT< ETag: "5802130d4320b56a72afe720e2c323a7"< Content-Length: 0* Server AmazonS3 is not blackListed< Server: AmazonS3<* Connection #0 to host mybucket-dev.s3.amazonaws.com left intact

所以我添加了标题

headers = {'Content-Length': '52369','Host': 'mybucket-dev.s3.amazonaws.com','Expect': '100-continue','Accept': '*/*','User-Agent': 'curl/7.37.1'}requests.put(url,files={'file': base64_encoded_image},headers=headers)

我尝试了不同的标题组合,仍然会抛出相同的错误.然后我也尝试发送查询参数:

payload={'Expires': '1436595966','AWSAccessKeyID': 'AQ','Signature': 'Ow%3D'}requests.put(url,headers=headers,data=payload)

它仍然失败并且同样的错误.我尝试了没有查询参数的URL,并将它们作为data = payload发送到请求,它失败并出现相同的错误.

解决方法 requests工程师帮助我:

with open('img.png','rb') as data:    requests.put(url,data=data)
总结

以上是内存溢出为你收集整理的如何使用Python请求在Amazon S3上执行“PUT”全部内容,希望文章能够帮你解决如何使用Python请求在Amazon S3上执行“PUT”所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1193418.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存