Triggering Cloud Storage With Cloud Functions in Java

Google Cloud Functions is a serverless execution environment for building and connecting cloud services. With Cloud Functions you write simple, single-purpose functions that are attached to events emitted from your cloud infrastructure and services. Your function is triggered when an event being watched is fired. Your code executes in a fully managed environment. There is no need to provision any infrastructure or worry about managing any servers.

  • Serverless execution environment for building and connecting cloud services
  • Write simple, single-purpose functions that are attached to events emitted from your cloud services
  • These functions execute code in response to events based on triggers

Key Terms used in Cloud Functions

Event

Events are the things that happen in the cloud environment. These can be:

  • changes to data in a database
  • files added to a storage system
  • a new virtual machine instance being created

Trigger

Declaration of interest in an event. We bind a function to a trigger and when trigger is set, function is executed.

Triggers available in Cloud Function

Triggers are associated with available resources such as Cloud storage, Cloud pub-sub, HTTP, Firebase Analytics, Firebase Authentication and Firebase real-time database.

Runtimes

Cloud Functions supports multiple language runtime:

Multiple language runtime for Cloud Functions

Triggering Cloud Storage with Cloud Functions

The idea is when an object is put in Google Cloud Storage, cloud function must be triggered and it will be logged likewise.

Step 1.) Create a Cloud Storage Bucket

Go to Navigation — Cloud Storage Give a unique bucket name

When GCS bucket is created, it looks like this.

Step 2.) Create a Cloud Function

Go to Navigation — Cloud Functions — Create Function

Step 3.) Selecting the Trigger and Event Type

After clicking Create Function, you must give a proper name of the Cloud Function and select the Trigger as Cloud Storage and Event type as Finalise/Create (this determines when object is put into the bucket, it will trigger cloud function).

Select Cloud Storage as the Trigger

Step 4.) Select the Google Cloud Storage bucket you want to trigger.

Selecting cloud-function-gcsbucket in the Browse-able Bucket field

Step 5.) Selecting the Runtime for running the Cloud Function

The Java programming language recently turned 25 years old, and it’s still one of the top-used languages powering today’s enterprise application customers. Google brought Java 11 to Google Cloud Functions in Q1 2020. That means you can now write Cloud Functions using your favorite JVM languages (JavaKotlinGroovyScala, etc) with our Functions Framework for Java, and also with Spring Cloud Functions and Micronaut!

With Cloud Functions for Java 11, now in beta, you can use Java to build business-critical applications and integration layers, and deploy the function in a fully managed environment, complete with access to resources in a private VPC network. Java functions will scale automatically based on your load. You can write HTTP functions to respond to HTTP events, and background functions to process events sourced from various cloud and GCP services, such as Pub/Sub, Cloud Storage, Firestore, and more.

So, you have to select Java 11 in the Runtime drop-down.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cloudfunctions</groupId>
  <artifactId>gcs-function</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <maven.compiler.target>11</maven.compiler.target>
    <maven.compiler.source>11</maven.compiler.source>
  </properties>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud.functions</groupId>
      <artifactId>functions-framework-api</artifactId>
      <version>1.0.1</version>
      <type>jar</type>
    </dependency>
  </dependencies>

  <!-- Required for Java 11 functions in the inline editor -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <excludes>
            <exclude>.google/</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

src/main/java/com/example/example.java

package com.example;

import com.example.Example.GCSEvent;
import com.google.cloud.functions.BackgroundFunction;
import com.google.cloud.functions.Context;
import java.util.logging.Logger;

public class Example implements BackgroundFunction<GCSEvent> {
  private static final Logger logger = Logger.getLogger(Example.class.getName());

  @Override
  public void accept(GCSEvent event, Context context) {
    logger.info("Processing file: " + event.name);
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public static class GCSEvent {
    String bucket;
    String name;
    String metageneration;
  }
}

Step 6.) Click the Create button for creating Cloud Function

Step 7.) Check and confirm that the Cloud Function is successfully created.

Step. 8) Check the View logs under Actions — 3-horizonatal-dots

When the function is created, you can view the logs to see that the function has been created.

Logs updated after triggerGCSEvent function is created

Step 9.) Upload a file in GCS bucket to trigger Cloud Functions

Uploaded an image, Sitting.jpeg in the GCS bucket

Step 10.) Check the logs after uploading the file in GCS bucket

Cloud Function got triggered when file is uploaded in the GCS object

Conclusion

Cloud Functions are a great fit for serverless application backends for integrating with third-party services and APIs, or for mobile or IoT backends. You can also use functions for real-time data processing systems, like processing files as they are uploaded to Cloud Storage, or to handle real-time streams of events from Pub/Sub. Last but not least, functions can serve intelligent applications like virtual assistants and chat bots, or video, image and sentiment analysis.

8 thoughts on “Triggering Cloud Storage With Cloud Functions in Java

  1. Good work.Keep going

    Liked by 1 person

  2. Thanks for sharing such a great post.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close