Python 从S3 django下载多个文件

Python 从S3 django下载多个文件,python,django,amazon-web-services,amazon-s3,Python,Django,Amazon Web Services,Amazon S3,这是我使用的链接()。使用这个,我可以下载单个文件 代码: s3\u template\u path=queryset.values('文件') 文件名='test.pdf' 连接=双向连接 bucket=conn.get_bucket(“你的_bucket”) s3\u文件\u路径=bucket.get\u密钥(s3\u模板\u路径) 响应\u头={ “响应内容类型”:“应用程序/强制下载”, “响应内容处置”:“附件;文件名=“%s”“%filename” } url=s3文件路径。生成ur

这是我使用的链接()。使用这个,我可以下载单个文件

代码:

s3\u template\u path=queryset.values('文件')
文件名='test.pdf'
连接=双向连接
bucket=conn.get_bucket(“你的_bucket”)
s3\u文件\u路径=bucket.get\u密钥(s3\u模板\u路径)
响应\u头={
“响应内容类型”:“应用程序/强制下载”,
“响应内容处置”:“附件;文件名=“%s”“%filename”
}
url=s3文件路径。生成url(60,'获取',
响应标题=响应标题,
强制(http=True)
返回HttpResponseRedirect(url)

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

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

不确定每个文件的
s3\u template\u 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        
#python 3
导入请求
导入操作系统
进口拉链
文件名=['test.pdf','test2.pdf','test3.pdf']
#设置zip文件夹
zip_subdir=“下载文件夹”
zip_filename=zip_subdir+“.zip”
byte_stream=io.BytesIO()
zf=zipfile.zipfile(字节流,“w”)
对于文件名称中的文件名:
s3\u template\u path=queryset.values('文件')
连接=双向连接
bucket=conn.get_bucket(“你的_bucket”)
s3\u文件\u路径=bucket.get\u密钥(s3\u模板\u路径)
响应\u头={
“响应内容类型”:“应用程序/强制下载”,
“响应内容处置”:“附件;文件名=“%s”“%filename”
}
url=s3文件路径。生成url(60,'获取',
响应标题=响应标题,
强制(http=True)
#下载该文件
file\u response=requests.get(url)
如果文件\u response.status\u code==200:
#创建文件的副本
f1=打开(文件名“wb”)
f1.写入(文件\u response.content)
f1.关闭()
#将文件写入zip文件夹
fdir,fname=os.path.split(文件名)
zip\u path=os.path.join(zip\u subdir,fname)
写入(文件名,zip_路径)
#关闭zip文件夹并返回
zf.close()
response=HttpResponse(byte_stream.getvalue(),content_type=“application/x-zip-compressed”)
响应['Content-Disposition']='附件;文件名=%s“%zip\u文件名”
返回响应

您要查找的每个文件的s3\u template\u路径是否相同?否,模板路径不同或者我编辑了两次,只要文件\u response=requests,这个版本应该可以工作。get(url)正确返回文件谢谢,将尝试此方法好的,您可能必须在循环内生成s3\u template\u路径,如果可以,请告诉我,我试图使用此代码,但我遇到了这种错误。
“str”对象没有属性“get”
。顺便说一句,我正在使用django 2.2和python 3。
# 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