funct7
funct7

Reputation: 3601

Firebase functions SDK 1.0.0 storage.onFinalize() not working as expected

I use Firebase function to detect an upload event to Storage and write the download URL to the database like so:

exports.processFile = functions.storage.object().onChange((event) => {
  const object = event.data,
        filePath = object.name;

  if (object.resourceState === 'exists') {
    // Do something
  }
}

Getting an email about the SDK 1.0 I updated the function like this:

exports.processFile = functions.storage.object().onFinalize((object, context) => {
  const filePath = object.name;

  if (object.resourceState === 'exists') {
    // Do something
  }
}

After the update, the function broke so I decided to print the resourceState in the log and got an undefined. What is going on?

Upvotes: 3

Views: 1594

Answers (1)

Bob Snyder
Bob Snyder

Reputation: 38299

Before SDK 1.0 there was only the onChange() event. It fired for all changes to the storage object and one had to test resourceState in the object metadata to determine if the change was a creation, update, or deletion.

In SDK 1.0, onChange() has been replaced by four events, eliminating the need to test the value of resourceState:

  • onArchive()
  • onDelete()
  • onFinalize()
  • onMetadataUpdate()

Although the documentation for ObjectMetadata still shows resourceState, my guess is that it has been removed from SDK 1.0 because it is no longer needed.

Upvotes: 5

Related Questions