从S3下载多个文件django

问题描述:

这是我使用的链接(Download files from Amazon S3 with Django)。使用这个我可以下载单个文件。从S3下载多个文件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会更好。所提及的方法是否可以修改和使用?如果不是请建议其他方法。

+0

是s3_template_path每个相同的文件,你正在寻找? – kujosHeist

+0

不,模板路径不同 – Manasa

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

不知道s3_template_path是为每个文件相同,但改变这个如果neccessary

# 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   
+0

对不起,我编辑了两次,这个版本应该可以,只要file_response = requests.get(url)正确返回文件 – kujosHeist

+0

谢谢,会试试这个 – Manasa

+0

好吧,你可能需要生成s3_template_path内循环,让我知道它是否可行 – kujosHeist