Marc Fletcher
Marc Fletcher

Reputation: 1072

CORS error with S3 even after adding CORS configuration

I am trying to upload images to an S3 bucket via javaScript. When submitting the request, the following error returns:

XMLHttpRequest cannot load https://somebucket.s3-us-west 
2.amazonaws.com/albums//apicture.png. Response to preflight 
request doesn't pass access control check: No 'Access-Control-Allow-Origin'
header is present on the requested resource. 
Origin 'http://someorigin:3000' is therefore not allowed access.

My CORS configuration on the bucket:

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

I even manually added the origin to the policy as you can see, along with *, but nothing seems to be working, and I'm not sure where to begin.

I examined the network pane in the Chrome Devtools and noticed that one of the XHR was apicture.png, and it had the following key information:

Request URL:https://somebucket.s3-us-west-2.amazonaws.com/pics/apicture.png
Request Method:OPTIONS
Status Code:301 Moved Permanently

The response tab had some other important information:

<Error>
<Code>PermanentRedirect</Code>
<Message>The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.</Message>
<Bucket>somebucket</Bucket>
<Endpoint>somebucket.s3-us-west-1.amazonaws.com</Endpoint>
<RequestId>
***************
</RequestId>
</Error>

So, it is clear that they want the region to be us-west-1 however, if I change my AWS.config to that, throws a different error. Amazon's own guides say that the area "Northern California" are encoded as us-west-2, so I am not sure why there is this inconsistency.

Upvotes: 5

Views: 4396

Answers (2)

Nagy Szili
Nagy Szili

Reputation: 111

Now you need to set in JSON. Permissions -> CORS

[{
    "AllowedHeaders": [
        "*"
    ],
    "AllowedMethods": [
        "GET"
    ],
    "AllowedOrigins": [
        "*"
    ],
    "ExposeHeaders": []
}]

Upvotes: 3

Ajay Tiwari
Ajay Tiwari

Reputation: 1

AWS S3 Bucket => Add MetaData 'Cache-Control' : 'no-cache'

Upvotes: 0

Related Questions