Skip to content

This project implements a serverless Cloud Function that automatically processes text files uploaded to a Google Cloud Storage (GCS) bucket.

Notifications You must be signed in to change notification settings

ompodey/-Longest-Line-Detector-using-Google-Cloud-Functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Longest-Line-Detector-using-Google-Cloud-Functions

πŸ“˜ Overview

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.

☁️ Key Concepts Used

| 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.| 

Learning Objectives

  • 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.

🧩 Project Structure

πŸ“¦ longest-line-detector
β”œβ”€β”€ main.py              # Function source code
β”œβ”€β”€ requirements.txt     # Dependencies
β”œβ”€β”€ README.md            # Project documentation

βš™οΈ Deployment Steps (via GCP Console UI)

  • 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.

πŸ§ͺ Testing

  • 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:

Init logs

About

This project implements a serverless Cloud Function that automatically processes text files uploaded to a Google Cloud Storage (GCS) bucket.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages