파이썬을 사용한 Firebase Storage 작업

배경.


Firestore를 사용하면 일주일에 한 번씩 대량의 데이터 액세스 위치를 만들면 Storage로 문제가 없는 것을 알아볼 수 있습니다.

결론


import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
#from firebase_admin import storage
from google.cloud import storage

GOOGLE_APPLICATION_CREDENTIALS=os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
FIREBASE_STORAGE_BUCKET=os.environ["FIREBASE_STORAGE_BUCKET"]

cred = credentials.Certificate(GOOGLE_APPLICATION_CREDENTIALS)
firebase_admin.initialize_app(cred,{'storageBucket':FIREBASE_STORAGE_BUCKET})
bucket = storage.bucket()

def upload_blob(source_file_name, destination_blob_name):
    storage_client = storage.Client()
    bucket = storage_client.bucket(FIREBASE_STORAGE_BUCKET)
    blob = bucket.blob(destination_blob_name)
    blob.upload_from_filename(source_file_name)
    print(
        "File {} uploaded to {}.".format(
            source_file_name, destination_blob_name
        )
    )
def download_blob(source_blob_name, destination_file_name):
    storage_client = storage.Client()
    bucket = storage_client.bucket(FIREBASE_STORAGE_BUCKET)
    blob = bucket.blob(source_blob_name)
    blob.download_to_filename(destination_file_name)
    print(
        "Blob {} downloaded to {}.".format(
            source_blob_name, destination_file_name
        )
    )

def blob_list(prefix=""):
    storage_client = storage.Client()
    bucket = storage_client.bucket(FIREBASE_STORAGE_BUCKET)
    blobs = list(bucket.list_blobs(prefix=prefix))
    print(
        "Blob list to {}.".format(
            blobs
        )
    )
    return blobs
blob_list에서 prefix를 지정하면 특정 폴더 아래의 파일만 추출할 수 있습니다.
아래 내용을 참고하시오.
참조: https://googleapis.dev/python/storage/latest/client.html

사용법


환경 변수FIREBASE_STORAGE_BUCKET에 통 이름을 설정하고GOOGLE_APPLICATION_CREDENTIALS에 Firebase에서 다운로드한 인증 파일을 설정하십시오.

통 이름


Firebase store 위쪽에 있는 문자열입니다.

이 경우 제거gs://Video-xxxx.appspot.com이다.

Key 파일


1. プロジェクトの概要 측면에서 선택하십시오プロジェクトを設定.

2. 위 탭에서 서비스 계정을 선택

3. 아래新しい秘密鍵の生成를 누르면 JSON 파일을 다운로드할 수 있습니다.

좋은 웹페이지 즐겨찾기