【问题标题】:Python AWS S3 Download S3 Files save in ZIPPython AWS S3 下载 S3 文件以 ZIP 格式保存
【发布时间】:2021-10-03 02:31:16
【问题描述】:

我有一堆文件存储在 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

【问题讨论】:

  • 请提供更多信息?您的代码中有什么不起作用,问题是什么?
  • 请阅读以下文章? stackoverflow.com/help/how-to-ask 它有助于以易于回答的方式提问。
  • @Dima 面临错误 stat: can't specify None for path argument --> zf.write(data)
  • 据我了解原因是 zf = zipfile.ZipFile(s, 'w')。第一个参数是文件名。您提供了一个空的 BitesIO(),因此当您尝试在那里写入时会遇到问题。
  • 任何解决方案

标签: python-3.x django amazon-web-services amazon-s3 boto3


【解决方案1】:

试试这个

使用 boto3 get_object

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.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

【讨论】:

    【解决方案2】:

    我有一个类似的要求,下面的代码可以满足:
    参考:zipfile documentation

    import sys
    import boto3
    import zipfile
    from io import StringIO, BytesIO
    import botocore
    import datetime
    
    if len(sys.argv) > 1:
        bucket_name=sys.argv[1];
    else:
        print("Please specify a bucket name to list.")
        sys.exit()
    
    s3 = boto3.client('s3')
    s3res = boto3.resource('s3')
    
    timestamp=datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S")
    
    file_name = '%s-files-%s.zip' % (bucket_name, timestamp)
    print(f"Saving into zip {file_name}")
    
    zf = zipfile.ZipFile(file_name, 'w')
    
    bucket = s3res.Bucket(bucket_name)
    try:
        for s3_object in bucket.objects.all():
            print("adding ",s3_object)
            data = s3.get_object(Bucket=bucket_name, Key=s3_object.key)
            zf.writestr(s3_object.key, data.get('Body').read())
    except botocore.exceptions.ClientError as resperror:
        print ("Error - does bucket exist?", str(resperror))
        print ("Please remove possible empty zip: ", file_name)
    zf.close()
    

    【讨论】:

      猜你喜欢
      • 2017-01-12
      • 1970-01-01
      • 2020-11-22
      • 2020-09-19
      • 1970-01-01
      • 2020-05-20
      • 2018-10-10
      • 1970-01-01
      • 2022-08-03
      相关资源
      最近更新 更多