I am creating a Python FastAPI server which will be uploading data to Firebase Storage using pyrebase4
.
Following is the code of how I will be initializing the Firebase instance (directly taken from pyrebase4 docs):
import pyrebase
config = {
"apiKey": "apiKey",
"authDomain": "projectId.firebaseapp.com",
"databaseURL": "https://databaseName.firebaseio.com",
"storageBucket": "projectId.appspot.com"
}
firebase = pyrebase.initialize_app(config)
Now how will I manage multiple instances of this firebase
object?
Should I keep it’s instance as a global object? What is the best approach to handle this, considering that there will be multiple users trying to access routes concurrently.
Also, if I keep the instance at a global level, then it will be creating a new instance each time. Do I need to close the connection.
I am confused, could not find anything relevant in their docs. Please guide. Thanks.
If I use DI in FastAPI, and ‘inject’ this instance with a function handling a request and it’s uploading some data, and assume a new request comes in from another user, will the newer request have to wait till the first request completes?
def return_storage():
print("I have been called")
return firebase.storage()
@app.get("/test1")
async def testing_1(storage = Depends(return_storage)):
storage.child("user1/video.mp4").put("video1.mp4")
@app.get("/test2")
async def testing_2(storage = Depends(return_storage)):
storage.child("user2/video.mp4").put("video1.mp4")