简体   繁体   English

如何在Django模板中显示存储在Amazon S3中的媒体文件?

[英]How to display media files stored in Amazon S3 in Django template?

I have integrated Amazon S3 with my Django app. 我已将Amazon S3与Django应用程序集成在一起。 A media folder has been created in my S3 bucket, and when users upload a file it shows up there just fine. 在我的S3存储桶中已经创建了一个媒体文件夹,当用户上传文件时,它就可以在那里显示。 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. 要记住的另一件事是,当我尝试从S3帐户访问这些文件时,得到了此输出。 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. 网址显示404错误。

  • In Admin the file path is /folder/file.jpg, but in the template the file path is /media/folder/file.jpg. 在管理中,文件路径为/folder/file.jpg,但在模板中,文件路径为/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. 任何帮助,将不胜感激。

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. 您也可以在模板上的Amazon S3服务器上的获取媒体图像上找到此答案因此,如果mods不需要这样做,请随时删除。

Super simple. 超级简单。 All you have to do is: 您要做的就是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM