prefect-gcp helps you leverage the capabilities of Google Cloud Platform (GCP) in your workflows.
For example, you can run flow on Vertex AI or Cloud Run, read and write data to BigQuery and Cloud Storage, retrieve secrets with Secret Manager.
Getting Started
Prerequisites
- Prefect installed in a virtual environment.
- A GCP account and the necessary permissions to access desired services.
Install prefect-gcp
Install or update to the latest version of the prefect-gcp library and dependencies.- Prefect 2
- Prefect 3
Register newly installed block types
Register the block types in the module to make them available for use.Authenticate using a GCP Credentials block
Authenticate with a service account to useprefect-gcp services.
- Refer to the GCP service account documentation to create and download a service account key file.
- Copy the JSON contents.
- Use the Python code below, replace the placeholders with your information.
Run flows on Google Cloud Run or Vertex AI
Run flows on Google Cloud Run or Vertex AI to dynamically scale your infrastructure. See the Google Cloud Run Worker Guide for a walkthrough of using Google Cloud Run to run workflows with a hybrid work pool. If you’re using Prefect Cloud, Google Cloud Run push work pools provide all the benefits of Google Cloud Run along with a quick setup and no worker needed.Use Prefect with Google BigQuery
Read data from and write to Google BigQuery within your Prefect flows. Be sure to installprefect-gcp with the BigQuery extra.
This code creates a new dataset in BigQuery, define a table, insert rows, and fetch data from the table:
Use Prefect with Google Cloud Storage
Interact with Google Cloud Storage. Be sure to installprefect-gcp with the Cloud Storage extra.
The code below uses prefect_gcp to upload a file to a Google Cloud Storage bucket and download the same file under a different file name.
Upload and download directories
GcsBucket supports uploading and downloading entire directories.Save secrets with Google Secret Manager
Read and write secrets with Google Secret Manager. Be sure to installprefect-gcp with the Secret Manager extra.
The code below writes a secret to the Secret Manager, reads the secret data, and deletes the secret.
Access Google credentials or clients from GcpCredentials
You can instantiate a Google Cloud client, such asbigquery.Client.
Note that a GcpCredentials object is NOT a valid input to the underlying BigQuery client - use the get_credentials_from_service_account method to access and pass a google.auth.Credentials object.
get_client method from GcpCredentials.