使用Flask作为文件上传的传递代理?

2024-06-16 14:02:04 发布

您现在位置:Python中文网/ 问答频道 /正文

它是为appengine的blobstore而设计的,因为它的上传接口每次都会生成一个临时端点。我想从前端去掉comlexity,Flask将接收post请求并将其转发到blobstore指定的端点。性能和流量成本一点都不关心,有人能推荐一种最直接的实现方法吗?在


Tags: 方法flask性能端点post流量成本appengine
1条回答
网友
1楼 · 发布于 2024-06-16 14:02:04

看一下the docs for the BlobStore flow,您需要做的就是自己接受该文件,然后将其发送到create_upload_url指定的端点:

@app.route("/upload-complete", methods=["POST"])
def handle_upload_response():
    """This will be called after every upload, but we can ignore it"""
    return "Success"

@app.route("/upload", methods=["POST"])
def upload():
    fp = request.files["name_of_file"]
    url = create_upload_url(url_for('handle_upload_response'))
    response = requests.post(url, {'file':
                                   (fp.filename, fp.stream,
                                    fp.content_type, fp.headers)})
    if response == "Success":
        return "File uploaded successfully"
    else:
        return "Something didn't work out"

相关问题 更多 >