Package: csfutils
cloud-storage-for-firebase-utilsPython utility for Cloud Storage for Firebase.What is This?In order to useGoogle Cloud Storagewith theFirebaseframework, you have to:Give an access token to uploaded files,Publish a URL with an access token for the domainfirebasestorage.googleapis.com.As a further application, you may want to grant a new access token, remove an existing access token, or in a real use case, get agoogle.cloud.storage.Blobinstance from a URL.Unfortunatelygoogle-cloud-storagepackage does not provide the functions for them.But by usingcloud-storage-for-firebase-utilsyou can easily achieve them👍.RequirementsPython >= 3.6google-cloud-storage>= 1.35.0Quick StartFirst, install bypip intall csfutils.
Second, prepare a target filestars⭐.jpg.
Finally, run below code❗fromgoogle.cloud.storageimportBlob,Bucket,Client# Import package# ==============importcsfutils# Initialize google.cloud.storage.Client, Bucket and Blob instances# =================================================================storage:Client=Client()bucket:Bucket=storage.bucket("example-project.appspot.com")# PUT YOUR BUCKET NAMEblob:Blob=bucket.blob("images/stars⭐.jpg")# PUT PATH ON CLOUD STORAGE YOU WANT# 🔥Upload "./stars⭐.jpg" to Cloud Storage for Firebase# ======================================================uploaded_url:str=csfutils.upload_from_filename_for_firebase(blob,"./stars⭐.jpg")print(f"uploaded_url={uploaded_url}")# --> uploaded_url=https://firebasestorage.googleapis.com/v0/b/example-project.appspot.com/o/images%2Fstars%E2%9C%A7.jpg?alt=media&token=f7d0815d-96f8-4907-b22c-70ad9e38d7ff# csfutils.upload_from_file_for_firebase() and csfutils.upload_from_string_for_firebase() also exist.# 🔥Add, get and delete an access token# =====================================current_access_token=csfutils.get_access_token(blob)asserttype(current_access_token)isstrprint(f"current_access_token={current_access_token}")# --> current_access_token=f7d0815d-96f8-4907-b22c-70ad9e38d7ffnew_access_token:str=csfutils.add_access_token(blob)print(f"new_access_token={new_access_token}")# --> new_access_token=e0d97b72-44c3-415d-8d88-1e3aeae2fc28access_tokens=csfutils.get_access_token(blob)assertisinstance(access_tokens,list)print(f"access_tokens={access_tokens}")# --> current_access_token=['f7d0815d-96f8-4907-b22c-70ad9e38d7ff','e0d97b72-44c3-415d-8d88-1e3aeae2fc28']csfutils.delete_access_token(blob,new_access_token)print(f"latest_access_token={csfutils.get_access_token(blob)}")# --> latest_access_token=f7d0815d-96f8-4907-b22c-70ad9e38d7ff# 🔥Get google.cloud.storage.Blob instance from URL# =================================================blob_ref_from_url:Blob=csfutils.ref_from_url(storage,"https://firebasestorage.googleapis.com/v0/b/example-project.appspot.com/o/images%2Fstars%E2%9C%A7.jpg?alt=media&token=f7d0815d-96f8-4907-b22c-70ad9e38d7ff")# --> blob_ref_from_url == storage.bucket("example-project.appspot.com").get_blob("images/stars✧.jpg")Bonus Trackfromcsfutils# BONUS 1: Get google.cloud.storage.Client instance# =================================================storage:Client=csfutils.init_storage("./your_service_account.json")# BONUS 2: Parse URL to bucket name & path# ========================================bucket_name,path=csfutils.parse_url("https://firebasestorage.googleapis.com/v0/b/example-project.appspot.com/o/images%2Fstars%E2%9C%A7.jpg?alt=media&token=f7d0815d-96f8-4907-b22c-70ad9e38d7ff")print(f"bucket_name={bucket_name},path={path}")# --> bucket_name=example-project.appspot.com,path=images/stars⭐.jpg# BONUS 3: Get 3 URLs: firebasestorage.googleapis.com, storage.googleapis.com and storage.cloud.google.com# ========================================================================================================blob:Blob=storage.bucket(bucket_name).blob(path)firestorage_url=csfutils.get_download_url(blob)print(f"firestorage_url={firestorage_url}")# --> firestorage_url=https://firebasestorage.googleapis.com/v0/b/example-project.appspot.com/o/images%2Fstars%E2%9C%A7.jpg?alt=media&token=f7d0815d-96f8-4907-b22c-70ad9e38d7ffpublic_url=csfutils.get_download_url(blob,csfutils.UrlType.PUBLIC_URL)print(f"public_url={public_url}")# --> firestorage_url=https://storage.googleapis.com/example-project.appspot.com/images/stars%E2%9C%A7.jpgauthenticated_url=csfutils.get_download_url(blob,csfutils.UrlType.AUTHENTICATED_URL)print(f"authenticated_url={authenticated_url}")# --> authenticated_url=https://storage.cloud.google.com/example-project.appspot.com/images/stars%E2%9C%A7.jpg# BONUS 4: Get GS path# ====================gs_path=csfutils.get_gs_path(blob)print(f"gs_path={gs_path}")# --> gs_path=gs://example-project.appspot.com/images/stars⭐.jpgLicenseMIT License