1
votes

I have a cloud function which is set to be triggered by a cloud storage bucket.

Is there anyway to tell if an event is set off by a newly uploaded object or rather an overwritten object?

I tried to console.log out the event object but nothing seems to be indicative of whether or not the object has been overwritten.

I do notice that there exists an "overwroteGeneration" attribute in Cloud Pub/Sub Notifications which the trigger event here is based on, but it's not available here.

2

2 Answers

1
votes

As staged by @Doug Stevenson, it looks like there is no easy way to achieve this. Moreover, I have been playing around with metageneration and, according to this example in the documentation (under "You upload a new version of the image "), when an object is overwritten (even when versioning is enabled), it will get its own new generation and metageneration numbers, reason why, as you commented in the comment to the other answer, metageneration = 1 in such a scenario.

The only workaround I see, and which may not satisfy your specific requirements, is using two Cloud Functions (let's call them funcA and funcB), one (funcA) that identifies object creation with google.storage.object.finalize and another one (funcB) that detects object overwriting with google.storage.object.archive or google.storage.object.delete (depending if you are using versioning or not, respectively). In this case, funcA would be triggered twice, because for object overwritting it will be triggered too, but depending on your use case, you can identify the proximity in time of the create and delete events and detect that these detect to a single event:

funcA logs: enter image description here

funcB logs: enter image description here

I know this does not exactly solve the question you posted, but I do not think there is any way to identify, using a single Cloud Function, that an object has been either created or overwritten, it looks like you will need one Cloud Function for each of those procedures.

0
votes

In order to determine the nature of the file upload (new file upload rather than existing file upload), you need to use the event and delivered to the function to figure it out.

You'll need to use an Object Finalize event type. As you can see from the documentation there:

This event is sent when a new object is created (or an existing object is overwritten, and a new generation of that object is created) in the bucket.

What's not so clear from that doc is that the metageneration combined with the resourceState property of the event is the indicator.

The documentation here should be clear about how to use metageneration along with resourceState to determine if a change to a bucket is a new file or a replaced file:

The resourceState attribute should be paired with the 'metageneration' attribute if you want to know if an object was just created. The metageneration attribute is incremented whenever there's a change to the object's metadata. For new objects, the metageneration value is 1.