【问题标题】:AWS S3 bucket: CORS ConfigurationAWS S3 存储桶:CORS 配置
【发布时间】:2017-09-21 12:40:48
【问题描述】:

我正在尝试从 S3 下载图像。但我得到了错误 CORS。

我做了什么:

-为 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>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>HEAD</AllowedMethod>
<ExposeHeader>Accept-Ranges</ExposeHeader>
<ExposeHeader>Content-Range</ExposeHeader>
<ExposeHeader>Content-Encoding</ExposeHeader>
<ExposeHeader>Content-Length</ExposeHeader>
<ExposeHeader>Access-Control-Allow-Origin</ExposeHeader>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

-我使用 AWS 控制台上传了一张图片

-在 html 中加载图像:

<div id="my-node">
   <img id="image" src="path/to/s3/bucket/image.png" class="img-responsive"/>
</div>

-下载图片,我用https://github.com/tsayen/dom-to-image下载

domtoimage.toBlob(document.getElementById('my-node'))
.then(function (blob) {
    window.saveAs(blob, 'my-node.png');
});

结果: 我收到错误

XMLHttpRequest 无法加载 path/to/s3/bucket/image.png。不 请求中存在“Access-Control-Allow-Origin”标头 资源。

我检查了图像,响应标头不包括 Access-Control-Allow-Origin(似乎 S3 响应中没有包含 CORS 配置)

Accept-Ranges:bytes
Content-Length:124824
Content-Type:image/png
Date:Mon, 24 Apr 2017 17:27:48 GMT
ETag:"xxxxxxxx00000000"
Last-Modified:Mon, 24 Apr 2017 17:18:53 GMT
Server:AmazonS3
x-amz-id-2:xxxxxxxxxxxxxxxxx
x-amz-request-id:xxxxxxxxxxxx

非常感谢任何建议或建议

【问题讨论】:

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


【解决方案1】:

您的存储桶策略需要至少将s3:GetBucketCORS 授予公共用户,例如:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:GetBucketCORS"
            ],
            "Resource": [
                "arn:aws:s3:::mybucketname"
            ]
        }
    ]
}

【讨论】:

    猜你喜欢
    • 2015-01-08
    • 2021-12-28
    • 2017-01-30
    • 1970-01-01
    • 2015-12-07
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    相关资源
    最近更新 更多