1
votes

I have a bucket on s3 with objects in it that my website is meant to be able to delete via the aws javascript sdk. For reference, my website is hosted on s3 in the us-east-1 region and my bucket with objects in it that need to be deleted via the website is in us-west-1.

I have tried the following code on my website:

var bucketInstance = new AWS.S3({region: 'us-west-1'});
var params = {
    Bucket: 'MyBucketName',
    Key: 'photoToDelete.JPG'
};
bucketInstance.deleteObject(params, function (err, data) {
    if (data) {
        console.log("Photo deleted successfully");         
    }
    else {
        console.log("Check if you have sufficient permissions : "+err);                                
    }
});

But it does not successed - the following errors appear in the console:

1) XMLHttpRequest cannot load https://MyBucketName.s3-us-west-1.amazonaws.com/photoToDelete.JPG. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://MyDomain.com' is therefore not allowed access. The response had HTTP status code 403.

2) aws-sdk.js:2122 OPTIONS https://MyBucketName.s3.amazonaws.com/?max-keys=0 403 (Forbidden)

3) XMLHttpRequest cannot load https://MyBucketName.s3.amazonaws.com/?max-keys=0. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://MyDomain.com' is therefore not allowed access. The response had HTTP status code 403.

And these errors seem to repeat a bit in the console.

What is going on?

For reference earlier in my code I config the AWS SDK like so:

AWS.config.region = 'us-east-1'; // 1. Enter your region

    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: 'us-east-1:*MyIdentityPoolId*' 
    });

I figured these may be causing an issue as the s3 bucket the objects are in that need to be deleted are in west 1 but my website and all the lambda function and api gateway I have set up is in east-1 so naturally I configured my AWS.config.region and credentials to east values. I'm not sure if this may be causing this problem though. I am explicitly setting the s3 region to west 1 and It is not possible to create an IdentityPool in us-west-1 so I am skeptical these config values are causing the error, but they may be.

Finally, the s3 bucket in us-west-1 that has the objects that need to be deleted has the following bucket policy:

{
    "Version": "2012-10-17",
    "Id": "Policy2354434234",
    "Statement": [
        {
            "Sid": "MakeItPublic",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:GetObject",
                "s3:DeleteObject",
                "s3:PutObject"
            ],
            "Resource": "arn:aws:s3:::MyBucketName/*"
        }
    ]
}

And I set the CORS configuration like so:

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

I also set READ and WRITE permissions for everyone on the bucket. Any idea why I am getting this very frustrating error??

1
It looks like the CORS config is where things are going wrong. Can you try changing the allowed origin to * briefly to retest?c3st7n
Thanks for the suggestion - I changed origin to * and the same result occured!sometimesiwritecode
Seriously grasping at straws here, but all the examples/samples I can find do not specify the xmlns or the version/encoding info you have.c3st7n

1 Answers

0
votes

the thing you need to add your domain the same way it shows in the error & the AllowedHeader wiled card

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