pythonfirebaseflaskgoogle-cloud-storagepyrebase

How to delete a image file from Google firebase Storage using python


I am currently in the process of creating a Web App (Flask) where users can log in and upload photos to Google Firebase. At one point in the code I was initially saving user uploaded photos to a local folder, but when deployed this doesn't work correctly so I decided to temporarily store it in Google Storage, analyze the faces in it, then delete it. However, I am unable to delete it from Google Storage for some reason.

Firebase Initilizations:

import pyrebase
from pyrebase.pyrebase import storage 
import firebase_admin
from firebase_admin import storage, credentials

firebase = pyrebase.initialize_app(json.load(open('firebase/firebaseConfig.json')))
auth = firebase.auth()
db = firebase.database()                                    
storage = firebase.storage()

I have not needed to delete the photos in storage before, but I am able to store Images as well as retrieve their URLs for download as seen below. I am certain the image is stored in my Google Storage and the try fails when I attempt the storage.delete()

try:
   storage.child("images/temp/" + filename).put(image, userIdToken)
   current_app.logger.info("[UPLOAD-IMAGE] Photo saved, grabbing url")
   imageURL = storage.child("images/temp/" + filename).get_url(None)

   anazlyzeInfo = recognize.facialRecognition(imageURL)

   delete_temp_image_path = "images/temp/" + filename
   #storage.delete(imageURL) # same error happens when URL is passed
   storage.delete(delete_temp_image_path)

The error described in the exception is: 'Storage' object has no attribute 'bucket'

I looked into this for a while and tried other solutions like StorageRef and was met with the error 'Storage' has no attribute 'ref'.

I also tried A Service Account following the Google Admin SDK setup but am not sure what to do now that I have:

cred = credentials.Certificate(json.load(open('firebase/fiddl-dev-firebase-adminsdk-stuffdnsnfsnfk.json')))
admin = firebase_admin.initialize_app(cred)

I tried working with this for a while but I could not figure out what was callable with admin.

Was I on the correct path with either of the two fixes I attempted? My use of Firebase was pretty low level before and I would think that deleting would be the same. Thanks!


Solution

  • I’m the OP and I figured out my issue! This GitHub post helped me learn that you need to "add a service account to the config" when getting the 'storage' has not 'bucket' error.

    To do this I followed the Firebase Admin Documentation which was pretty straight forward.

    However there were 2 main fixes I needed. I fixed this using this Stackoverflow post as a guide.

    The first was adding my storageBucket for my app which I was missing above.

    admin = firebase_admin.initialize_app(cred, {
          'storageBucket': 'fiddl-dev.appspot.com'})
    

    The second issues was when I was trying the bucket = storage.bucket() seen in the same [Stackoverflow] post I was getting the error that storage didn’t have an attribute bucket. This I couldn’t find anything on and was why I made the post.

    At the top of my file.py I import:

    import pyrebase
    from pyrebase.pyrebase import storage  
    import firebase_admin
    from firebase_admin import storage as admin_storage, credentials, firestore
    

    The key being that I added import storage as admin_storage rather than what I had import storage. Essentially I was importing a module named storage twice and it was getting confused.

    With that last change I was now able to test the following code that deleted the image from the filepath in Google Firebase Storage specified.

    bucket = admin_storage.bucket()
    blob = bucket.blob('images/temp/pumpkin.jpg')
    print(blob)
    blob.delete()