正在验证 Google Cloud Datastore c# SDK

Authenticating Google Cloud Datastore c# SDK

我正在尝试在 google 云中的 k8 pod 运行 中验证 Google Datastore c# SDK。 除了使用 GOOGLE_APPLICATION_CREDENTIALS 环境变量外,我找不到任何方法将 account.json 文件注入 DatastoreDb 或 DatastoreClient。 使用 GOOGLE_APPLICATION_CREDENTIALS 环境变量是有问题的,因为我不想暴露帐户文件。

根据文档:https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.Datastore.V1/index.html

When running on Google Cloud Platform, no action needs to be taken to authenticate.

但这似乎不起作用。

向正确的方向推动将不胜感激 (:

我建议使用 K8s 密钥来存储服务帐户密钥,然后在 运行 时将其安装到 pod 中。见下文:

  1. 为所需的应用程序创建一个服务帐户。
  2. 生成并编码服务帐户密钥:只需为上一步中新创建的服务帐户生成一个 .json 密钥,然后使用 base64 -w 0 key-file-name 对其进行编码。这很重要:K8S 期望秘密的内容是 Base64 编码的。
  3. 创建K8s secret manifest文件(见下文内容),然后应用

    apiVersion: v1 kind: Secret metadata: name: your-service-sa-key-k8s-secret type: Opaque data: sa_json: previously_generated_base64_encoding

  4. 装载机密。

       volumes:
     - name: service-account-credentials-volume
       secret:
         secretName: your-service-sa-key-k8s-secret
         items:
         - key: sa_json
           path: secrets/sa_credentials.json
    

现在您所要做的就是将 GOOGLE_APPLICATION_CRENDENTIALS 设置为 secrets/sa_credentials.json.

希望这对您有所帮助。抱歉格式化(匆忙)。

这是可以做到的:

    var credential = 
 GoogleCredential.FromFile(@"/path/to/google.credentials.json").CreateScoped(DatastoreClient.DefaultScopes);
    var channel = new Grpc.Core.Channel(DatastoreClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());

    DatastoreClient client = DatastoreClient.Create(channel, settings: 
    DatastoreSettings.GetDefault());
    DatastoreDb db = DatastoreDb.Create(YOUR_PROJECT_ID, client:client);
    // Do Datastore stuff...
    // Shutdown the channel when it is no longer required.
    await channel.ShutdownAsync();

取自:https://github.com/googleapis/google-cloud-dotnet/blob/master/apis/Google.Cloud.Datastore.V1/Google.Cloud.Datastore.V1/DatastoreClient.cs