The important part of this is configuring the DEFAULT_FILE_STORAGE
to the s3Boto3Storage in your .env
:
AWS_ACCESS_KEY_ID=...\nAWS_SECRET_ACCESS_KEY=...\nAWS_STORAGE_BUCKET_NAME=my-bucket\nAWS_S3_REGION_NAME=us-east-1\n\n# This is the important config\nDEFAULT_FILE_STORAGE=storages.backends.s3boto3.S3Boto3Storage\n
With these env variables set I later added an S3 config in the settings:
\n# S3 storage configuration\nif AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY:\n # AWS_DEFAULT_ACL = \"public-read\" # I don't use ACL so I don't use it, but you can have it set here.\n\n # I generate an S3 custom domain here because I manage multiple projects in a single bucket, but you can generate a static url here.\n # I just find this to be more organized.\n AWS_S3_CUSTOM_DOMAIN = (\n f\"{AWS_STORAGE_BUCKET_NAME}.s3.{AWS_S3_REGION_NAME}.amazonaws.com\"\n )\n AWS_LOCATION = \"media\" # You can change this base on your desire media directory.\n\n # Media URL will point to S3\n MEDIA_URL = f\"https://{AWS_S3_CUSTOM_DOMAIN}/{AWS_LOCATION}/\"\n # I don't set MEDIA_ROOT here since files are provisioned by an s3 url.\n\n # S3 upload settings\n AWS_S3_FILE_OVERWRITE = False # Don't overwrite files with the same name (optional)\n AWS_QUERYSTRING_AUTH = False # Don't add query parameters to URLs (optional)\nelse:\n # If the S3 config fails then I fallback to defaults. Not needed I think but I do it. \n MEDIA_URL = \"/media/\"\n MEDIA_ROOT = os.path.join(BASE_DIR, \"media\")\n
By doing this configurations I was able to change the default behavior of the Logo file storage and handle the S3 implementation. This is more of an S3/Django default configurations. But I think it's worth to have it documented somewhere.
","upvoteCount":1,"url":"https://github.com/fabiocaccamo/django-admin-interface/discussions/383#discussioncomment-12495601"}}}-
Hello all, I want to serve media files over a S3 bucket, but don't want the media to be entirely public. But I do need the logos public! They are stored in the media folder. Does anybody know how to achieve this? Love the work! |
Beta Was this translation helpful? Give feedback.
-
@hcwinsemius this could be achieved by supporting a new optional setting |
Beta Was this translation helpful? Give feedback.
-
Hi, I stumble through this issue and got around it kinda simple. But It was hard for me to figure out the pieces so I'll leave this here and look to open a PR to enhance documentarion. First of all in my
The important part of this is configuring the
With these env variables set I later added an S3 config in the settings:
By doing this configurations I was able to change the default behavior of the Logo file storage and handle the S3 implementation. This is more of an S3/Django default configurations. But I think it's worth to have it documented somewhere. |
Beta Was this translation helpful? Give feedback.
Hi, I stumble through this issue and got around it kinda simple. But It was hard for me to figure out the pieces so I'll leave this here and look to open a PR to enhance documentarion.
First of all in my
settings.py
I have something like this: