How to display media files stored in Amazon S3 in

2020-08-01 06:08发布

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.

1条回答
放我归山
2楼-- · 2020-08-01 06:40

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 }}"/>
查看更多
登录 后发表回答