Google Professional Cloud Developer Question 344
Single answerGoogle Cloud PlatformYou are building a serverless application using Google Cloud Functions, and you need to trigger a function whenever a file is uploaded to a specific Cloud Storage bucket. How should you configure the trigger to achieve this?
- A
Set up an Eventarc trigger with a Cloud Storage event type and specify the bucket name.
- B
Configure a Pub/Sub topic and write a custom script to monitor the bucket for changes.
- C
Use Cloud Scheduler to check the bucket periodically and invoke the function manually.
- D
Create a Cloud Storage trigger directly on the Cloud Function and specify the bucket name and event type.
Show answer and explanation
Correct answer: D
Explanation
To trigger a Cloud Function when a file is uploaded to a specific Cloud Storage bucket, the most efficient and native solution is to use a Cloud Storage trigger directly in Google Cloud Functions. This eliminates the need for additional components like Eventarc or Pub/Sub and integrates seamlessly with the event-driven nature of the application.
- A. Incorrect.
While Eventarc can be used to handle various event triggers, the most straightforward and recommended approach for triggering a function from a Cloud Storage event is to use a Cloud Storage-specific trigger directly in Cloud Functions.
- B. Incorrect.
This approach is unnecessarily complex for the given requirement. Using Pub/Sub for this purpose would require additional setup and maintenance, which is not needed here.
- C. Incorrect.
Cloud Scheduler is not designed for event-driven architectures. It is used for time-based scheduling and would not provide real-time triggering for file uploads.
- D. Correct.
This is the correct approach because Google Cloud Functions natively supports Cloud Storage triggers. You can specify the bucket name and event type (e.g., 'google.storage.object.finalize') to invoke the function whenever a file is uploaded.