如何在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.
任何帮助将不胜感激.