Typed interface to call the Google Cloud Storage v2 gRPC API to manage buckets and list objects.
Google Cloud Storage is an object-storage service for storing and retrieving any amount of data. The gRPC API is exposed through the google.storage.v2.Storage service, reached at storage.googleapis.com:443 over TLS, with the main methods ListBuckets, CreateBucket, DeleteBucket and ListObjects.
Requests are built with typed classes such as TsgcGRPCStorageListBucketsRequest (Parent = projects/<id>) and the reply is returned in TsgcGRPCStorageListBucketsResponse, whose Buckets contain the bucket Name. Note that the gRPC Storage API also expects an x-goog-request-params routing metadata header that identifies the targeted resource; the typed interface adds it for you on each call.
The example below authenticates with a service-account JWT, wires a TsgcGRPCClient over a TsgcHTTP2Client to the Storage host, sets the authorization Bearer metadata and calls ListBuckets for a project:
oHTTP2 := TsgcHTTP2Client.Create(nil);
oHTTP2.Host := 'storage.googleapis.com';
oHTTP2.Port := 443;
oHTTP2.TLS := True;
oGRPC := TsgcGRPCClient.Create(nil);
oGRPC.Client := oHTTP2;
// service-account JWT authentication
oGRPC.GoogleCloudOptions.JWT.KeyFile := 'service-account.json';
oGRPC.GoogleCloudOptions.JWT.API_Endpoint := 'https://storage.googleapis.com/';
oGRPC.DefaultMetadata.AddValue('authorization', 'Bearer ' + oGRPC.GoogleCloudOptions.JWT.Token);
// build the typed request and call the method
oRequest := TsgcGRPCStorageListBucketsRequest.Create;
try
oRequest.Parent := 'projects/my-project-id';
oResponse := oGRPC.Call('google.storage.v2.Storage', 'ListBuckets', oRequest.ToBytes);
ShowMessage(oResponse.DataString);
finally
oRequest.Free;
end;
| Name | Description |
|---|---|
| ListBuckets | Lists the buckets that belong to a project. |
| CreateBucket | Creates a new bucket in a project. |
| DeleteBucket | Deletes an existing bucket. |
| ListObjects | Lists the objects stored inside a bucket. |
A working sample is available in the demo folder Demos/21.GRPC/15.Cloud_Storage, which shows how to authenticate and list the buckets of a project with the ListBuckets method.