Newtt
Newtt

Reputation: 6200

CORS error on Django S3 Direct

I'm using Django S3 Direct to upload files to an S3 Bucket

I use the following configs

AWS_SECRET_ACCESS_KEY = ''
AWS_ACCESS_KEY_ID = ''
AWS_STORAGE_BUCKET_NAME = ''

The values for these were generated in the IAM Management console

I setup the bucket and the CORS configuration is as follows

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

However, when I try to upload a file in the Django admin, it gives me the following error:

XMLHttpRequest cannot load https://s3.amazonaws.com/<bucketname>. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.

Upvotes: 2

Views: 1496

Answers (2)

Kanhaiya
Kanhaiya

Reputation: 58

I faced the same problem with django-s3direct even after adding CORS values as you mentioned. For me the problem was resolved after added the S3DIRECT_REGION = 'ap-southeast-1' you can choose your region from here

Upvotes: 0

Todor
Todor

Reputation: 16060

Same problem here, except that I keep gettings 301 from AWS. After some digging I found this comment comment:

From what I know, S3 is cross-region, which means that you do not need to specify a region to upload files to, as it will uploads to one and replicates to other regions behind the scenes. Therefor endpoint = 's3.amazonaws.com' If your bucket called 'test', you will upload to 'test.s3.amazonaws.com', not to 'test.s3-eu-west-1.amazonaws.com'.

So I solve this doing the opposite of what @Kanhaiya suggested. I had S3DIRECT_REGION in the settings and after removing it (to query s3.amazonaws.com directly) things get fixed!

Upvotes: 1

Related Questions