我一直在努力,但我无法解决它。它在控制台中出现了这个错误。
XMLHttpRequest cannot load https://s3-us-east-2.amazonaws.com/My-bucket. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
这是我在Angular2上的代码
imageUploadToS3: {
bucket: 'flagfrog-article-cloud',
region: 's3-us-east-2',
keyStart: 'article-image/',
params: {
acl: 'public-read', // ACL according to Amazon Documentation.
AWSAccessKeyId: 'AKIAJEMDZVXVLMPDNA4A', // Access Key from Amazon.
policy: 'Policy1498724219240', // Policy string computed in the backend.
signature: 'flagfrogweb', // Signature computed in the backend.
}
这是我在Amazon S3上的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>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>
`
我在这里遗漏了什么吗?请帮忙
答案 0 :(得分:1)
亚马逊不允许使用通配符。您的CORS配置应该是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>http://localhost:4200</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>