Python: 将 Pillow Image 上传到 Firebase 存储桶
Python: upload Pillow Image to Firebase storage bucket
我正在尝试弄清楚如何将 Pillow
Image
实例上传到 Firebase 存储桶。这可能吗?
这是一些代码:
from PIL import Image
image = Image.open(file)
# how to upload to a firebase storage bucket?
我知道有一个 gcloud-python
库,但是它支持 Image
个实例吗?将图像转换为字符串是我唯一的选择吗?
gcloud-python
library is the correct library to use. It supports uploads from Strings, file pointers, and local files on the file system (see 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)
这是将枕头图像直接上传到 firebase 存储的方法
from PIL import Image
from firebase_admin import credentials, initialize_app, storage
# Init firebase with your credentials
cred = credentials.Certificate("YOUR DOWNLOADED CREDENTIALS FILE (JSON)")
initialize_app(cred, {'storageBucket': 'YOUR FIREBASE STORAGE PATH (without gs://)'})
bucket = storage.bucket()
blob = bucket.blob('image.jpg')
bs = io.BytesIO()
im = Image.open("test_image.jpg")
im.save(bs, "jpeg")
blob.upload_from_string(bs.getvalue(), content_type="image/jpeg")
我正在尝试弄清楚如何将 Pillow
Image
实例上传到 Firebase 存储桶。这可能吗?
这是一些代码:
from PIL import Image
image = Image.open(file)
# how to upload to a firebase storage bucket?
我知道有一个 gcloud-python
库,但是它支持 Image
个实例吗?将图像转换为字符串是我唯一的选择吗?
gcloud-python
library is the correct library to use. It supports uploads from Strings, file pointers, and local files on the file system (see 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)
这是将枕头图像直接上传到 firebase 存储的方法
from PIL import Image
from firebase_admin import credentials, initialize_app, storage
# Init firebase with your credentials
cred = credentials.Certificate("YOUR DOWNLOADED CREDENTIALS FILE (JSON)")
initialize_app(cred, {'storageBucket': 'YOUR FIREBASE STORAGE PATH (without gs://)'})
bucket = storage.bucket()
blob = bucket.blob('image.jpg')
bs = io.BytesIO()
im = Image.open("test_image.jpg")
im.save(bs, "jpeg")
blob.upload_from_string(bs.getvalue(), content_type="image/jpeg")