Python:将Pillow Image上传到Firebase存储桶

时间:2017-01-30 09:29:49

标签: python firebase pillow firebase-storage

我正在尝试弄清楚如何将Pillow Image实例上传到Firebase存储分区。这可能吗?

以下是一些代码:

from PIL import Image

image = Image.open(file)
# how to upload to a firebase storage bucket?

我知道有一个gcloud-python库,但这支持Image个实例吗?将图像转换为字符串是我唯一的选择吗?

1 个答案:

答案 0 :(得分:1)

gcloud-python库是要使用的正确库。它支持从文件系统上的字符串,文件指针和本地文件上传(参见the docs)。

from PIL import Image
from google.cloud import storage

client = storage.Client()
bucket = client.get_bucket('bucket-id-here')
blob = bucket.blob('image.png')
# use pillow to open and transform the file
image = Image.open(file)
# perform transforms
image.save(outfile)
of = open(outfile, 'rb')
blob.upload_from_file(of)
# or... (no need to use pillow if you're not transforming)
blob.upload_from_filename(filename=outfile)