Skip to content

Commit

Permalink
[apache#26644] Set GCS UserAgent to Beam for Go. (apache#26702)
Browse files Browse the repository at this point in the history
* [apache#26644] Set GCS UserAgent to Beam.

* Wrap agent in parens.

---------

Co-authored-by: lostluck <13907733+lostluck@users.noreply.github.com>
  • Loading branch information
2 people authored and cushon committed May 24, 2024
1 parent 3731834 commit e6acfc8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 2 additions & 3 deletions sdks/go/pkg/beam/io/filesystem/gcs/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/apache/beam/sdks/v2/go/pkg/beam/util/fsx"
"github.com/apache/beam/sdks/v2/go/pkg/beam/util/gcsx"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)

func init() {
Expand All @@ -46,11 +45,11 @@ type fs struct {
// default credentials. If it fails, it falls back to unauthenticated
// access.
func New(ctx context.Context) filesystem.Interface {
client, err := storage.NewClient(ctx, option.WithScopes(storage.ScopeReadWrite))
client, err := gcsx.NewClient(ctx, storage.ScopeReadWrite)
if err != nil {
log.Warnf(ctx, "Warning: falling back to unauthenticated GCS access: %v", err)

client, err = storage.NewClient(ctx, option.WithoutAuthentication())
client, err = gcsx.NewUnauthenticatedClient(ctx)
if err != nil {
panic(errors.Wrapf(err, "failed to create GCS client"))
}
Expand Down
8 changes: 6 additions & 2 deletions sdks/go/pkg/beam/util/gcsx/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ import (
"google.golang.org/api/option"
)

var userAgent = option.WithUserAgent("(GPN:Beam)")

// NewClient creates a new GCS client with default application credentials, and supplied
// OAuth scope. The OAuth scopes are defined in https://pkg.go.dev/cloud.google.com/go/storage#pkg-constants.
// Sets the user agent to Beam.
func NewClient(ctx context.Context, scope string) (*storage.Client, error) {
return storage.NewClient(ctx, option.WithScopes(scope))
return storage.NewClient(ctx, option.WithScopes(scope), userAgent)
}

// NewUnauthenticatedClient creates a new GCS client without authentication.
// Sets the user agent to Beam.
func NewUnauthenticatedClient(ctx context.Context) (*storage.Client, error) {
return storage.NewClient(ctx, option.WithoutAuthentication())
return storage.NewClient(ctx, option.WithoutAuthentication(), userAgent)
}

// Upload writes the given content to GCS. If the specified bucket does not
Expand Down

0 comments on commit e6acfc8

Please sign in to comment.