简体   繁体   中英

Download multiple files from S3 django

Here is the link i have used ( Download files from Amazon S3 with Django ). Using this i'm able to download single file.

Code:

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)

I need to download multiple files from S3, as a zip would be better. Can the mentioned method be modified and used. If not please suggest other method.

Okay here is a possible solution, it basically downloads each file and zips them into a folder, then returns this to the user.

Not sure if s3_template_path is the same for each file, but change this if 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        

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