我正在尝试将文件图像上传到我的Amazon s3存储桶,但仍然收到400 Bad request错误。在挖掘我发现错误代码是无效参数但我不知道为什么我一直得到错误。
controller.js
Upload.upload({
url: url,
method: 'POST',
fields : {
key: imageURI.name,
AWSAccessKeyId: key,
acl: 'public-read',
policy: policy,
signature: signature,
"Content-Type": imageURI.type != '' ? imageURI.type : 'application/octet-stream',
filename: imageURI.name
},
file: imageURI,
}).success(function(data, status, headers, config) {
console.log(data);
console.log(headers);
}).error(function (data, status, headers, config) {
});
cors配置
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>http://localhost:8100</AllowedOrigin>
<AllowedOrigin>http://staging.com.my</AllowedOrigin>
<AllowedOrigin>http://partners.com.my</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
这是我得到的错误报告
<Error>
<Code>InvalidArgument</Code>
<Message>Authorization header is invalid -- one and only one ' ' (space) required</Message>
<ArgumentName> Authorization</ArgumentName>
<ArgumentValue>YrRHhyr9uDF5SYntzyR3</ArgumentValue>
<RequestId>94E78E47B15BE7C5</RequestId>
<HostId>eR/Smasry6u1tE5b3DfrTLSGve3y4a/dZlYMLnBjcC2YhjUzskLzu3q85SYdfb9q0Ii09HCWcSI=</HostId>
</Error>
感谢任何帮助。
谢谢
答案 0 :(得分:3)
@ Katana24试试这个。您的标头中可能会发送授权值,因此您必须删除它。有时这样做之后它仍然会给你一个错误,然后你把它分配给undefined。希望这对你有用,并标记为答案。干杯
Upload.upload({
url: "https://partners-mobile-app.s3.amazonaws.com/", //S3 upload url including bucket name
method: 'POST',
transformRequest: function (data, headersGetter) {
//Headers change here
var headers = headersGetter();
delete headers['Authorization'];
return data;
},
headers: {
'Authorization': undefined
},
fields : {
key: imageURI.name,
AWSAccessKeyId: key,
acl: 'public-read',
policy: policy,
signature: signature,
"Content-Type": imageURI.type != '' ? imageURI.type : 'application/octet-stream',
filename: imageURI.name
},
file: imageURI,
}).success(function(data, status, headers, config) {
console.log(data);
console.log(headers);
}).error(function (data, status, headers, config) {
});
答案 1 :(得分:0)
S3 Amazon文件夹中的CORS配置。
请检查并查看标题中发送给S3的内容, 你发送了&#34; Content-Type&#34;在你的标题中。包括在cors配置中。检查这个样本。
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
<AllowedHeader>Content-Type</AllowedHeader>
</CORSRule>
</CORSConfiguration>