javascript - 使用javascript上传照片时,aws s3返回禁止的403
问题描述
我有一个查询,我正在将文件上传到 aws s3 服务器,但它返回错误我还向 aws s3 服务器添加了核心策略,但它仍然返回我尝试过的错误。
**Html code**
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.410.0.min.js"></script>
<input type="file" id="photoupload">
<button onclick="addPhoto()">Submit</button>
**Javascript Code**
<script type="text/javascript">
var albumBucketName = "bucketname";
var bucketRegion = "region";
var IdentityPoolId = "mypoolid";
AWS.config.update({
region: bucketRegion,
credentials: new AWS.CognitoIdentityCredentials({
IdentityPoolId: IdentityPoolId
})
});
var s3 = new AWS.S3({
apiVersion: "2006-03-01",
params: { Bucket: albumBucketName }
});
function addPhoto() {
var files = document.getElementById("photoupload").files;
if (!files.length) {
return alert("Please choose a file to upload first.");
}
var file = files[0];
var fileName = file.name;
// var albumPhotosKey = encodeURIComponent(albumName) + "/";
var filepath = '/' + fileName;
// var photoKey = albumPhotosKey + fileName;
debugger
// Use S3 ManagedUpload class as it supports multipart uploads
var upload = new AWS.S3.ManagedUpload({
params: {
Bucket: albumBucketName,
Key: filepath,
Body: file,
ACL: 'public-read'
}
});
var promise = upload.promise();
promise.then(
function (data) {
alert("Successfully uploaded photo.");
// viewAlbum(albumName);
},
function (err) {
return alert("There was an error uploading your photo: ", err.message);
}
);
}
</script>
**Error I am getting**
加载资源失败:服务器响应状态为 403(禁止)
解决方案
推荐阅读
- python - 遍历查询的复制结果的最佳方法是什么?
- javascript - How to check empty in variable in Google Apps Script
- assembly - 程序集的数值变量修改了前一个
- vbscript - .vbs 用 Sapi 告诉时间,但不中断给定一些打开的进程
- reactjs - React Typescript with Redux - 未处理的拒绝(TypeError):无法在已撤销的代理上执行“设置”
- javascript - 根据内部数组重复重组对象
- python - 在 Windows 10 上安装了 Anaconda,但无法运行任何 .exe 文件。几秒后消失
- junit - 春季WebFlux;Mono.map() 中抛出的单元测试异常
- qt - 从应用程序的不同点调用具有相同参数的 positionViewAtIndex 会提供不同的结果
- android - 在 .xml 中使用数据绑定时将 rootView 传递给 ViewModel 是一种好习惯吗?