无法从AWS获取AWS存储桶列表

时间:2019-06-01 06:47:40

标签: javascript amazon-web-services amazon-s3

出现以下错误: 从源'null'对'https://s3.amazonaws.com/'处XMLHttpRequest的访问已被CORS策略阻止:对预检请求的响应未通过访问控制检查:邮件头上没有'Access-Control-Allow-Origin'标头请求的资源。

我还添加了s3 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>
    <ExposeHeader>ETag</ExposeHeader>
    <ExposeHeader>x-amz-meta-custom-header</ExposeHeader>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

仍然无法获取s3存储桶。使用以下代码:

  var bucket = new AWS.S3({
            params: {
                Bucket: bucketName
            }
        });

console.log(bucket);
var params = {};
bucket.listBuckets(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data); });

0 个答案:

没有答案