2017-01-30 101 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' :) –

相關問題