I'm trying to set up Django so that all static files are uploaded to s3, but for whatever reason, it's not working. Here is the relevant section in settings.py
:
AWS_STORAGE_BUCKET_NAME = "bucket_name"
AWS_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY")
AWS_S3_FILE_OVERWRITE = False
AWS_S3_REGION_NAME = "us-east-2"
AWS_S3_CUSTOM_DOMAIN = (
f"{AWS_STORAGE_BUCKET_NAME}.s3.{AWS_S3_REGION_NAME}.amazonaws.com"
)
AWS_S3_ENDPOINT_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}"
AWS_LOCATION = "static"
STATIC_URL = f"{AWS_S3_ENDPOINT_URL}/{AWS_LOCATION}/"
STATICFILES_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
Supposing that my bucket is called bucket_name, this is what will happen:
How do I either get the django assets to use this new address, or change the address on aws so that it aligns with django?
I've done some things to debug this.
AWS_STORAGE_BUCKET_NAME
after all of these lines, all that will change is the folder name will change from bucket_name to whatever else. Except I don't want to rename this folder, I want to either remove it entirely or get django to understand this folder.STATICFILES_STORAGE
doesn't seem to do anything, unless it gets removed, which will cause an errorAWS_LOCATION
just changes the folder to bucket_name/<new_thing>/static, it has no effect on the base folder. This will make the assets use <new_thing>/static, but the bucket_name still isn't there.I imagine my problem is similar or the same as the one here: why static files and images are not working on my django s3 bucket project? . But it did not get a solution.
My problem was that I was using both AWS_S3_CUSTOM_DOMAIN
and AWS_S3_ENDPOINT_URL
. I had to pick just one of them.
When AWS_S3_CUSTOM_DOMAIN
is specified and not AWS_S3_ENDPOINT_URL
, S3 will contain a folder according to AWS_LOCATION
, so in my case, the static files are stored in https://bucket_name.s3.us-east-2.amazonaws.com/static . Django will access this URL and the assets will be found.
When AWS_S3_ENDPOINT_URL
is specified and not AWS_S3_CUSTOM_DOMAIN
, S3 will contain a folder with the bucket name, which contains a folder according to AWS_LOCATION
. So I can access it with https://bucket_name.s3.us-east-2.amazonaws.com/bucket_name/static . Django again will be able to access this URL as would be expected.
The problem arises when I have both at the same time. The domain for AWS_S3_CUSTOM_DOMAIN
will be used on django and AWS_S3_ENDPOINT_URL
will be used on S3, resulting in the disconnect that I was having.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.