Google Cloud Storage

Typed interface to call the Google Cloud Storage v2 gRPC API to manage buckets and list objects.

Introduction

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;

Methods

NameDescription
ListBucketsLists the buckets that belong to a project.
CreateBucketCreates a new bucket in a project.
DeleteBucketDeletes an existing bucket.
ListObjectsLists the objects stored inside a bucket.

Demo

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.

See Also