Skip to content

Commit

Permalink
Merge pull request #36 from Aniket-Parlikar/Firestore_delete_script
Browse files Browse the repository at this point in the history
Added new files to firestore folder
  • Loading branch information
Aniket-Parlikar authored Aug 16, 2022
2 parents 8cedcbb + f4b942f commit ab9c573
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
11 changes: 10 additions & 1 deletion deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ gcloud functions deploy trigger-street2sat \
--runtime=python39 \
--entry-point=hello_gcs \
--set-env-vars INFERENCE_HOST="$URL" \
--timeout=300s
--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
34 changes: 34 additions & 0 deletions gcp/delete-street2sat-prediction/main.py
Original file line number Diff line number Diff line change
@@ -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!.')
3 changes: 3 additions & 0 deletions gcp/delete-street2sat-prediction/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Function dependencies, for example:
# package>=version
google-cloud-firestore

0 comments on commit ab9c573

Please sign in to comment.