从S3 django下载多个文件

时间:2017-04-13 16:10:42

标签: python django amazon-web-services amazon-s3

这是我使用的链接(Download files from Amazon S3 with Django)。使用这个我可以下载单个文件。

代码:

s3_template_path = queryset.values('file')
filename = 'test.pdf'
conn = boto.connect_s3('<aws access key>', '<aws secret key>')
bucket = conn.get_bucket('your_bucket')
s3_file_path = bucket.get_key(s3_template_path)
response_headers = {
'response-content-type': 'application/force-download',
'response-content-disposition':'attachment;filename="%s"'% filename
}
url = s3_file_path.generate_url(60, 'GET',
            response_headers=response_headers,
            force_http=True)
return HttpResponseRedirect(url)

我需要从S3下载多个文件,因为zip会更好。可以修改和使用上述方法。如果没有,请建议其他方法。

1 个答案:

答案 0 :(得分:1)

好的,这是一个可能的解决方案,它基本上下载每个文件并将它们压缩到一个文件夹中,然后将其返回给用户。

不确定每个文件s3_template_path是否相同,但如果需要,请更改此内容

# python 3

import requests
import os
import zipfile

file_names = ['test.pdf', 'test2.pdf', 'test3.pdf']

# set up zip folder
zip_subdir = "download_folder"
zip_filename = zip_subdir + ".zip"
byte_stream = io.BytesIO()
zf = zipfile.ZipFile(byte_stream, "w")  


for filename in file_names:
    s3_template_path = queryset.values('file')  
    conn = boto.connect_s3('<aws access key>', '<aws secret key>')
    bucket = conn.get_bucket('your_bucket')
    s3_file_path = bucket.get_key(s3_template_path)
    response_headers = {
    'response-content-type': 'application/force-download',
    'response-content-disposition':'attachment;filename="%s"'% filename
    }
    url = s3_file_path.generate_url(60, 'GET',
                response_headers=response_headers,
                force_http=True)

    # download the file
    file_response = requests.get(url)  

    if file_response.status_code == 200:

        # create a copy of the file
        f1 = open(filename , 'wb')
        f1.write(file_response.content)
        f1.close()

        # write the file to the zip folder
        fdir, fname = os.path.split(filename)
        zip_path = os.path.join(zip_subdir, fname)
        zf.write(filename, zip_path)    

    # close the zip folder and return
    zf.close()
    response = HttpResponse(byte_stream.getvalue(), content_type="application/x-zip-compressed")
    response['Content-Disposition'] = 'attachment; filename=%s' % zip_filename
    return response