Cloud Storage Tutorial

This simple tutorial demonstrates writing, deploying, and triggering a Background Cloud Function with a Cloud Storage trigger.



This tutorial uses billable components of Cloud Platform, including:

  • Google Cloud Functions
  • Google Cloud Storage

Use the Pricing Calculator to generate a cost estimate based on your projected usage.

New Cloud Platform users might be eligible for a free trial.

Before you begin

  1. Sign in to your Google account.

    If you don't already have one, sign up for a new account.

  2. Select or create a Cloud Platform project.

    Go to the Projects page

  3. Enable billing for your project.

    Enable billing

  4. Enable the Cloud Functions and Cloud Storage APIs.

    Enable the APIs

  5. Install and initialize the Cloud SDK.
  6. Update and install gcloud components:
    gcloud components update beta &&
    gcloud components install

Preparing the application

  1. Create a Cloud Storage bucket to stage your Cloud Functions files, where [YOUR_STAGING_BUCKET_NAME] is a globally-unique bucket name:

    gsutil mb gs://[YOUR_STAGING_BUCKET_NAME]

  2. Create a Cloud Storage bucket to upload a test file, where [YOUR_UPLOAD_BUCKET_NAME] is a globally-unique bucket name:

    gsutil mb gs://[YOUR_UPLOAD_BUCKET_NAME]

  3. Create a directory on your local system for the application code:

    • Linux or Mac OS X:

      mkdir ~/gcf_gcs
      cd ~/gcf_gcs
    • Windows

      mkdir %HOMEPATH%\gcf_gcs
      cd %HOMEPATH%\gcf_gcs
  4. Create an index.js file in the gcf_gcs directory with the following contents:


     * Background Cloud Function to be triggered by Cloud Storage.
     * @param {object} event The Cloud Functions event.
     * @param {function} The callback function.
    exports.helloGCS = function helloGCS (event, callback) {
      const file =;
      const isDelete = file.resourceState === 'not_exists';
      if (isDelete) {
        console.log(`File ${} deleted.`);
      } else {
        console.log(`File ${} uploaded.`);

    The helloGCS function is exported by the module and is executed when an object is added to the bucket, deleted from the bucket, or its metadata changes.

Deploying the function

To deploy the helloGCS function with a Cloud Storage trigger, run the following command in the gcf_gcs directory:

gcloud beta functions deploy helloGCS --stage-bucket [YOUR_STAGING_BUCKET_NAME] --trigger-bucket [YOUR_UPLOAD_BUCKET_NAME]


  • [YOUR_STAGING_BUCKET_NAME] is the name of your staging Cloud Storage Bucket.
  • [YOUR_UPLOAD_BUCKET_NAME] is the name of your Cloud Storage Bucket where you will upload a test file.

Triggering the function

  1. Create an empty gcf-test.txt file in the gcf_gcs directory.

  2. Upload the file to Cloud Storage in order to trigger the function:

    gsutil cp gcf-test.txt gs://[YOUR_UPLOAD_BUCKET_NAME]


    • [YOUR_UPLOAD_BUCKET_NAME] is the name of your Cloud Storage Bucket where you will upload a test file.
  3. Watch the logs to be sure the executions have completed:

    gcloud beta functions logs read --limit 50

Cleaning up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this tutorial:

Deleting the project

The easiest way to eliminate billing is to delete the project you created for the tutorial.

To delete the project:

  1. In the Cloud Platform Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click
      Delete project
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Deleting the Cloud Functions

Deleting the Cloud Functions removes all Container Engine resources, but you need to manually remove any resources in Cloud Storage.

Delete a Cloud Function:

gcloud beta functions delete helloGCS

You can also delete Cloud Functions from the Google Cloud Platform Console.

Send feedback about...

Cloud Functions Documentation