将大型视频文件上载到谷歌应用程序引擎

2024-04-26 05:06:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图通过谷歌应用程序引擎将一个大型视频上传到谷歌云存储中

我遵循了这篇文章中提到的上传大型电子表格的原则。 Can't upload large files to Python + Flask in GCP App Engine

我已经使用dropzone.js设置了分块

我已经在main.py中设置了一个上传,我希望它能将文件块上传到应用程序的tmp目录中,并在所有部分就绪后将完成的文件移动到谷歌云存储中

我收到以下错误消息:

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,

};



Tags: pathfromimportappflaskgetindexif