我正在尝试通过Django将文件从s3下载到客户端。为此,Ive从所需的s3对象生成了一个公共URL,Im试图将用户重定向到该URL,因此将该文件下载到他/她的计算机上。代码如下
def generate_public_url(self, bucket, file)
url = ''
filename = file
client = boto3.client(
's3',
)
url = client.generate_presigned_url(
ClientMethod ='get_object',
ExpiresIn = 7200,
Params = {
'Bucket': bucket,
'Key': file,
}
)
return url
重定向网址的代码:
from .django.http import HttpResponseRedirect
.
.
.
http_response_redirect = HttpResponseRedirect(url)
return http_response_redirect
aws的响应是403,出现以下错误:
代码:SignatureDoesNotMatch
消息:我们计算出的请求签名与您提供的签名不匹配。检查您的密钥和签名方法。
经过几天的研究并尝试了许多事情之后,我确定这是django的问题,并且在github上阅读了一些帖子后,人们说如果标题与用于生成的标题相比有所不同公共网址会生成此错误。因此,以django提出请求的方式必须有所不同,但我不知道会是什么。
还可以直接在浏览器中使用url来下载文件,也可以使用POSTMAN,并且也可以在flask上正常运行,并且应用重定向并下载文件。
这与django有关,请确保标头。这是使用django和flask的wireshark捕获的http。
DJANGO
GET /table_video/descarga/?csrfmiddlewaretoken=%7B%7B+csrf_token+%7D%7D
HTTP/1.1
Host: 127.0.0.1:8000
Connection: keep-alive
Accept: */*
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
Content-Type: application/json
Referer: http://127.0.0.1:8000/table_video/
Accept-Encoding: gzip, deflate, br
Accept-Language: es-ES,es;q=0.9
Cookie: csrftoken=j0VucAJTbmoYNhabs5HSY1qLnj3KSstGrga35y1lrn1zvIX5XhGyjLTcbvc347I6; sessionid=n5wmcy27utko9uwj53brb62jhy4i4gv8
HTTP/1.1 302 Found
Date: Mon, 17 Jun 2019 13:53:04 GMT
Server: WSGIServer/0.2 CPython/3.7.3
Content-Type: text/html; charset=utf-8
Location: https://django-static-videos.s3.amazonaws.com/test_file.txt?AWSAccessKeyId=***************&Signature=w9a2G0J8puFpwPtRIzEXYL0XF0Y%3D&Expires=1560786784
Content-Length: 0
烧瓶
GET / HTTP/1.1
Host: 127.0.0.1:8000
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate, br
Accept-Language: es-ES,es;q=0.9
Cookie: csrftoken=j0VucAJTbmoYNhabs5HSY1qLnj3KSstGrga35y1lrn1zvIX5XhGyjLTcbvc347I6; sessionid=n5wmcy27utko9uwj53brb62jhy4i4gv8
HTTP/1.0 302 FOUND
Content-Type: text/html; charset=utf-8
Content-Length: 533
Location: https://django-static-videos.s3.amazonaws.com/test_file.txt?AWSAccessKeyId=***************&Signature=RIOzX36RfPkHmhGl4STBYTADzdI%3D&Expires=1560784567
Server: Werkzeug/0.15.4 Python/3.7.3
Date: Mon, 17 Jun 2019 13:55:30 GMT
请求上是否有不同的内容,导致来自django的请求在AWS服务器中失败?也许请求标头的顺序可能会使它失败?如果是这样,python中是否有任何库可以直接创建http请求而不是使用django?
谢谢