commonknowledge / banmarchive

The Amiel Melburn Archive - an online database of socialist and radical writings
https://banmarchive.org.uk/
1 stars 0 forks source link

small change to make presigned media urls on cdn work #10

Closed chrisdevereux closed 2 years ago

chrisdevereux commented 2 years ago

We'll also need to update MEDIA_URL in the app settings to point to the cdn-enabled spaces url.

As a reminder, the reason using the cdn is particuarly important is that it runs over cloudflare, so (unlike the IP of a DO spaces bucket) if an ISP were to carelessly IP-block it (as virgin media currently are), it would also break the entire internet. So they'd actually fix it.

From: https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html

AWS_S3_ADDRESSING_STYLE (optional: default is None) Possible values virtual and path.

From https://docs.digitalocean.com/products/spaces/how-to/enable-cdn/

You can also use presigned URLs with the Spaces CDN. To do so, configure your SDK or S3 tool to use the non-CDN endpoint, generate a presigned URL for a GetObject request, then modify the hostname in the URL to be the CDN hostname (..cdn.digitaloceanspaces.com, unless the Space uses a custom hostname).

chrisdevereux commented 2 years ago

@conatus @janbaykara fyi

conatus commented 2 years ago

Nice one for finding this.

In this PR we added AWS_S3_ENDPOINT_URL to the mix in order to begin serving from the CDN, as MEDIA_URL (which was set to the CDN on StopWatch but seemingly ignored) doesn't appear to be taken up. Suspect we may have to do this trick here also, but hopefully it should just work with MEDIA_URL.

The great thing is once this is solved, we've solved it. Maybe one for a short blog post!

chrisdevereux commented 2 years ago

you're right, AWS_S3_CUSTOM_DOMAIN is the right one. MEDIA_URL is apparently only used by django's default storage so not helpful here. Where we went wrong before is that the signed url didn't work on the cdn until we changed AWS_S3_ADDRESSING_STYLE.

conatus commented 2 years ago

Cool!

For the record I think the library calling the variable AWS_S3_CUSTOM_DOMAIN is absolutely heading down a silly path. Why call it this when it is about CDNs, typically Cloudfront presumably on AWS? It is even how the documentation talks about it. Anyway, now we know!

https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#cloudfront