Google Cloud Storage

用于调用 Google Cloud Storage v2 gRPC API 的类型化接口,以管理存储桶并列出对象。

简介

Google Cloud Storage 是一种用于存储和检索任意数量数据的对象存储服务。该 gRPC API 通过 google.storage.v2.Storage 服务公开,在 storage.googleapis.com:443 上通过 TLS 访问,主要方法包括 ListBucketsCreateBucketDeleteBucketListObjects

请求通过类型化类构建,例如 TsgcGRPCStorageListBucketsRequest (Parent = projects/<id>),响应在 TsgcGRPCStorageListBucketsResponse 中返回,其 Buckets 包含存储桶 Name。请注意,gRPC Storage API 还期望一个用于标识目标资源的 x-goog-request-params 路由元数据标头;类型化接口会在每次调用时为您添加它。

下面的示例使用服务账户 JWT 进行身份验证,通过 TsgcHTTP2ClientTsgcGRPCClient 连接到 Storage 主机,设置授权 Bearer 元数据,并为某个项目调用 ListBuckets


    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;

方法

名称描述
ListBuckets列出属于某个项目的存储桶。
CreateBucket在某个项目中创建一个新的存储桶。
DeleteBucket删除现有存储桶。
ListObjects列出存储在某个存储桶内的对象。

演示

示例文件夹 Demos/21.GRPC/15.Cloud_Storage 中提供了一个可用的示例,演示如何使用 ListBuckets 方法进行身份验证并列出某个项目的存储桶。

另请参阅