5

I have integrated Amazon S3 with my Django app. A media folder has been created in my S3 bucket, and when users upload a file it shows up there just fine. What I'm unclear about is how to display these files in the template. Currently the code is:

<img src="https://s3.console.aws.amazon.com/s3/buckets/myprojectbucket/media/{{ model.file }}"/>

I've also tried:

<img src="{{ model.file }}"/>

Another thing to keep in mind is when I try to access these files from the S3 account I get this output. Not sure if this is a permissions issue or what. However, in the admin the image shows up fine.

  • No longer getting access denied-I made access public (Not sure if this is the only/best way to do it). Still unclear on how to display it in template. The url gives a 404 error.

  • In Admin the file path is /folder/file.jpg, but in the template the file path is /media/folder/file.jpg. I think this could be the source of the problem, but not sure why media is there.

Any help would be appreciated.

  • Please see this answer: https://stackoverflow.com/questions/26604977/url-for-public-amazon-s3-bucket – Randall Hunt Nov 15 '18 at 22:27
  • Possible duplicate of [URL for public Amazon S3 bucket](https://stackoverflow.com/questions/26604977/url-for-public-amazon-s3-bucket) – Randall Hunt Nov 15 '18 at 22:28
  • @RandallHunt The question you link to helps somewhat, but mine is more about how to display it in a Django template. –  Nov 15 '18 at 22:58

1 Answers1

12

This answer can also be found at Getting MEDIA images on Amazon S3 server on template so if mods feel this is not needed feel free to delete.

Super simple. All you have to do is:

<img src="{{ model.file.url }}"/>