简体   繁体   中英

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. Clicking on any item in the list will download the video.

I am using Python and 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. It seems like it should be simple to do but I may have to use javascript in html 'onClick.'

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.

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.

{% 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

This actually works fine on Firefox but is not doing anything in Chrome/Chromium.

You can use generate_url to generate a url for downloading.

For example:

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

Then you get some urls which will expire in 100 seconds.

EDIT

While you have some special character, use urllib.quote to escape them:

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 is an implementation of Fuse for amazon-S3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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