0

I've used Django-Cookiecutter's default setup for Heroku, with Whitenoise serving static files and S3 serving media files.

However, I want to use 'media' to store certain, heavier content files (pdfs) that are going to change over time, not just user file uploads, and a change shouldn't require a collectstatic. (Eventually I'll create an authoring tool to upload files to S3, but for the moment I'm just manually placing some files like pdfs in correct S3 bucket subdirectories.)

The standard setup for Django-Cookiecutter is to set the MEDIA_URL to the full S3 address, like https://my-bucket.s3.amazonaws.com/media/some.pdf ... but I want to access these files in my templates using relative path like /media/some.pdf in the same way I can browse directly to static content via /static.

I want a relative URL like /media/some.pdf because I'm using the download attribute on the href tag, which should download the file when clicked, but with a fully qualified url pointing at amazonaws.com, the browser will ignore the download attribute and just load the file in the browser.

Is there a straightforward way to config Django (running on Heroku using Whitenoise for static and S3 for media) to return the file from S3 when accessed via /media?

DanielMcQ
  • 37
  • 1
  • 8

1 Answers1

2

So you want to the browser to open a "download file" pop-up when the user click on the link? The download attribute isn't working because it only works for same-origin, according to the specs:

This attribute only works for same-origin URLs.

The specs also mentions that Content-Disposition header overrides it, so the header is a more robust way of doing this.

You can set-up S3 to do that by setting the Content-Disposition header properly. Here is a running example below:

<a href="https://s3.eu-west-2.amazonaws.com/test-cookie-cutter/media/cookiecutter-attachment.pdf">
  Document with header set
</a>
<br/>
<a href="https://s3.eu-west-2.amazonaws.com/test-cookie-cutter/media/cookiecutter.pdf">
  Document without header
</a>

When I uploaded the one with header, I've added the metadata header in the AWS console:

Content-Disposition: attachment; filename="cookiecutter.pdf" 

You cannot host your media files on Heroku, they will be lost at the next deploy due to their ephemeral filesystem, so having them on the server itself isn't an option.

Bruno A.
  • 1,765
  • 16
  • 17
  • Thanks Bruno...didn't realize that download attribute only worked for same domain. What I was hoping, however, was that there was a standard way to have the url point to current domain (mysite.com/media/blah.pdf), but then have Django load asset from S3. So the URL for the user looks like my domain, but the server is pulling from S3 behind the scene. Is that not possible? – DanielMcQ Nov 25 '18 at 20:45
  • I'm actually not sure about why the download attribute doesn't work, I'll rephrase to make this clearer. There is not a standard way as far as I know. The thing that spring to mind is a Django view proxying the media from S3, but that seems hacky without an actual media file. – Bruno A. Nov 26 '18 at 13:46
  • Actually, the spec confirms what I suspected, so I've updated my answer to reflect that, which a link to it. – Bruno A. Nov 26 '18 at 13:55