I have a django project that uploads files to a AWS S3 bucket. The uploaded files and the static files are shown correctly if they are rendered in the template using the {% static %}
tag for example. However, when I want to access the image in a javascript code using model_object.image.url
, the image doesn't show up. I inspected the code, took the url that was rendered in the js code and pasted it on the browser, it gives me this error:
<Error>
<Code>AccessDenied</Code>
<Message>Query-string authentication requires the Signature, Expires and AWSAccessKeyId parameters</Message>
<RequestId>xxxx</RequestId><HostId>xxx</HostId>
</Error>
Some more information that might be useful:
The user uploads an image, then he can crop the image. I use Croppie for this, it is a js library that uses the url of the image like this:
$('.div').croppie({
url: '{{ model_object.image.url }}',
});
Everything works locally. It is a problem with AWS I clearly don't understand.
Following solarissmoke
comment, I added this bucket policy to my bucket:
{
"Version": "2012-10-17",
"Id": "Policy1468082822770",
"Statement": [
{
"Sid": "Stmt1468082812651",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::criptolibertad/*"
}
]
}
However, it still doesn't work. I noticed the url rendered in the template that doesn't work looks like this:
https://criptolibertad.s3.amazonaws.com/Django/0_squashmigrations.jpeg?Signature=HFDdOYvrfqz5DG...
If I open the resource directly from my bucket, the url looks like this:
https://s3-us-west-2.amazonaws.com/criptolibertad/Django/0_squash+migrations.jpeg?X-Amz-Date=201607...
I also right clicked on the folder and selected Make Public
just in case.
Any advice will help.