firebase / firebase-admin-python

Firebase Admin Python SDK
https://firebase.google.com/docs/admin/setup
Apache License 2.0
988 stars 300 forks source link

import firebase admin #785

Closed atom2-source closed 2 months ago

atom2-source commented 2 months ago

[REQUIRED] Step 2: Describe your environment

[REQUIRED] Step 3: Describe the problem

when running cloud run it fails to import firebase admin

Steps to reproduce:

What happened? How can we make the problem occur?

errorGroups: [1] insertId: "661c7cf1000cfff511dfefb7" labels: {1} logName: "projects/rayneo-29da0/logs/run.googleapis.com%2Fstderr" receiveTimestamp: "2024-04-15T01:03:45.856897602Z" resource: {2} severity: "ERROR" textPayload: "Traceback (most recent call last): File "/app/app.py", line 2, in import firebase_admin " timestamp: "2024-04-15T01:03:45.851957Z"

This could be a description, log/console output, etc.

Relevant Code:

Flask==3.0.3 requests==2.31.0 debugpy==1.8.1 google-cloud-storage==2.16.0 google-cloud-firestore==2.16.0

from google.cloud import storage import firebase_admin from firebase_admin import firestore import logging from flask import Flask, request, jsonify

Initialize Firebase Admin SDK

firebase_admin.initialize_app()

app = Flask(name)

def move_file_and_delete(file_path): source_bucket_name = 'removed bucket'
destination_bucket_name = 'removed bucket'

storage_client = storage.Client()
db = firestore.client()  # Firestore client for deleting metadata

try:
    source_bucket = storage_client.get_bucket(source_bucket_name)
    source_blob = source_bucket.blob(file_path)
    destination_bucket = storage_client.get_bucket(destination_bucket_name)

    blob_copy = source_bucket.copy_blob(
        source_blob, destination_bucket, file_path
    )
    logging.info(f"Blob {file_path} copied to gs://{destination_bucket_name}/{file_path}")

    source_blob.delete()
    logging.info(f"Blob {file_path} deleted from the Firebase bucket.")

    # Delete Firestore metadata (adjust as needed)
    doc_ref = db.collection('your_metadata_collection').document('relevant_doc_id')
    doc_ref.delete()

except Exception as e:
    logging.error(f"An error occurred: {e}")

@app.route('/', methods=['POST'])
def endpoint(): data = request.get_json() file_path = data.get('path')

if not file_path:
    return 'File path not found in Firestore data.', 400 

move_file_and_delete(file_path) 
return 'Success', 200

if name == 'main': app.run(debug=True, host='0.0.0.0', port=8080)

// TODO(you): code here to reproduce the problem
google-oss-bot commented 2 months ago

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

atom2-source commented 2 months ago

I was moving the files from firestore cloud bucket to my google cloud to process the images with cloud vision to keep the photos in my firestore bucket for as short as possible to make it harder for anyone to access as its the only entry point I set the cloud run up in google cloud but I might just use the moderated images example in the firestore functions github page and move them to another firestore bucket with a firestore function if using cloud run from gcp console is too problematic.

atom2-source commented 2 months ago

Didn't mean to close this