首页 > 解决方案 > 以 zip 格式获取目录中的所有文件

问题描述

我能够在一个字典中获取我想要的所有文件,但是当我想读取所有文件时,它只返回带有 API 烧瓶的字典的第一个文件,我不会返回所有文件,如 zip会好的。

def download():
"""
API Endpoint responsible for download student
:return:
"""
# If a valid payload wasn't sent just return a 404
if not request.json:
    return jsonify({"results": "404", "description": "invalid payload sent"}), 404
# Load the payload
payload = request.json
# TODO Flesh this out with proper security or else anyone can do a POST and auth in
# TODO Most likely need to create a common module for this
status_code = 200
my_response = {
    "results": {},
    "description": "",
}

if "studentid" in payload and "assignmentid" in payload:
    student_id = payload["studentid"]
    assignment_id = payload["assignmentid"]
    folder = download_for_single_student(student_id,assignment_id)

    return Response(
        folder['Body'].read(),
        mimetype='application/pdf',
        headers={"Content-Disposition": "attachment:report.pdf"}
    )


else:
    status_code = 400
    my_response[
        "description"
    ] = "Please ensure both studentid and assignmentid are in the request payload"
    print("Studentid and/or Assignmentid not found in request")

return jsonify(my_response), status_code

标签: jsonamazon-web-servicesflaskamazon-s3

解决方案


您一次只能从 Amazon S3 下载一个对象(即每个 API 调用一个对象)。

没有请求 Amazon S3 创建包含多个文件/文件夹的 Zip 文件的功能。


推荐阅读