简体   繁体   English

如何创建存储在Amazon S3存储桶中的文件列表,并将其显示为下载链接?

[英]How do I create a list of files stored in my Amazon S3 bucket and display them as download links?

I have a number of videos in an Amazon S3 bucket and I want to display them as a list on my website. 我在Amazon S3存储桶中有很多视频,我想将它们作为列表显示在我的网站上。 Clicking on any item in the list will download the video. 单击列表中的任何项目将下载视频。

I am using Python and Flask. 我正在使用Python和Flask。 I currently have the urls and names displaying on screen but I cannot download them. 我目前在屏幕上显示的网址和名称,但无法下载。 In python it would be simple to just use the "get_file" function on the S3 key but I can't figure out how I would call this method. 在python中,仅在S3键上使用“ get_file”函数会很简单,但我不知道如何调用此方法。 It seems like it should be simple to do but I may have to use javascript in html 'onClick.' 看起来应该很简单,但是我可能不得不在html'onClick'中使用javascript。

I am new to javascript so that may be an option but I would most of all like to just access the built in boto method. 我是javascript的新手,所以这可能是一个选择,但我最想只访问内置的boto方法。

Currently I have keys: 目前我有钥匙:

from boto.s3.connection import S3Connection

def get_video_keys():
key = local_settings.local_vars['AWS_ACCESS_KEY_ID']
skey = local_settings.local_vars['AWS_SECRET_ACCESS_KEY']
bucket = local_settings.local_vars['VIDEO_BUCKET_NAME']

conn = S3Connection(key, skey)
mybucket = conn.get_bucket(bucket)
keys = mybucket.get_all_keys()
return keys

And also some html to display them using Jinja2 templating. 还有一些html使用Jinja2模板显示它们。

{% for key in keys %}
        <li><div><a href="{{ key.generate_url(3600) }}"> {{ key.name }}</a></div></li>
{% endfor %}

Looking on line has only found ways to download all files rather than downloading in response to a click event. 在线查看仅找到了下载所有文件的方法,而不是下载响应单击事件的方法。

UPDATE 9/5/13 更新9/5/13

This actually works fine on Firefox but is not doing anything in Chrome/Chromium. 这实际上在Firefox上可以正常工作,但在Chrome / Chromium中却无能为力。

You can use generate_url to generate a url for downloading. 您可以使用generate_url生成要下载的URL。

For example: 例如:

urls = [key.generate_url(100) for key in keys]

Then you get some urls which will expire in 100 seconds. 然后,您将获得一些URL,这些URL将在100秒后失效。

EDIT 编辑

While you have some special character, use urllib.quote to escape them: 当您有一些特殊字符时,请使用urllib.quote对其进行转义:

new_urls = [urllib.quote(url) for url in urls]

You can install s3fs on your server to "mount" your S3 bucket as a file system. 您可以在服务器上安装s3fs ,以将S3存储桶“挂载”为文件系统。 s3fs is an implementation of Fuse for amazon-S3 s3fs是Amazon-S3的Fuse的实现

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

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