AWS.config.update({
region: bucketRegion,
credentials: new AWS.CognitoIdentityCredentials({
IdentityPoolId: IdentityPoolId
})
});
function upload_image(id){
var files = document.getElementById(id).files;
if (!files.length) {
return alert("Please choose a file to upload first.");
}
var file = files[0];
var fileName = file.name;
var albumPhotosKey = encodeURIComponent(albumBucketName) + "//";
var photoKey = albumPhotosKey + fileName;
// Use S3 ManagedUpload class as it supports multipart uploads
var upload = new AWS.S3.ManagedUpload({
params: {
Bucket: albumBucketName,
Key: photoKey,
Body: file,
ACL: "public-read"
}
});
var promise = upload.promise();
promise.then(
function(data) {
alert("Successfully uploaded photo.");
viewAlbum(albumName);
},
function(err) {
console.log(err)
return alert("There was an error uploading your photo: ", err.message);
}
);}
当通过JS将图像上传到S3存储桶时,该消息显示访问错误被拒绝。在浏览器控制台错误([HTTP / 1.1 403 Forbidden 2156ms])。所有存储桶设置都是公开的。**
尝试将其添加到策略中的存储桶名称->权限-> CORS配置中的CORS规则中
<CORSRule>
<AllowedOrigin>localhost:3000</AllowedOrigin>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<AllowedMethod>HEAD</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>