python-3.x - Python AWS S3 下载 S3 文件以 ZIP 格式保存
问题描述
我有一堆文件存储在 AWS S3 上。我想将这些发现下载到一个 zip 下面是我的代码。
import boto3
import zipfile
from io import StringIO, BytesIO
s3 = boto3.client('s3')
s = BytesIO()
zf = zipfile.ZipFile(s, 'w')
file_name = '%s-files-%s.zip' % (student.get_full_name(), str(datetime.datetime.now()))
files_key_list = ['file1.png', 'file3.png']
for f in files_key_list:
data = s3.download_file(settings.AWS_STORAGE_BUCKET_NAME, f, f)
zf.write(data)
zf.close()
resp = HttpResponse(s.getvalue(), content_type="application/x-zip-compressed")
resp['Content-Disposition'] = 'attachment; filename=%s' % file_name
return resp
错误
stat:无法为路径参数指定 None
解决方案
尝试这个
使用 boto3 get_object
import boto3
import zipfile
from io import StringIO, BytesIO
s3 = boto3.client('s3')
s = BytesIO()
zf = zipfile.ZipFile(s, 'w')
file_name = '%s-files-%s.zip' % (student.get_full_name(), str(datetime.datetime.now()))
files_key_list = ['file1.png', 'file3.png']
for f in files_key_list:
data = s3.get_object(Bucket=settings.AWS_STORAGE_BUCKET_NAME, Key=fpath.file_key)
zf.writestr(fpath.file_name, data.get('Body').read())
zf.close()
resp = HttpResponse(s.getvalue(), content_type="application/x-zip-compressed")
resp['Content-Disposition'] = 'attachment; filename=%s' % file_name
return resp
推荐阅读
- php - 如何将 PHP 值发送回 HTML 表单?
- powershell-2.0 - 当Powershell 2中的文件夹达到一定大小时重命名文件夹+删除超过一定大小的任何内容
- python - 以与输入相同的顺序返回列表的索引
- python - 命令“python setup.py egg_info”在 /tmp/pip-build-qgafa8x3/numba/ 中失败,错误代码为 1
- spring-boot - 保存新的父+子实体时“参数索引超出范围”(manyToOne)
- http - HTTP POST请求中的自定义内容类型?
- firebase - 使用 fastlane 和 Firebase App Distribution 分发 Flutter 应用
- mongodb - Mongodb - 索引使用和应用
- python - 用于测试 Postgres 连接并获取响应代码的 Python 代码
- android - 颤振错误:没有为“对象”类型定义方法“数据”