0

I am growing through Python, to read a JSON file present on Firebase Storage via the admin sdk. I have already read the following question

How to download file from firebase storage in python using firebase_admin

but in this case the file is downloaded to another file. Is it possible instead to read it without downloading it and without making it public? Or alternatively is it possible to make a temporary file and once python prints it deletes it? Thank you

My code:

import firebase_admin
from firebase_admin import credentials, storage
cred = credentials.Certificate('serviceAccount.json')
firebase_admin.initialize_app(cred, {
    "storageBucket": "**.appspot.com"
})
source_blob_name = "UQ4ADY.json"
#The path to which the file should be downloaded
destination_file_name = r"temp\ftmp.json"

bucket = storage.bucket()
blob = bucket.blob(source_blob_name)
blob.download_to_filename(destination_file_name)

Be aware that there may be a lot of requests and that is why I prefer not to download the files

Matteo
  • 118
  • 2
  • 12
  • Read the object into memory **download_as_string()**, then print the string. – John Hanley Feb 20 '22 at 16:35
  • Perfect, it works but I used `download_as_text` so I can convert it to json. If you submit this comment as an answer I sign it as a solution. – Matteo Feb 20 '22 at 19:59

1 Answers1

1

After a comment by Johm Hanley, i used download_as_text funcion:

import firebase_admin
from firebase_admin import credentials, storage
cred = credentials.Certificate('serviceAccount.json')
firebase_admin.initialize_app(cred, {
    "storageBucket": "**.appspot.com"
})
source_blob_name = "UQ4ADY.json"
#The path to which the file should be downloaded
destination_file_name = r"temp\ftmp.json"

bucket = storage.bucket()
blob = bucket.blob(source_blob_name)
data = blob.download_as_text()
Matteo
  • 118
  • 2
  • 12