简体   繁体   English

如何使用 .net 核心连接到 Cloud Firestore DB?

[英]How to connect to Cloud Firestore DB with .net core?

So far all the examples of using Google Cloud Firestore with .net show that you connect to your Firestore db by using this command:到目前为止,所有将 Google Cloud Firestore 与 .net 结合使用的示例都表明您使用以下命令连接到 Firestore 数据库:

FirestoreDb db = FirestoreDb.Create(projectId);

But is this skipping the step of authentication?但这是否跳过了身份验证步骤? I can't seem to find an example of wiring it up to use a Google service account.我似乎无法找到连接它以使用 Google 服务帐户的示例。 I'm guessing you need to connect using the service account's private_key/private_key_id/client_email?我猜您需要使用服务帐户的 private_key/private_key_id/client_email 进行连接?

You can also use the credentials stored in a json file:您还可以使用存储在 json 文件中的凭据:

    GoogleCredential cred = GoogleCredential.FromFile("credentials.json");
    Channel channel = new Channel(FirestoreClient.DefaultEndpoint.Host,
                  FirestoreClient.DefaultEndpoint.Port,
                  cred.ToChannelCredentials());
    FirestoreClient client = FirestoreClient.Create(channel);
    FirestoreDb db = FirestoreDb.Create("my-project", client);

I could not compile @Michael Bleterman's code, however the following worked for me:我无法编译@Michael Bleterman 的代码,但是以下对我有用:

using Google.Cloud.Firestore;
using Google.Cloud.Firestore.V1;


var jsonString = File.ReadAllText(_keyFilepath);
var builder = new FirestoreClientBuilder {JsonCredentials = jsonString};
FirestoreDb db = FirestoreDb.Create(_projectId, builder.Build());

Packages I use:我使用的软件包:

<PackageReference Include="Google.Cloud.Firestore" Version="2.0.0-beta02" />
<PackageReference Include="Google.Cloud.Storage.V1" Version="2.5.0" />

But is this skipping the step of authentication?但这是否跳过了身份验证步骤?

No. It will use the default application credentials.否。它将使用默认应用程序凭据。 If you're running on Google Cloud Platform (AppEngine, GCE or GKE), they will just be the default service account credentials for the instance.如果您在 Google Cloud Platform(AppEngine、GCE 或 GKE)上运行,它们将只是实例的默认服务帐户凭据。 Otherwise, you should set the GOOGLE_APPLICATION_CREDENTIALS environment variable to refer to a service account credential file.否则,您应该设置GOOGLE_APPLICATION_CREDENTIALS环境变量以引用服务帐户凭据文件。

From the home page of the user guide you referred to:从您提到的用户指南主页

When running on Google Cloud Platform, no action needs to be taken to authenticate.在 Google Cloud Platform 上运行时,无需采取任何操作来进行身份验证。

Otherwise, the simplest way of authenticating your API calls is to download a service account JSON file then set the GOOGLE_APPLICATION_CREDENTIALS environment variable to refer to it.否则,验证 API 调用的最简单方法是下载服务帐户 JSON 文件,然后设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量以引用它。 The credentials will automatically be used to authenticate.凭据将自动用于身份验证。 See the Getting Started With Authentication guide for more details.有关更多详细信息,请参阅身份验证入门指南。

It's somewhat more awkward to use non-default credentials;使用非默认凭据有点尴尬; this recent issue gives an example. 最近的这个问题给出了一个例子。

This worked for me.这对我有用。

https://pieterdlinde.medium.com/netcore-and-cloud-firestore-94628943eb3c https://pieterdlinde.medium.com/netcore-and-cloud-firestore-94628943eb3c

string filepath = "/Users/user/Downloads/user-a4166-firebase-adminsdk-ivk8q-d072fdf334.json";
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", filepath);
fireStoreDb = FirestoreDb.Create("user-a4166");

The simplest way:最简单的方法:

Get service account json file and hardcode values into a class:获取服务帐户 json 文件并将值硬编码为 class:

    public class FirebaseSettings
    {
        [JsonPropertyName("project_id")]
        public string ProjectId => "that-rug-really-tied-the-room-together-72daa";
    
        [JsonPropertyName("private_key_id")]
        public string PrivateKeyId => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    
        // ... and so on
    }

Add it Startup.cs添加Startup.cs

var firebaseJson = JsonSerializer.Serialize(new FirebaseSettings());

services.AddSingleton(_ => new FirestoreProvider(
    new FirestoreDbBuilder 
    { 
        ProjectId = firebaseSettings.ProjectId, 
        JsonCredentials = firebaseJson // <-- service account json file
    }.Build()
));

Add wrapper FirebaseProvider添加包装器FirebaseProvider

public class FirestoreProvider
{
    private readonly FirestoreDb _fireStoreDb = null!;

    public FirestoreProvider(FirestoreDb fireStoreDb)
    {
        _fireStoreDb = fireStoreDb;
    }

    // ... your methods here

}

Here is a full example of a generic provider.这是通用提供程序的完整示例。

https://dev.to/kedzior_io/simple.net-core-and-cloud-firestore-setup-1pf9 https://dev.to/kedzior_io/simple.net-core-and-cloud-firestore-setup-1pf9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM