我正在尝试弄清楚如何将Pillow
Image
实例上传到Firebase存储分区。这可能吗?
以下是一些代码:
from PIL import Image
image = Image.open(file)
# how to upload to a firebase storage bucket?
我知道有一个gcloud-python
库,但这支持Image
个实例吗?将图像转换为字符串是我唯一的选择吗?
答案 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)