S3存储桶CORS配置:
[
{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"GET",
"HEAD",
"PUT",
"POST",
"DELETE"
],
"AllowedOrigins": [
"*",
"https://performance.mobilads.co"
],
"ExposeHeaders": [
"x-amz-server-side-encryption",
"x-amz-request-id",
"x-amz-id-2",
"ETag"
],
"MaxAgeSeconds": 3000
}
]
我的下载图片代码:
downloadImages = (s3Url) => {
const urlParts = s3Url.split('/');
const imageName = urlParts[urlParts.length - 1];
fetch(s3Url, {
method: 'GET'
})
.then(res => {
if (!res.ok) {
throw new Error(`Failed to fetch: ${res.status} ${res.statusText}`);
}
return res.blob();
})
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = imageName;
document.body.appendChild(a);
a.click();
setTimeout(
_ => { window.URL.revokeObjectURL(url); },
60000);
a.remove();
})
.catch(err => {
console.error('Error fetching image: ', err);
});
}
错误:
CORS策略阻止了在‘https://mobilads-maps-hosting.s3.amazonaws.com/public/prod/campaign-shoots/204/ll9mqhj3-1066461b5705cbd2a4a074dfa7df12e9.jpg’从源http://localhost:3001‘获取的访问:请求的资源上不存在"Access-Control-Allow-Origin"标头.如果一个不透明的响应满足您的需求,请将请求的模式设置为‘no-CORS’,以便在禁用CORS的情况下获取资源. Network-badcrums.js:134获取https://mobilads-maps-hosting.s3.amazonaws.com/public/prod/campaign-shoots/204/ll9mqhj3-1066461b5705cbd2a4a074dfa7df12e9.jpg Net::Err_FAILED
我try 将CORS策略更改为允许来源中的特定网站URL和开发URL.仍然不起作用.