Google 云和 Google.Cloud.Functions.Framework

Google Cloud and Google.Cloud.Functions.Framework

我正在使用 Google 具有 .net 核心的云功能和 Google.Cloud.Functions.Framework 来记录用户 activity。我考虑过使用 DB 来存储这些活动,但是在第一步我会尝试将其存储在 google 存储上的简单文本文件中(从架构的角度来看是否合适?)。

我的问题是如何使用 .net 核心和 google 函数写入 google 存储桶(我的函数是在 HTTP 上触发的)并且看起来很简单

    public async Task HandleAsync(HttpContext context)
    {
         //code goes here
         await context.Response.WriteAsync("Activity logged");
    }

您将使用 Google.Cloud.Storage.V1 NuGet package (documentation),就像在任何其他环境中一样。 (就 Google Cloud Functions 而言,这里没有什么特别之处 - 身份验证应该开箱即用,无需任何额外凭据。)

我们有一个 sample function 集成了 Google Cloud Storage 和 Vision API,其中包括将文件上传到 Google Cloud Storage。

我建议使用 dependency injection via a FunctionsStartup class 配置 StorageClient,然后您应该能够直接将对象上传到存储桶。