简体   繁体   中英

Python Firebase How to check whether a file exists on firebase storage

Hi I wondering whether it was possible to check whether a file exists on firebase storage using python. Here are a couple ways of doing it for other languages.

How to check if a file exists in Firebase storage from your android application?

how to check if file exists in Firebase Storage?

What is the equivalent method for python.

I was able to get it to work using the request module and using the public url for the file. Hope this helps anyone else facing the same issue.

import requests
from firebase_admin import storage


#Creates the blob reference to file on the server
bucket = storage.bucket()
blob = bucket.blob("firebase/path/to/file")

#Requests the file using the public url
r = requests.head(blob.public_url)
            
#Whether the file exists or not on the server
fileExists = (r.status_code == requests.codes.ok)

Edit

Thanks to some helpful pointers you can use

fileExists = blob.exists()

Which means you no longer require the requests module

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM