Python:将Pillow图片上传到Firebase存储桶
问题描述:
我试图弄清楚如何将Pillow
Image
实例上传到Firebase存储桶。这可能吗?Python:将Pillow图片上传到Firebase存储桶
下面是一些代码:
from PIL import Image
image = Image.open(file)
# how to upload to a firebase storage bucket?
我知道有一个gcloud-python
库,但做到这一点支持Image
实例?将图像转换为字符串我唯一的选择?
答
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)
''bucket-id-here''看起来像''gs:// example.appspot.com''还是只是''example.appspot.com''? – rigdonmr
只是'example.appspot.com' :) –