如何在Flask-RESTful PUT处理程序方法中使用像curl localhost:5000/upload/test.bin --upload-file tmp/test.bin
这样的curl命令保存上传的数据?
Ron Harlev's answer到Flask-RESTful - Upload image的代码使用curl -F "file=@tmp/test.bin" localhost:5000/upload/test.bin
的POST请求(稍微修改如下):
def post(self, filepath):
parse = reqparse.RequestParser()
parse.add_argument('file', type=werkzeug.datastructures.FileStorage, location='files')
args = parse.parse_args()
upload_file = args['file']
upload_file.save("/usr/tmp/{}".format(filepath))
return ({'filepath': filepath}, 200)
但是,如果我尝试使用代码来处理来自curl --upload-file
的PUT请求(当然,将post
更改为put
),我得到:“'NoneType'对象没有属性'save'”。这是指上面代码中的倒数第二行。
如何获取使用curl --upload-file
上传的文件数据的句柄,以便将其保存到本地文件?
更新:这解决了问题:curl --request PUT -F "file=@tmp/test.bin" localhost:5000/upload/test.bin
,但我仍然没有回答我的问题。
curls docs将--upload-file定义为PUT http请求https://curl.haxx.se/docs/manpage.html#-T。
我不确定是否需要通过一个安静的API处理这个问题,而且我很确定curl会导致问题,也许这个假设必须通过烧瓶来实现,这会让你退缩吗?
也许尝试将其构建为香草烧瓶端点,此代码应该适合您。
from flask import Flask, request, jsonify
...
@app.route('/simpleupload/<string:filepath>', methods=['POST','PUT'])
def flask_upload(filepath):
with open("/tmp/{}".format(filepath), 'wb') as file:
file.write(request.stream.read())
return (jsonify({'filepath': filepath}), 200)