[Python] Python Flask Backend (Cloud Run) - Firebase Admin SDK fails with "404 The database...

Discussão em 'Python' iniciado por Stack, Agosto 3, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Google Cloud project 'gestionale-cantieri-app-12345'. My React frontend application (deployed on Firebase Hosting) successfully connects to and writes data to the Firestore (default) database in this project. I can see the data in the Firebase Console.

    However, my Python Flask backend service (deployed on Google Cloud Run in europe-west1) fails to connect to Firestore when attempting to read data from the reports collection. It returns an HTTP 500 error with the specific message:


    404 The database (default) does not exist for project gestionale-cantieri-app-12345 Please visit https://console.cloud.google.com/datastore/setup?project=gestionale-cantieri-app-12345 to add a Cloud Datastore or Cloud Firestore database.
    ​

    This error occurs both when deployed on Cloud Run and when running the Flask backend locally on my machine.

    What I have already verified:

    1. Firestore Database Existence: The (default) Firestore database clearly exists in my project. I can see it and its data (including the reports collection) in the Firebase Console.
    2. Database Region: The (default) Firestore database is located in europe-west1, which is the same region as my Cloud Run service.
    3. Service Account Credentials:
      • I have downloaded the serviceAccountKey.json file for the firebase-adminsdk-fbsvc@gestionale-cantieri-app-12345.iam.gserviceaccount.com service account.
      • This file is placed in the same directory as my backend_cantieri.py script.
      • I have verified the content of the serviceAccountKey.json file, and it is valid.
    4. Service Account Permissions (IAM):
      • The service account firebase-adminsdk-fbsvc@gestionale-cantieri-app-12345.iam.gserviceaccount.com has the "Owner" role for the entire project, which should grant it full access to Firestore.
    5. API Enablement: The "Cloud Firestore API" is enabled in "APIs & Services -> Library" for my project.
    6. Firebase Admin SDK Initialization: When running the Flask app locally, the console prints Firebase Admin SDK inizializzato con successo., indicating that firebase_admin.initialize_app(cred) succeeds. The error occurs when firestore.client().collection('reports').stream() is called.
    7. Cloud Run Logs: I have extensively checked Cloud Run logs. While I see stderr logs (e.g., Flask development server warnings, debugger active, and the GET /api/export-reports-csv HTTP/1.1" 500 - access log), I cannot find any textPayload or jsonPayload containing the detailed Python traceback or the specific error message Errore Firebase durante l'esportazione dei report: that my code explicitly prints in the except block. This makes debugging on Cloud Run impossible.

    My backend_cantieri.py code (relevant parts):

    # Incolla qui il codice completo del tuo backend_cantieri.py
    # Assicurati che sia leggibile e formattato correttamente con i blocchi di codice
    #
    http://googleusercontent.com/immersive_entry_chip/0


    My serviceAccountKey.json (partial - replace sensitive parts with ...): JSON

    {
    "type": "service_account",
    "project_id": "gestionale-cantieri-app-12345",
    "private_key_id": "...",
    "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
    "client_email": "firebase-adminsdk-fbsvc@gestionale-cantieri-app-12345.iam.gserviceaccount.com",
    "client_id": "...",
    "auth_uri": "...",
    "token_uri": "...",
    "auth_provider_x509_cert_url": "...",
    "client_x509_cert_url": "...",
    "universe_domain": "googleapis.com"
    }


    Question: What could be causing the Firebase Admin SDK to report "404 The database (default) does not exist" when the database clearly exists and is accessible by the client SDK, and the service account has the Owner role? Why are detailed Python tracebacks not appearing in Cloud Run logs?

    What I have tried and what I expected:

    1. Verified Firestore database existence and region: I expected the backend to connect successfully. Instead, the backend still reports "404 The database (default) does not exist", even though the database is visible in the Firebase Console and located in europe-west1 (same as Cloud Run service).
    2. Verified serviceAccountKey.json: I ensured the serviceAccountKey.json is valid and located in the same directory as backend_cantieri.py. I expected the Firebase Admin SDK to initialize correctly. It does initialize correctly locally (prints "Firebase Admin SDK inizializzato con successo."), but the connection to Firestore still fails.
    3. Verified Service Account IAM permissions: The service account firebase-adminsdk-fbsvc@gestionale-cantieri-app-12345.iam.gserviceaccount.com has the "Owner" role for the project. I expected it to have full access to Firestore.
    4. Verified Cloud Firestore API enablement: The "Cloud Firestore API" is enabled in the Google Cloud Console. I expected this to ensure backend access.
    5. Modified backend code for more explicit logging: I added print statements in the except blocks (e.g., ERRORE FIRESTORE:, ERRORE GENERICO:) to get more detailed error messages. I expected these detailed messages or a full Python traceback to appear in Cloud Run logs.
    6. Performed multiple Cloud Run deploys: I deployed the backend multiple times (gcloud run deploy --source .) to ensure the latest code and configuration are active. I expected the new deployments to resolve the issue or at least provide better logs.
    7. Attempted local Flask backend execution: I ran python backend_cantieri.py locally and accessed the /api/export-reports-csv endpoint via browser. I expected to see a full Python traceback in the local terminal to pinpoint the error. While the local execution did print the same 404 error message, it did not produce a full Python traceback, which is highly unusual.
    8. Extensive Cloud Run log investigation: I have thoroughly checked Cloud Run logs (filtering by stderr, ERROR severity, and specific text searches). I can see Flask's own logs (development server warnings, debugger active, HTTP access logs like GET /api/export-reports-csv HTTP/1.1" 500 -), but I cannot find any textPayload or jsonPayload containing the detailed Python traceback or the explicit error messages from my except blocks. This prevents effective debugging on Cloud Run.

    Expected outcome: I expect the backend to successfully retrieve data from the Firestore reports collection and return it as a CSV.

    Continue reading...

Compartilhe esta Página