How do I authenticate with SDK credentials on GCP using Go?

To authenticate with Google Cloud Platform (GCP) using SDK credentials in Go, you need to use the Google Cloud Client Libraries. Follow the example below to set up authentication using a service account file.

package main import ( "context" "fmt" "log" "cloud.google.com/go/storage" "google.golang.org/api/option" ) func main() { ctx := context.Background() // Use the service account key file for authentication client, err := storage.NewClient(ctx, option.WithCredentialsFile("path/to/your/service-account-file.json")) if err != nil { log.Fatalf("Failed to create client: %v", err) } defer client.Close() // Replace "your-bucket-name" with the name of your GCP bucket bkt := client.Bucket("your-bucket-name") fmt.Println("Successfully authenticated with GCP!") // Example operation: Listing objects in the bucket it := bkt.Objects(ctx, nil) for { objAttrs, err := it.Next() if err == iterator.Done { break } if err != nil { log.Fatalf("Error listing objects: %v", err) } fmt.Println(objAttrs.Name) } }

Go GCP Google Cloud Platform SDK credentials service account authentication