在 PySide 中上传 firebase 存储的进度

问题描述 投票:0回答:0

我正在尝试将文件上传到 firebase 存储,我可以看到使用 firebase-admin storage.blob 上传的 3 个选项

  • upload_from_string
  • 从文件上传
  • 上传自_文件名

我正在尝试在 python PySide 中获取文件的上传进度 我使用 1 mb 块字符串使用 upload_from_string 实现了显示文件上传的进度。但是在 firebase-storage 中,每个数据块都会替换该文件。有什么方法可以将数据附加到同一个文件而不是替换?

这是我使用的上传线程代码

from firebase-admin import storage
from PySide2.QtCore import QThread, Signal

class UploadThread(QThread):
    progress = Signal(int)

    def __init__(self, bucket_name, blob_name, file_path):
        super().__init__()
        self.bucket_name = bucket_name
        self.blob_name = blob_name
        self.file_path = file_path

    def run(self):
        client = storage.Client()
        bucket = client.get_bucket(self.bucket_name)
        blob = bucket.blob(self.blob_name)

        with open(self.file_path, 'rb') as f:
            chunk_size = 1024 * 1024 # 1MB
            total_size = os.path.getsize(self.file_path)
            bytes_uploaded = 0

            while True:
                chunk = f.read(chunk_size)
                if not chunk:
                    break

                blob.upload_from_string(chunk)
                bytes_uploaded += len(chunk)
                progress = int(bytes_uploaded / total_size * 100)
                self.progress.emit(progress)

        self.progress.emit(100)
python-3.x firebase firebase-admin pyside6
© www.soinside.com 2019 - 2024. All rights reserved.