From 7ba02993703dac101f1673401fd5297b1d32aff7 Mon Sep 17 00:00:00 2001 From: line1029 Date: Thu, 15 Jan 2026 23:58:47 +0900 Subject: [PATCH] feat(gcs): add support for application default credentials (ADC) Allows GCS initialization without explicit credentials to support ADC. Fixes #18. --- oss/gcsblob/gcs.go | 23 ++++++++++++++++------- oss/oss.go | 4 ++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/oss/gcsblob/gcs.go b/oss/gcsblob/gcs.go index 43a8eb4..08e7be6 100644 --- a/oss/gcsblob/gcs.go +++ b/oss/gcsblob/gcs.go @@ -29,13 +29,22 @@ func NewGoogleCloudStorage(args oss.OSSArgs) (oss.OSS, error) { ctx := context.Background() bucket := args.GoogleCloudStorage.Bucket credentialsB64 := args.GoogleCloudStorage.CredentialsB64 - credentials, err := base64.StdEncoding.DecodeString(credentialsB64) - if err != nil { - return nil, oss.ErrProviderInit.WithError(err).WithDetail("credentials must be a base64 encoded string") - } - client, err := storage.NewClient(ctx, option.WithCredentialsJSON(credentials)) - if err != nil { - return nil, oss.ErrProviderInit.WithError(err) + + var client *storage.Client + if credentialsB64 == "" { + client, err = storage.NewClient(ctx) + if err != nil { + return nil, oss.ErrProviderInit.WithError(err).WithDetail("failed to initialize GCS client with ADC") + } + } else { + credentials, err := base64.StdEncoding.DecodeString(credentialsB64) + if err != nil { + return nil, oss.ErrProviderInit.WithError(err).WithDetail("credentials must be a base64 encoded string") + } + client, err = storage.NewClient(ctx, option.WithCredentialsJSON(credentials)) + if err != nil { + return nil, oss.ErrProviderInit.WithError(err) + } } return &GoogleCloudStorage{ bucket: bucket, diff --git a/oss/oss.go b/oss/oss.go index c7aaa80..376cd20 100644 --- a/oss/oss.go +++ b/oss/oss.go @@ -143,8 +143,8 @@ type GoogleCloudStorage struct { } func (g *GoogleCloudStorage) Validate() error { - if g.Bucket == "" || g.CredentialsB64 == "" { - msg := fmt.Sprintf("bucket and credentials cannot be empty.") + if g.Bucket == "" { + msg := fmt.Sprintf("bucket cannot be empty.") return ErrArgumentInvalid.WithDetail(msg) } return nil