如何在Django模板中显示存储在Amazon S3中的媒体文件? [英] How to display media files stored in Amazon S3 in Django template?

查看:94
本文介绍了如何在Django模板中显示存储在Amazon S3中的媒体文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将Amazon S3与我的Django应用程序集成在一起.在我的S3存储桶中已经创建了一个媒体文件夹,当用户上传文件时,它就可以在那里显示.我不清楚的是如何在模板中显示这些文件.当前代码是:

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 }}"/>

我也尝试过:

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

要记住的另一件事是,当我尝试从S3帐户访问这些文件时,得到了此输出.不知道这是权限问题还是什么.但是,在管理员中该图像显示正常.

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.

  • 不再拒绝访问-我公开了访问权限(不确定这是否是唯一/最佳方法).尚不清楚如何在模板中显示它.网址显示404错误.

  • 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.

在Admin中,文件路径为/folder/file.jpg,但在模板中,文件路径为/media/folder/file.jpg.我认为这可能是问题的根源,但不确定为什么会有媒体.

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.

任何帮助将不胜感激.

推荐答案

此答案也可以在

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 }}"/>

这篇关于如何在Django模板中显示存储在Amazon S3中的媒体文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆