使用Flask作为代理通过文件上传? [英] Using Flask as pass through proxy for file upload?

查看:172
本文介绍了使用Flask作为代理通过文件上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它用于应用程序引擎的blobstore,因为其上载界面每次都会生成一个临时端点.我想摆脱前端的复杂性,Flask会接受发布请求,并将其转发到blobstore指定的终点.性能和流量成本根本不是问题,有人可以推荐一种最简单的实施方法吗?

It's for app engine's blobstore since its upload interface generates a temporary endpoint every time. I'd like to take the comlexity out of frontend, Flask would take the post request and forward it to the end point specified by blobstore. Performance and traffic cost is not a concern at all, can someone recommend a most straightforward way to implement?

推荐答案

查看 BlobStore流的文档,看来您需要做的就是自己接受文件,然后将其发送到create_upload_url指定的端点:

Looking at the docs for the BlobStore flow it looks like all you need to do is accept the file yourself and then send it on to the endpoint specified by 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"

这篇关于使用Flask作为代理通过文件上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆