This project implements a serverless Cloud Function that automatically processes text files uploaded to a Google Cloud Storage (GCS) bucket. This project implements a serverless Cloud Function that automatically processes text files uploaded to a Google Cloud Storage (GCS) bucket. Whenever a new .txt file is uploaded, the function:
Downloads the file temporarily to /tmp/.
Finds the longest line(s) in the file.
Saves those lines in a new output file named <original_filename>_longest.txt.
Uploads this result file back to the same GCS bucket.
The function ignores hidden files (.filename), non-text files (.pdf, .jpg, etc.), and result files ending with _longest.txt.
| Component | Description |
| Google Cloud Function (2nd Gen) | A serverless compute service that automatically scales in response to events. |
| Eventarc Trigger (GCS Event) | Automatically invokes the function whenever a new file is created in the GCS bucket. |
| Cloud Storage | Used to store both input .txt files and output _longest.txt result files. |
| Functions Framework | Enables writing portable and trigger-based Python functions compatible with Cloud Functions.|
-
Understand how event-driven architectures work in GCP.
-
Learn to handle Cloud Storage events with Cloud Functions.
-
Implement file I/O operations in a secure, ephemeral environment (/tmp/).
-
Use structured logging for debugging and observability.
-
Design a clean, modular function using the functions_framework decorator.
π¦ longest-line-detector
βββ main.py # Function source code
βββ requirements.txt # Dependencies
βββ README.md # Project documentation
-
Go to β Google Cloud Console β Cloud Functions .
-
Click Create Function.
-
Under Environment, select 2nd Gen.
-
Name: longest-line-detector
-
Trigger type: Eventarc trigger β Cloud Storage β Finalize/Create event (select your bucket, e.g. ibd-week3)
-
Runtime: Python 3.10 or later
-
Entry point: hello_gcs
-
Source code: Inline editor
-
Paste the above main.py code
-
Add dependencies in requirements.txt:
google-cloud-storage
functions-framework
- Click Deploy and wait for confirmation.
-
Upload a .txt file to the GCS bucket (e.g., sample.txt).
-
Wait a few seconds for the function to trigger.
-
Youβll see logs like: