使用JavaScript将文件上传到s3存储桶时出现“403 Forbidden Access Denied Error”

时间:2017-12-29 06:23:20

标签: javascript amazon-web-services amazon-s3 http-status-code-403 image-upload

使用JavaScript

将文件上传到s3存储桶时出现“403 Forbidden Access Denied Error”

我正在尝试建立与s3存储桶的连接并上传图像文件。我收到了一个权限错误。请让我知道我做错了什么。我遵循了http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album.html

错误:

<Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>C1D4D7DBB7797DD8</RequestId><HostId>s1ThVeqju5MLEyuPNoSxppDTge2VvcHgsU7xdsWgBDTchJR1YJlllCzgwJY4NTJsPOeAJ+46jpk=</HostId></Error>

这是我的代码,

 var albumBucketName = '**';
  var bucketRegion = 'us-east-2';
  var IdentityPoolId = '**';

  AWS.config.update({
    region: bucketRegion,
    credentials: new AWS.CognitoIdentityCredentials({
      IdentityPoolId: IdentityPoolId
    })
  });


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

 addPhoto('albumname')    

  function addPhoto(albumName) {
    var files = 'sf';
    if (!files.length) {
      return alert('Please choose a file to upload first.');
    }
    var file = '*/firebase-logo.png';
    var fileName = 'firebase-logo.png'
    var albumPhotosKey = encodeURIComponent(albumName) + '/';

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


Bucket Policy

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::[bucket_name]/*"
        }
    ]
}


Role Policy

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


Cors Config

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedMethod>DELETE</AllowedMethod>
        <AllowedMethod>HEAD</AllowedMethod>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

0 个答案:

没有答案