[Python] firebase storage에 사진 저장하고 읽기

bluejoy·2023년 3월 20일
0

파이썬

목록 보기
4/4

개요

  • 간단한 개발용 서버를 제작하기 위해 파이썬으로 서버를 만들었는데, firebase storage의 문서가 좀 부실했다.

    관리 챕터에 기타 서버 언어들이 들어있는데 시작하기만 적혀있다 ㅠㅠ

  • 다행히도 정보를 찾아보니 google cloud storage 문서에 불친절하게 적혀있었다.

코드

  • 나는 이미지를 생성해서 메모리에 있는 이미지를 저장하고 싶었다.
from firebase_admin import credentials, initialize_app, storage
from PIL import Image

cred = credentials.Certificate(# 인증 파일)
initialize_app(cred, {
    'storageBucket': # gs://가 없는 경로 - 붙이면 에러남
})
bucket = storage.bucket()

app = FastAPI()

@app.post("/image")
def post_image():
    # 이미지 생성
    img: Image.Image = text_to_image(content)
    # 이미지 저장
    blob = bucket.blob('image/test.png') # 이미지 경로 설정
    bs = io.BytesIO()
    img.save(bs, "png")
    blob.upload_from_string(bs.getvalue(), content_type="image/png") # 이미지 저장

    return True
profile
개발자 지망생입니다.

0개의 댓글