我正在尝试通过Google App Engine将大型视频上传到Google Cloud Storage。
我遵循了这篇文章中的原则,即上传大型电子表格。 Can't upload large files to Python + Flask in GCP App Engine
我已经使用dropzone.js设置了分块
我已经在main.py中设置了一个上传文件,希望将文件块上传到应用程序的tmp目录中,并添加了将所有文件都放置到位后将完整文件移至Google Cloud Storage中的逻辑。
我收到以下错误消息:
TypeError:write()参数必须为str,而不是字节
这是我的后端代码
from flask import Flask, render_template, request, redirect, url_for from google.cloud import storage from flask_dropzone import Dropzone from werkzeug.utils import secure_filename import os import base64 app = Flask(__name__, template_folder='./templates', static_folder="./static") dropzone = Dropzone(app) app.config['UPLOAD_PATH'] = '/tmp' @app.route('/', methods=['GET', 'POST']) def index(): return render_template('index.html') @app.route('/upload', methods=['POST', 'GET']) def upload(): if request.method == 'POST': upload_file = request.files.get('file') tmp_file_path = '/tmp/' + upload_file.filename with open(tmp_file_path, 'a') as f: f.write(uploaded_file.read()) chunk_index = int(flask.request.form.get('dzchunkindex')) if (flask.request.form.get('dzchunkindex') is not None) else 0 chunk_count = int(flask.request.form.get('dztotalchunkcount')) if (flask.request.form.get('dztotalchunkcount') is not None) else 1 if (chunk_index == (chunk_count - 1)): print('Saving file to storage') print( chunk_count ) storage_client = storage.Client() storage_bucket = storage_client.get_bucket('percy-277618.appspot.com') blob = storage_bucket.blob(upload_file.filename) blob.upload_from_filename(tmp_file_path, client=storage_client) print('Saved to Storage') print('Deleting temp file') os.remove(tmp_file_path) if __name__ == '__main__': app.run(host='127.0.0.1', port=8080, debug=True)
这是我的前端代码。
Dropzone.options.uploadwidget = { paramName: 'file', forceChunking: true, timeout: 300000, chunking: true, url: '/upload', chunkSize: 10485760, maxFilesize: 1025, };
答案 0 :(得分:1)
使用uploaded_file.read()
产生字节,而不是字符串。您应该在binary mode中打开文件:
with open(tmp_file_path, 'ab') as f: