Django amazon s3 SuspiciousOperation

Therefore, when I try to access a specific image on S3 from my browser, everything works fine. But when python does this, I get a SuspiciousOperation error. My static folder is open on S3, so I really don't know where this comes from.

 Publication.objects.get(id=4039).cover.url Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/vagrant/.pyenv/versions/blook/lib/python2.7/site-packages/django/db/models/fields/files.py", line 64, in _get_url return self.storage.url(self.name) File "/home/vagrant/.pyenv/versions/blook/lib/python2.7/site-packages/queued_storage/backends.py", line 291, in url return self.get_storage(name).url(name) File "/home/vagrant/.pyenv/versions/blook/lib/python2.7/site-packages/queued_storage/backends.py", line 115, in get_storage elif cache_result is None and self.remote.exists(name): File "/home/vagrant/.pyenv/versions/blook/lib/python2.7/site-packages/storages/backends/s3boto.py", line 410, in exists name = self._normalize_name(self._clean_name(name)) File "/home/vagrant/.pyenv/versions/blook/lib/python2.7/site-packages/storages/backends/s3boto.py", line 341, in _normalize_name name) SuspiciousOperation: Attempted access to 'http:/s3-eu-west-1.amazonaws.com/xpto/static/images/default-image.png' denied. 

My settings:

 AWS_S3_SECURE_URLS = True # use http instead of https S3_URL = 'http://s3-eu-west-1.amazonaws.com/%s' % AWS_STORAGE_BUCKET_NAME MEDIA_ROOT = 'media/' STATIC_ROOT = '/static/' STATIC_URL = S3_URL + STATIC_ROOT MEDIA_URL = S3_URL + '/' + MEDIA_ROOT 

So far I can get around this, but this is not a long-term solution. any ideas?

+5
source share
1 answer

Danigosa answer in this thread is the answer: django repositories and amazon s3 are suspicious operations

Create the custom storage class specified in this post: Using Amazon S3 to store Django static and multimedia files .

Then override _normalize_name as follows:

 from django.conf import settings from storages.backends.s3boto3 import S3Boto3Storage class StaticStorage(S3Boto3Storage): location = settings.STATICFILES_LOCATION def _clean_name(self, name): return name def _normalize_name(self, name): if not name.endswith('/'): name += "/" name += self.location return name class MediaStorage(S3Boto3Storage): location = settings.MEDIAFILES_LOCATION def _clean_name(self, name): return name def _normalize_name(self, name): if not name.endswith('/'): name += "/" name += self.location return name 

Finally - (at least in Python 3) DO NOT use

 {% load static from staticfiles %} 

in your templates.

Stick to:

 {% load static %} 
+1
source

Source: https://habr.com/ru/post/1200956/


All Articles