Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
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.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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        
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda