小编mer*_*rio的帖子

使用Flask将文件上传到Google云端存储

我正在尝试使用Flask通过App Engine实例将一些图像上传到GCS,但每次上传文件时,当我下载它时,我都会收到一个损坏的文件.我究竟做错了什么 ?

我已经按照文档的方式下载并使用了Google云端存储客户端.

@app.route('/upload', methods=['POST'])
def upload():
    if request.method == 'POST':
        file = request.files['file']
        extension = secure_filename(file.filename).rsplit('.', 1)[1]
        options = {}
        options['retry_params'] = gcs.RetryParams(backoff_factor=1.1)
        options['content_type'] = 'image/' + extension
        bucket_name = "gcs-tester-app"
        path = '/' + bucket_name + '/' + str(secure_filename(file.filename))
        if file and allowed_file(file.filename):
            try:
                with gcs.open(path, 'w', **options) as f:
                    f.write(str(file))
                    print jsonify({"success": True})
                return jsonify({"success": True})
            except Exception as e:
                logging.exception(e)
                return jsonify({"success": False})
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助 !!

python google-app-engine google-cloud-storage

5
推荐指数
1
解决办法
2768
查看次数