gpt4 book ai didi

python - 从 S3 django 下载多个文件

转载 作者:行者123 更新时间:2023-12-03 09:09:48 27 4
gpt4 key购买 nike

这是我使用过的链接( 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 会更好。上述方法是否可以修改并使用。如果没有,请建议其他方法。

最佳答案

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

不确定每个文件的 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

关于python - 从 S3 django 下载多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43397080/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com