首页 > 解决方案 > python Firebase Cloud Storage - 上传文件而不将其保存在本地

问题描述

我想将 zip 文件上传到 Firebase Cloud Storage,但我想避免将其保存在本地。相反,我想将其存储在内存中(使用BytesIO),然后直接上传。可能吗?

标签: pythonzipgoogle-cloud-storagefirebase-admin

解决方案


Theoretically this should be possible to write from BytesIO to storage without writing the file locally.

There is a method in Blob class called upload_from_string (reference). The method accepts bytes and it's possible to add content type as well. Ex. I was able to run something like that:

    # create simplest BytesIO object
    b = io.BytesIO(b'hello')
    # create storage client
    storage_client = storage.Client()
    # create test bucket
    bucket = storage_client.bucket("vittoh-test-bytesio")
    # create test blob
    blob = bucket.blob("vitooh-test-blob")
    # upload with type zip
    blob.upload_from_string(b.read(),content_type='application/zip')

This created object with content_type "application/zip" in my testing bucket. I don't know how do you want to create this zip file in BytesIO, but after that it should be possible.


推荐阅读