Cloud Storage functions

There are many cases where some action needs to be taken as a result of changes to objects in Cloud Storage, including processing new files, alerting other systems of changes, and performing some cleanup tasks on file deletions. Cloud Functions allow developers to do just that via Cloud Storage triggers. In this model, developers simply specify a given bucket to monitor, and the function will be invoked whenever an object is created, modified, or deleted.

In the case of Cloud Storage triggers, the event payload is in the form of a Cloud Storage API object model:

{
"data": {
"bucket": "<BUCKET_NAME>",
"contentType": "<CONTENT_TYPE>",
"crc32c": "EIwIPh==",
"etag": "CKeb6qzb+NzCEBE=",
"generation": "1517075690065311",
"id": "<BUCKET>/<FILENAME>/<GENERATION>",
"kind": "storage#object",
"md5Hash": "YjA3Njg1OcZjOGJmZGJjZGY2Y2JhMjU0YTUwODI1OTc=",
"mediaLink": "https://www.googleapis.com/download/storage/v1/b/...",
"metageneration": "1",
"name": "<FILE_NAME>",
"resourceState": "<exists | not_exists>",
"selfLink": "https://www.googleapis.com/storage/v1/b/...",
"size": "<SIZE_BYTES>",
"storageClass": "<REGIONAL | MULTI_REGIONAL | NEARLINE | COLDLINE>",
"timeCreated": "2018-01-27T17:54:50.058Z",
"timeStorageClassUpdated": "2018-01-27T17:54:50.058Z",
"updated": "2018-01-27T17:54:50.058Z"
},
"eventId": "28421872875176",
"eventType": "providers/cloud.storage/eventTypes/object.change",
"resource": "projects/_/buckets/<BUCKET>/objects/<FILENAME>#<GENERATION>",
"timestamp": "2018-01-27T17:54:50.157Z"
}

Developers are left to determine the nature of the event using a few of the provided values, primarily using event.data.resourceState, which will be one of exists or not_exists. Note that functions will also be invoked when an object's metadata is modified or the object is moved. In the event that an object is moved, the function will be invoked twice: once for the old object reference as resourceState: not_exists and once for the new location as resourceState: exists.