正在将大型视频文件上传到 Google App Engine

Uploading large video file to Google App Engine

我正在尝试通过 Google App Engine 将大型视频上传到 Google 云存储。

我遵循了这个 post 中提到的上传大型电子表格的原则。

我已经使用 dropzone.js

设置了分块

我在 main.py 中设置了一个上传,我希望将文件块上传到应用程序的 tmp 目录中,并放入将完成的文件移动到 Google 云存储一次的逻辑所有部件都已到位。

我收到以下错误消息:

TypeError: write() 参数必须是 str,而不是 bytes

这是我的后端代码


    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,

};


使用uploaded_file.read() 生成字节,而不是字符串。您应该在 binary mode:

中打开文件
with open(tmp_file_path, 'ab') as f: