Skip to content

Commit ab74300

Browse files
authored
blob/gcsblob: add support for using gRPC (#3616)
1 parent 3690eda commit ab74300

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

blob/gcsblob/gcsblob.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,14 @@ type Options struct {
295295
// If your implementation of 'SignBytes' needs a request context, set this instead.
296296
MakeSignBytes func(requestCtx context.Context) SignBytesFunc
297297

298+
// Client provides a *storage.Client to use, instead of constructing one based on
299+
// the HTTPClient. When set, you must pass nil as the gcp.HTTPClient to OpenBucket.
300+
//
301+
// For example, this can be used to create a Bucket backed by a gRPC client.
302+
Client *storage.Client
303+
298304
// ClientOptions are passed when constructing the storage.Client.
305+
// Ignored if Client is set.
299306
ClientOptions []option.ClientOption
300307
}
301308

@@ -312,12 +319,21 @@ type SignBytesFunc func([]byte) ([]byte, error)
312319

313320
// openBucket returns a GCS Bucket that communicates using the given HTTP client.
314321
func openBucket(ctx context.Context, client *gcp.HTTPClient, bucketName string, opts *Options) (*bucket, error) {
315-
if client == nil {
316-
return nil, errors.New("gcsblob.OpenBucket: client is required")
322+
if opts == nil {
323+
opts = &Options{}
317324
}
318325
if bucketName == "" {
319326
return nil, errors.New("gcsblob.OpenBucket: bucketName is required")
320327
}
328+
if opts.Client != nil {
329+
if client != nil {
330+
return nil, errors.New("gcsblob.OpenBucket: client must be nil when providing Options.Client")
331+
}
332+
return &bucket{name: bucketName, client: opts.Client, opts: opts}, nil
333+
}
334+
if client == nil {
335+
return nil, errors.New("gcsblob.OpenBucket: client is required")
336+
}
321337

322338
// We wrap the provided http.Client to add a Go CDK User-Agent.
323339
clientOpts := []option.ClientOption{option.WithHTTPClient(useragent.HTTPClient(&client.Client, "blob"))}
@@ -328,9 +344,6 @@ func openBucket(ctx context.Context, client *gcp.HTTPClient, bucketName string,
328344
option.WithHTTPClient(http.DefaultClient),
329345
}
330346
}
331-
if opts == nil {
332-
opts = &Options{}
333-
}
334347
clientOpts = append(clientOpts, opts.ClientOptions...)
335348
c, err := storage.NewClient(ctx, clientOpts...)
336349
if err != nil {

internal/testing/setup/setup.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func NewAWSv2Config(ctx context.Context, t *testing.T, region string, scrubBody
143143
}
144144

145145
// NewGCPClient creates a new HTTPClient for testing against GCP.
146-
// NewGCPClient creates a new HTTPClient for testing against GCP.
146+
//
147147
// If the test is in --record mode, the client will call out to GCP, and the
148148
// results are recorded in a replay file.
149149
// Otherwise, the session reads a replay file and runs the test as a replay,
@@ -174,6 +174,7 @@ func NewGCPClient(ctx context.Context, t *testing.T) (client *gcp.HTTPClient, rt
174174
}
175175

176176
// NewGCPgRPCConn creates a new connection for testing against GCP via gRPC.
177+
//
177178
// If the test is in --record mode, the client will call out to GCP, and the
178179
// results are recorded in a replay file.
179180
// Otherwise, the session reads a replay file and runs the test as a replay,

0 commit comments

Comments
 (0)