diff --git a/deploy.sh b/deploy.sh index f361b50..a4516d9 100644 --- a/deploy.sh +++ b/deploy.sh @@ -17,4 +17,13 @@ gcloud functions deploy trigger-street2sat \ --runtime=python39 \ --entry-point=hello_gcs \ --set-env-vars INFERENCE_HOST="$URL" \ - --timeout=300s \ No newline at end of file + --timeout=300s + +gcloud functions deploy delete-street2sat-prediction \ + --source=gcp/delete-street2sat-prediction \ + --trigger-event=google.storage.object.delete \ + --trigger-resource=$BUCKET \ + --allow-unauthenticated \ + --runtime=python39 \ + --entry-point=hello_gcs \ + --timeout=300s diff --git a/gcp/delete-street2sat-prediction/main.py b/gcp/delete-street2sat-prediction/main.py new file mode 100644 index 0000000..9b69be9 --- /dev/null +++ b/gcp/delete-street2sat-prediction/main.py @@ -0,0 +1,34 @@ +# The given script creates a CLoud Function to that is triggered when an image file is deleted from the street2sat-uploaded bucket +# and then proceeds to delete the corresponding record from the Firestore DB. +import logging +from google.cloud import firestore + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) +db = firestore.Client() + +def hello_gcs(event, context): + """Triggered by a change to a Cloud Storage bucket. + Args: + event (dict): Event payload. + context (google.cloud.functions.Context): Metadata for the event. + """ + #Getting the image name + img_path = event['name'] + + #Extracting the document name from the file path + doc_ref=img_path.replace("/","-").split(".")[0] + logging.info("The name of the document is "+doc_ref+".") + + #Getting the document reference + doc = db.collection('street2sat').document(doc_ref) + doc_check=doc.get() + + #Checking if the document reference exists in the collections and deleting it. + if doc_check.exists: + logger.info("The record associated with the image file exists in the Firestore DB.") + logger.info("Deleting the given document.") + doc.delete() + logger.info("Document has been deleted.") + else: + logger.info('No such record associated with the image file exists in the Firestore DB!.') diff --git a/gcp/delete-street2sat-prediction/requirements.txt b/gcp/delete-street2sat-prediction/requirements.txt new file mode 100644 index 0000000..072cbf9 --- /dev/null +++ b/gcp/delete-street2sat-prediction/requirements.txt @@ -0,0 +1,3 @@ +# Function dependencies, for example: +# package>=version +google-cloud-firestore \ No newline at end of file