Python 蟒蛇3+;jQuery文件上载直接上载s3 |方法不允许405
我正在使用Python3+Flask将视频文件直接上传到S3存储器,但我得到了Python 蟒蛇3+;jQuery文件上载直接上载s3 |方法不允许405,python,amazon-s3,flask,jquery-file-upload,Python,Amazon S3,Flask,Jquery File Upload,我正在使用Python3+Flask将视频文件直接上传到S3存储器,但我得到了方法不允许405 此外,上传必须工作客户端,因为视频文件没有大小限制,将是巨大的。这将导致服务器超时 以下是我的CORS和桶政策: <?xml version="1.0" encoding="UTF-8"?> <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <CORSRule> <A
方法不允许405
此外,上传必须工作客户端,因为视频文件没有大小限制,将是巨大的。这将导致服务器超时
以下是我的CORS和桶政策:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AddPerm",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::mybucket/*"
}
]
}
在服务器端,我使用它来签署请求:
# Sign request for direct file upload through client for video
@app.route('/sign_s3/<path:file_name_data>/<path:file_type_data>/<up_type>', methods=["GET", "POST"])
@login_required
@check_confirmed
def sign_s3(file_name_data, file_type_data, up_type):
if "localhost" in request.url_root:
if up_type == "profile_vid":
file_name = str(current_user.id) + get_random_code(5) + "local-profil-video." + file_name_data.split(".")[-1]
else:
file_name = str(current_user.id) + str(randint(1,100)) + "local-post-video-temp." + file_name_data.split(".")[-1]
else:
if up_type == "profile_vid":
file_name = str(current_user.id) + get_random_code(5) + "-profil-video." + file_name_data.split(".")[-1]
else:
file_name = str(current_user.id) + str(randint(1,100)) + "-post-video-temp." + file_name_data.split(".")[-1]
file_type = file_type_data
session = boto3.Session(
aws_access_key_id=app.config['MY_AWS_ID'],
aws_secret_access_key=app.config['MY_AWS_SECRET'],
region_name='eu-central-1'
)
presigned_post = s3.generate_presigned_post(
Bucket = 'mybucket',
Key = 'videos/' + file_name,
Fields = {"acl": "public-read", "Content-Type": file_type},
Conditions = [
{"acl": "public-read"},
{"Content-Type": file_type}
],
ExpiresIn = 3600
)
if up_type == "profile_vid":
if current_user.profile_video != None:
delete_file_from_aws("videos/", current_user.profile_video)
setattr(current_user, "profile_video", file_name)
else:
print ('post video has been uploaded, no need to delete or set here')
db_session.commit()
return json.dumps({'data': presigned_post, 'url': 'https://s3.eu-central-1.amazonaws.com/mybucket/' + 'videos/' + file_name, 'created_file_name' : file_name})
响应标题:
Access-Control-Allow-Methods: GET, POST, PUT
Access-Control-Allow-Origin: *
Allow: HEAD, DELETE, GET, PUT
Connection: close
Content-Type: application/xml
Date: Wed, 23 May 2018 15:18:49 GMT
Server: AmazonS3
Transfer-Encoding: chunked
Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
x-amz-id-2: SOME DATA
x-amz-request-id: MY ID
请求标头:
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
Cache-Control: max-age=0
Connection: keep-alive
Content-Length: 1057282
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary5gUAipNLOHxSqN8F
Host: s3.eu-central-1.amazonaws.com
Origin: http://localhost:5000
Referer: http://localhost:5000/profile
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
你看过报纸了吗?我不确定您配置的会话对象是如何工作的,但我发现使用S3资源和
upload\u fileobj
上传到S3 bucket更容易。您可以从发布的文件中分块写入视频数据,而无需将其临时写入服务器,但这不是客户端上载?巨大的文件将导致服务器超时,还是我错了@斯卡米看到了。你能更新你的问题,包括你想要客户端上传吗?从你的帖子上看不太清楚。谢谢你的提示。
Request URL: https://s3.eu-central-1.amazonaws.com/mybucket/videos/3rBHwNlocal-profil-video.mp4
Request Method: POST
Status Code: 405 Method Not Allowed
Remote Address: 52.219.74.48:443
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Methods: GET, POST, PUT
Access-Control-Allow-Origin: *
Allow: HEAD, DELETE, GET, PUT
Connection: close
Content-Type: application/xml
Date: Wed, 23 May 2018 15:18:49 GMT
Server: AmazonS3
Transfer-Encoding: chunked
Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
x-amz-id-2: SOME DATA
x-amz-request-id: MY ID
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
Cache-Control: max-age=0
Connection: keep-alive
Content-Length: 1057282
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary5gUAipNLOHxSqN8F
Host: s3.eu-central-1.amazonaws.com
Origin: http://localhost:5000
Referer: http://localhost:5000/profile
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36