4

Getting "403 Forbidden Access Denied Error" on file upload to s3 bucket using JavaScript

I'm trying to establish a connection to an s3 bucket and upload an image file. I am getting a permission error. Please, let me know where I am doing it wrong. I followed this http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album.html

Error:

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

Here is my code,

 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>
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133

0 Answers0