Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#26644] Set GCS UserAgent to Beam for Go. #26702

Merged
merged 2 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[#26644] Set GCS UserAgent to Beam.
  • Loading branch information
lostluck committed May 15, 2023
commit c4402510c012d9525e658e8f0b534bb96657a144
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")
lostluck marked this conversation as resolved.
Show resolved Hide resolved

// 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