Hello,
I'd like to ask your advice. I am trying to add a trigger to a firebase function in Python that will respond to uploading a new image to the firebase storage bucket. The purpose of this function is to retrieve this newly uploaded image and upload it again to a Google Cloud bucket that is not in firebase, but when I deploy the function and upload the new image to firebase, it doesn't work, it doesn't appear in the Google Cloud bucket.
I would be very grateful for any advice.
Thank you very much.
Here is my entire program that I uploaded to firebase using the "firebase deploy" command.
from firebase_functions import storage_fn
from firebase_admin import initialize_app, storage
from google.oauth2 import service_account
initialize_app()
def upload_to_bucket(bucket_name, bytes_data, destination_blob_name):
credentials = service_account.Credentials.from_service_account_file(
'path/to/credentials.json'
)
storage_client = storage.Client(credentials=credentials)
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_string(bytes_data)
@storage_fn.on_object_finalized(bucket="myproject-ae52c.appspot.com")
def convert_to_binary(event: storage_fn.CloudEvent[storage_fn.StorageObjectData]):
"""When a new object is uploaded to Cloud Storage, download it and
convert it to a binary string, then save it to a new bucket."""
source_bucket_name = event.data.bucket
file_path = event.data.name
source_bucket = storage.bucket(source_bucket_name)
blob = source_bucket.blob(file_path)
file_bytes = blob.download_as_bytes()
my_file_bytes = file_bytes
bucket_name = 'bucket-name'
destination_blob_name = 'uploaded_image'
upload_to_bucket(
bucket_name=bucket_name,
bytes_data=my_file_bytes,
destination_blob_name=destination_blob_name
)