Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new files to firestore folder #36

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions scripts/firestore_delete/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Inline comments should start with space: https://peps.python.org/pep-0008/#inline-comments


import logging
from google.cloud import firestore

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
db = firestore.Client()

def firestore_delete(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!')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the logs end with punctuation and some don't, best to be consistent

3 changes: 3 additions & 0 deletions scripts/firestore_delete/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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the package not available by default in the Cloud Functions environment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it is needed to be mentioned in the requirements.txt file, else we get an error