javascript AWS S3 - CORS OPTIONS 预检在 DELETE w/VersionId 期间抛出 400 错误请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22186347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:39:18  来源:igfitidea点击:

AWS S3 - CORS OPTIONS Preflight throwing 400 Bad Request during DELETE w/ VersionId

javascriptamazon-web-servicesamazon-s3xmlhttprequestcors

提问by ThievingSix

I am attempting a deleteObject request for a delete marker using the Key of the object and the VersionID of the delete marker.

我正在尝试使用对象的键和删除标记的 VersionID 对删除标记发出 deleteObject 请求。

Because of CORS, the browser (Chrome 34.0.1847.11) sends an OPTIONS preflight request to: http://bucket.s3-us-west-2.amazonaws.com/Folder/File.ext?versionId=0123456789

由于 CORS,浏览器 (Chrome 34.0.1847.11) 将 OPTIONS 预检请求发送至:http://bucket.s3-us-west-2.amazonaws.com/Folder/File.ext?versionId=0123456789

Amazon S3 responds with 400 (Bad Request) with the following XML body:

Amazon S3 使用以下 XML 正文以 400(错误请求)进行响应:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>InvalidArgument</Code>
    <Message>This operation does not accept a version-id.</Message>
    <ArgumentValue>0123456789</ArgumentValue>
    <ArgumentName>versionId</ArgumentName>
    <RequestId>12345</RequestId>
    <HostId>1122334455</HostId>
</Error>

Because the XMLHttpRequest returns 400 (Bad Request), the DELETE request never gets executed. I am under the impression that AWS isn't handling the options request correctly. If there is a workaround, that would be great!

因为 XMLHttpRequest 返回 400(错误请求),所以 DELETE 请求永远不会被执行。我的印象是 AWS 没有正确处理选项请求。如果有解决方法,那就太好了!

My current CORS policy on the bucket is:

我目前对存储桶的 CORS 政策是:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>HEAD</AllowedMethod>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>DELETE</AllowedMethod>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

FYI: I am using the AWS SDK for JS 2.0.0-rc10

仅供参考:我正在使用适用于 JS 2.0.0-rc10 的 AWS 开发工具包

Thank you in advance.

先感谢您。

EDIT 1: I tried adding <AllowedMethod>OPTIONS</AllowedMethod>but Amazon returns Found unsupported HTTP method in CORS config. Unsupported method is OPTIONS

编辑 1:我尝试添加<AllowedMethod>OPTIONS</AllowedMethod>但亚马逊返回Found unsupported HTTP method in CORS config. Unsupported method is OPTIONS

EDIT 2:

编辑 2

OPTIONS request/response headers:

OPTIONS 请求/响应标头:

Remote Address: *********:443
Request URL: https://bucket.s3-us-west-2.amazonaws.com/path/to/file_name?versionId=0123456789
Request Method: OPTIONS
Status Code: 400 Bad Request

Request Headers
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Access-Control-Request-Headers: x-amz-user-agent, x-amz-security-token, x-amz-date, authorization, content-type
Access-Control-Request-Method: DELETE
Cache-Control: no-cache
Connection: keep-alive
DNT: 1
Host: bucket.s3-us-west-2.amazonaws.com
Origin: https://website.com
Pragma: no-cache
Referer: https://website.com/
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.60 Safari/537.36
Query String Parameters
versionId: 0123456789

Response Headers
Access-Control-Allow-Headers: x-amz-user-agent, x-amz-security-token, x-amz-date, authorization, content-type
Access-Control-Allow-Methods: HEAD, GET, PUT, POST, DELETE
Access-Control-Allow-Origin: *
Connection: close
Content-Type: application/xml
Date: Tue, 18 Mar 2014 23:59:15 GMT
Server: AmazonS3
Transfer-Encoding: chunked
Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
x-amz-id-2: *************************
x-amz-request-id: ***********

The delete request doesn't ever actually happen because the OPTIONS fails.

删除请求实际上从未发生,因为 OPTIONS 失败。

回答by tmont

I just ran into this problem. It only occurs on Chrome. It was pretty awesome.

我刚刚遇到了这个问题。它只发生在 Chrome 上。真是太棒了。

The solution is to add the following to your relevant <CORSRule>configuration in AWS:

解决方案是将以下内容添加到您<CORSRule>在 AWS 中的相关配置中:

<AllowedHeader>*</AllowedHeader>

That makes Chrome NOT send the OPTIONS request, and everything should work properly.

这使得 Chrome 不发送 OPTIONS 请求,一切都应该正常工作。