M
M
MalikDeveloper20772020-08-08 00:25:38
Django
MalikDeveloper2077, 2020-08-08 00:25:38

AWS s3 & Django & Nginx. Configuration?

(I’ll say right away that it works perfectly on localhost)

I made a configuration for my s3 bucket, Django media files and Nginx server.
It worked, the pictures loaded and showed, but after a few hours it no longer works ...
I don't know if it matters, but I created a bucket policy s3 for the "getObject" action with the condition that the request must have a User-Agent header with a secret line, for example: My Django AWS Configuration (django_storages):
curl -I some.url -H "User-Agent: dog"


AWS_ACCESS_KEY_ID = 'my_access'
AWS_SECRET_ACCESS_KEY = 'my_private'
AWS_STORAGE_BUCKET_NAME = 'diasmart-media-static'

S3_REGION = 'eu-north-1'
AWS_S3_CUSTOM_DOMAIN = f'https://s3-{S3_REGION}.amazonaws.com/{AWS_STORAGE_BUCKET_NAME}'
AWS_S3_SIGNATURE_VERSION = 's3v4'

AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'


# Static and media
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

MEDIA_URL = '/media/'


My Nginx config
location /media {
                resolver 8.8.8.8;

                limit_except GET {
                        deny all;
                }

                set $aws_bucket "diasmart-media-static";
                set $aws_endpoint "s3-eu-north-1.amazonaws.com";
                set $aws_custom_secret "for_example_dog";

                proxy_set_header User-Agent $aws_custom_secret;

                rewrite ^/media/(.*)$ /$aws_bucket/$1 break;
                proxy_buffering off;
                proxy_pass https://$aws_endpoint;
        }


When I upload photos from the Django admin page, there are no errors, but no images are shown.
And my s3 bucket is empty...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question