首页 > 解决方案 > 使用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(禁止)

标签: javascriptamazon-web-servicesamazon-s3

解决方案


推荐阅读