Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Flask as pass through proxy for file upload?

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?

like image 541
user3242938 Avatar asked Oct 01 '22 09:10

user3242938


1 Answers

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"
like image 92
Sean Vieira Avatar answered Oct 31 '22 14:10

Sean Vieira