2017-01-30 183 views
0

我试图弄清楚如何将PillowImage实例上传到Firebase存储桶。这可能吗?Python:将Pillow图片上传到Firebase存储桶

下面是一些代码:

from PIL import Image 

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

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

回答

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) 
+0

''bucket-id-here''看起来像''gs:// example.appspot.com''还是只是''example.appspot.com''? – rigdonmr

+0

只是'example.appspot.com' :) –

相关问题