python - 如何使用 Flask-RESTPlus 发布多个文件?

标签 python flask file-upload flask-restplus

我希望能够发布一组文件。我让它与单个文件一起工作,但是在将 action="append" 添加到解析器时它不起作用。添加 action 时,swagger UI 允许我上传多个文件,但是当打印 files 变量时它是 None 即使我选择了多个文件。如何使用 Flask-RESTPlus 发布一组文件?

    from flask_restplus import Namespace, Resource, reqparse
    from werkzeug.datastructures import FileStorage

    file_upload = Namespace(name="files")
    upload_parser = reqparse.RequestParser(bundle_errors=True)
    upload_parser.add_argument(
        name="files",
        type=FileStorage,
        location="files",
        action="append" # If this is removed it works with 1 file
    )

    @files_upload.route("/")
    class Files(Resource):
        @files_upload.expect(upload_parser)
        def post(self):
            args = upload_parser.parser_args()
            files = args["files"]
            do_something(files)
            return ""

最佳答案

试试request.files.getlist

class UploadImg(Resource):

   def store_images(self,image,filename):
       S3_KEY = '************'
       S3_SECRET = '****************'
       S3_BUCKET = '****'
       file='pictures/'+filename
       content_type = request.mimetype
       s3 = boto3.client('s3', 
       config=boto3.session.Config(signature_version='s3v4'),
                      region_name='ap-south-1',
                      aws_access_key_id=S3_KEY,
                      aws_secret_access_key=S3_SECRET)
       s3.put_object(Body=image,    Bucket=S3_BUCKET,     Key=file,       
                  ContentType=content_type)

    def post(self):
       files= request.files.getlist('image')
       for img in files:
           print(type(img))
           print(img.filename)
           self.store_images(img,img.filename)
        return '',201

关于python - 如何使用 Flask-RESTPlus 发布多个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58009068/

相关文章:

python - 为什么当 View 引发异常时只调用一个 Flask teardown_request 函数?

node.js - Nodejs上传并处理视频

amazon-web-services - Amazon S3 如何在 “folder” 中列出文件

python - 使用 Scrapy 进行条件 URL 抓取

python - SciKit-Learn 随机森林子样本大小如何等于原始训练数据大小?

python - 类型错误 : _deserialize() got an unexpected keyword argument 'partial' in marshmallow

Python Flask jinja2 模板变量上下文。无法渲染变量

ruby-on-rails - 如何使用 Active Storage 保留存储空间和加载时间?

Python fabric 如何为用户提示发送密码和是/否

Python:urllib2.HTTPError:HTTP 错误 401:未经授权