Cloud Storage Triggers

With Cloud Storage Triggers, you can execute a Firebase Cloud Function in response to create, update, or delete operation of files and folders in Cloud Storage. We can create a new function for Cloud Storage events with functions.storage. Depending on the requirement, you can have a function that listens for all changes on the default storage bucket, or you can restrict it to a specific bucket by specifying the bucket name:

functions.storage.object() - listen for object changes on the default storage bucket.
functions.storage.bucket('test').object() - listen for object changes on a specific bucket called 'test'

For example, we can write a function that compresses the uploaded files to reduce the size:

exports.compressFiles = functions.storage.object().onChange(event => {
// ...
});

The change event gets triggered whenever an object is created, modified, or deleted.

The following attributes are exposed by Cloud Storage functions, which can be used to do the further processing of files:

  • event.data: Represents the storage object.
  • event.data.bucket: The storage bucket inside which the file is stored.
  • event.data.name: The file path in the bucket.
  • event.data.contentType: The file content type.
  • event.data.resourceState: Two possible values: exists or not_exists. The not_exists value is set if the file / folder has been deleted.
  • event.data.metageneration: Number of times the metadata of the file has been generated; for new objects, the initial value is 1.

A most common use case for a Firebase Cloud Function is to further process a file, for example, compress the file or generate thumbnails of an image file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset