Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3 bucket name automatically appended to URL path #1349

Closed
niccolomineo opened this issue Jan 17, 2024 · 7 comments
Closed

S3 bucket name automatically appended to URL path #1349

niccolomineo opened this issue Jan 17, 2024 · 7 comments

Comments

@niccolomineo
Copy link

niccolomineo commented Jan 17, 2024

An S3 file URL is expected to be as follows:

https://<bucket_name>.s3.amazonaws.com/public/media/<file_name>

The problem is django-storages appears to automatically append the bucket name to the endpoint URL, which therefore leads to having:

https://<bucket_name>.s3.amazonaws.com/<bucket_name>/public/media/<file_name>

These are the init'ed Django settings:

AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_STORAGE_BUCKET_NAME=<bucket_name>
AWS_S3_ENDPOINT_URL="https://<bucket_name>.s3.amazonaws.com"
AWS_LOCATION="public/media"
AWS_QUERYSTRING_AUTH=True
AWS_S3_FILE_OVERWRITE=True

Any ideas on how to prevent this behavior cleanly?

@niccolomineo niccolomineo changed the title Bucket name showing up twice in URL S3 bucket name showing up twice in URL Jan 17, 2024
@niccolomineo niccolomineo changed the title S3 bucket name showing up twice in URL S3 bucket name showing up in URL path Jan 17, 2024
@niccolomineo niccolomineo changed the title S3 bucket name showing up in URL path S3 bucket name automatically appended to URL path Jan 17, 2024
@shukurillo0516
Copy link

Try to set AWS_S3_CUSTOM_DOMAIN in your settings

@niccolomineo
Copy link
Author

AWS_S3_CUSTOM_DOMAIN

But then I would lose the query strings.

@shukurillo0516
Copy link

shukurillo0516 commented Jan 18, 2024

If you mean AWS_QUERYSTRING_AUTH you shouldn't lose it.

@niccolomineo
Copy link
Author

niccolomineo commented Jan 18, 2024

If you mean AWS_QUERYSTRING_AUTH you shouldn't lose it.

I do lose it unfortunately, there is an open thread on this. If there is not other way around the bucket name thing but use AWS_S3_CUSTOM_DOMAIN, I'll resort to one of the hacks suggested in that Github issue to work around the query strings bug.

@niccolomineo
Copy link
Author

I ended up omitting the AWS_S3_ENDPOINT_URL altogether in production, only using it for the local MinIO instance.

@amoralesc
Copy link

Hey, pretty sure this has to do with the AWS_S3_ADDRESSING_STYLE variable. Setting this variable to virtual, and setting your AWS_S3_ENDPOINT_URL to https://s3.amazonaws.com will automatically prepend your bucket name to the endpoint and not add it to the URL path.

@niccolomineo
Copy link
Author

Hey, pretty sure this has to do with the AWS_S3_ADDRESSING_STYLE variable. Setting this variable to virtual, and setting your AWS_S3_ENDPOINT_URL to https://s3.amazonaws.com will automatically prepend your bucket name to the endpoint and not add it to the URL path.

I did see that but thanks for highlighting it, maybe someone who really need to set AWS_S3_ENDPOINT_URL explicitly will benefit from your suggestion. I managed to skip that step entirely, as it seems to be how django-storages works out of the box.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants