Problems with CORS in Django using AWS S3

Asked

Viewed 364 times

7

My application uses a Bucket on AWS to host the static and MEDIA files, here’s the link: http://memoriasclubeturismoenv.us-east-2.elasticbeanstalk.com/gilson-rolim-02-12-2019 or https://memorias.clubeturismo.com.br/gilson-rolim-02-12-2019

In my application I use a plugin called "Dearflip".

The problem is that, for some reason, even configuring CORS in Bucket, still gives problem when running the plugin.

The error is this: Access to image at 'https://media-memorias.s3.amazonaws.com/media/models/Capa_mvd8KiC.png' from origin 'http://memoriasclubeturismoenv.us-east-2.elasticbeanstalk.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested Resource.

I added the following to Bucket:

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

Does anyone have any idea how to solve this mystery?

  • Tried using https://pypi.org/project/django-cors-headers/ ?

  • Yes, but it didn’t work, unfortunately :(

  • But you just installed and didn’t do the config CORS_ORIGIN_WHITELIST? If not apply it does nothing

  • Yes, I added the Hots to CORS_ORIGIN_WHITELIST but it didn’t work :(

  • The error is: Access to image at 'https://media-memorias.s3.amazonaws.com/media/models/Capa_bRI7lVR.png' from origin 'https://memorias.clubeturismo.com.br' has been blocked by CORS policy: No 'Access-Control-Present-Allow-Origin' header is on the requested Resource. Currently Django-Cors-headers is on

  • Looking at it, I don’t think it’s going to work, give me one: https://github.com/bradleyg/django-s3direct#access-setup

Show 1 more comment

1 answer

0

I think just add one more line in the Bucket configuration

<AllowedMethod>HEAD</AllowedMethod>

To test correctly, I go to www.jquery.com, open the console and type:

$.get('https://media-memorias.s3.amazonaws.com/media/models/Capa_mvd8KiC.png', 
  data => console.log(data) );

and correctly, shows CORS error

Access to XMLHttpRequest at
'https://media-memorias.s3.amazonaws.com/media/models/Capa_mvd8KiC.png'
from origin 'https://jquery.com' has been blocked by CORS policy: No
'Access-Control-Allow-Origin' header is present on the requested
resource.

for testing, I copied your image and put it in my new Bucket, and also showed the error, but after configuring it with HEAD it soon worked out.

inserir a descrição da imagem aqui


If I edit your Image Array and insert my link, I see everything right too:

inserir a descrição da imagem aqui

I just modified the variable option_button_image you have at the end of the page to contain my link...

  • I added the <Allowedmethod>HEAD</Allowedmethod> but it didn’t work :(, the strange thing is that the same image opens in the page, but when it starts the plugin, it doesn’t work.

  • @Victorfernandes but now with the HEAD I see all the other images I couldn’t get before ;) you’ve undone the "book"?

  • This is the strange thing, the other images open, except the first one. When I click to flip through, only the first image does not appear here. You can see them all?

  • do the following, delete the image, and upload again, even with another name, change in the code and try ... yes all other images can save the cover... but if you delete and put it back in Bucket can give result...

  • I did it, changed the image by the application, but again, it didn’t work :'(

  • just to see if it’s da-thing... do the following: put 2 cover images with different names, use one of the names in the start image, use the other name in the Image Array... and see if it works...

  • I tried renaming the file, but it won’t, and I noticed that it’s only the cover, the content and the back cover not the problem. This is how I populate the array: var option_button_image = {source: ["{{ Object.model.cover.url }",{% for x, y in images %}"{{ x.page.url }}","{{ y.url }}",{% endfor %}"{{ Object.model.back_cover.url }"]}; Where the cover is the cover, and the back_cover is the back cover.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.