Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django S3 uploaded file urls show credentials

I am using django-storages and Amazon S3 for file storages. In my model I have: avatar = models.ImageField(_('Avatar'), upload_to='avatars/profiles/', blank=True, null=True)

The image is uploaded successfully on save, but full url with credentials is saved. In my Retrieve requests/ when I read the url from db via console) I get something like: https://subdomain.amazonaws.com/avatars/profiles/filename.jpg?X-Amz-Algorithm=XXX&X-Amz-Expires=XXX&X-Amz-SignedHeaders=XXXX&X-Amz-Signature=XXXX&X-Amz-Date=XXXXXX&X-Amz-Credential=XXXX

How can I prevent this? I could strip the url before responding, but I do not need and therefore do not want to save them in this format, because all files can be accessed publicly, also no need for credentials. Ps. I though of using the post_save hook but it seemed like a hack to me.

like image 918
Ali Rasim Kocal Avatar asked Nov 26 '25 20:11

Ali Rasim Kocal


1 Answers

To remove the authentication credentials in the query string, set AWS_QUERYSTRING_AUTH = False in your settings.py. From django-storages documentation at https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html:

AWS_QUERYSTRING_AUTH (optional; default is True)

Setting AWS_QUERYSTRING_AUTH to False to remove query parameter authentication from generated URLs. This can be useful if your S3 buckets are public.

like image 102
Vitor Baptista Avatar answered Nov 29 '25 08:11

Vitor Baptista



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!