stkvtflw
stkvtflw

Reputation: 13577

GCS: Manually trigger "object created" event

I have a Cloud Function that is being triggered by new objects creation in a GCS bucket.

Sometimes things go wrong and the GCF fails. I know that I can enable automatic retry. Still, it would be nice to be able to trigger "object created" event for existing objects during development/debugging. How do I do that?

Upvotes: 4

Views: 1926

Answers (1)

ewertonvsilva
ewertonvsilva

Reputation: 1945

Example on how to simulate a file upload event trig:

User a function to print a event result, so you can have a template of how is the event data:

def hello_gcs(event, context):
    """Triggered by a change to a Cloud Storage bucket.
    Args:
         event (dict): Event payload.
         context (google.cloud.functions.Context): Metadata for the event.
    """
    print(event)

Get the json result in the logs:

enter image description here

Use the tests tab to resend the same json object when you want for test (logs result can take some time)

You have to format the json before use because you have to use " insteade of '. Use this site

enter image description here


Json example:

{
   "bucket":"<bucket name>",
   "contentType":"image/png",
   "crc32c":"a1/tEw==",
   "etag":"9999999999999999/UCEAE=",
   "generation":"9999999999999999",
   "id":"<bucket name>/<file name>",
   "kind":"storage#object",
   "md5Hash":"9999999999999999==",
   "mediaLink":"https://www.googleapis.com/download/storage/v1/b/<bucket name>/o/<file name>?generation=9999999999999999&alt=media",
   "metageneration":"1",
   "name":"Screenshot 2022-02-10 6.09.37 PM.png",
   "selfLink":"https://www.googleapis.com/storage/v1/b/<bucket name>/o/<file name>",
   "size":"452941",
   "storageClass":"STANDARD",
   "timeCreated":"2022-02-11T10:22:01.919Z",
   "timeStorageClassUpdated":"2022-02-11T10:22:01.919Z",
   "updated":"2022-02-11T10:22:01.919Z"
}

Upvotes: 3

Related Questions