使用Cognito令牌访问S3时获得“ AccessDenied”

时间:2018-11-12 10:57:05

标签: amazon-web-services amazon-s3 amazon-cognito

我正在遵循以下教程,以允许基于浏览器的上传到我的S3存储桶。

https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album.html

我完成了整个教程,但是当我从localhost提交照片时,我得到403的访问被拒绝,其中acl设置为public-read400 acl设置为public

我的IAM政策如下所示

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

我正在使用未经身份验证的匿名用户。

cognito控制台正在显示unauthenticated个用户,但我也不知道为什么。

我用来上传的功能如下

function uploadPDF() {

    var file = $('#fileUpload').val();
    var fileName = Date.now().toString() + ".pdf";
    var albumPhotosKey = encodeURIComponent("files") + '/';

    var photoKey = albumPhotosKey + fileName;
    s3.upload({
        Key: photoKey,
        Body: file,
        ACL: 'public'
    }, function(err, data) {
        if (err) {
            console.log(err);
            return alert('There was an error uploading your photo: ', err.message);
        }
        alert('Successfully uploaded photo.');
    });
}

并且我已经按如下所示设置了AWS SDK

    AWS.config.update({
            region: 'ap-south-1',
            credentials: new AWS.CognitoIdentityCredentials({
                IdentityPoolId: 'ap-south-1:IdentityPoolKey'
            })
        });

        var s3 = new AWS.S3({
            apiVersion: '2006-03-01',
            params: {Bucket: albumBucketName}
        });

基于this的答案,我尝试将IdentityPoolId更改为us-east-1,但未显示任何更改。

0 个答案:

没有答案