在 Azure Functions 中使用 Google.Cloud.BigQuery.V2 API

Using Google.Cloud.BigQuery.V2 API in Azure Functions

有没有办法在 Azure Functions 中使用 Google.Cloud.BigQuery.V2 (1.0.0-beta10) API?

我喜欢在 Azure Functions 中使用此 API 和我的 C# 代码,但出现此错误:

error CS0103: The name 'BigQueryClient' does not exist in the current context
error CS0246: The type or namespace name 'BigQueryTable' could not be found (are you missing a using directive or an assembly reference?)
error CS0246: The type or namespace name 'BigQueryJob' could not be found (are you missing a using directive or an assembly reference?)
error CS0246: The type or namespace name 'CreateQueryJobOptions' could not be found (are you missing a using directive or an assembly reference?)
error CS0246: The type or namespace name 'BigQueryResults' could not be found (are you missing a using directive or an assembly reference?)
error CS0246: The type or namespace name 'GetQueryResultsOptions' could not be found (are you missing a using directive or an assembly reference?)
Compilation failed.

在我的 project.json 中,我有以下代码:

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "Google.Cloud.BigQuery.V2": "1.0.0-beta10"
      }
    }
   }
}

我认为 API 在 .NET 版本 4.5 中,而 Azure Functions 需要 4.6。所以好像没有办法在AF中使用这个API? 我也用 "net45" 尝试过,但得到了同样的错误。

更新:此 API 正在处理 AF(另请参见下面的评论)。编译成功。但是由于缺少凭据,我的功能仍然无法正常工作。在我的 Visual Studio 中,代码是 运行 并且正常工作,但在 AF 中我收到以下错误:

Exception while executing function: Functions.TimerTriggerCSharp1. mscorlib: Exception has been thrown by the target of an invocation. Google.Api.Gax: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

我已经生成了一个服务帐户密钥作为 json 文件并上传到我的 AF。通常,我想,我可以通过在我的 C# 代码中了解项目和数据集 ID 来访问我在 BigQuery 上的表:

string projectId = "...";
string datasetId = "...";

var client = BigQueryClient.Create(projectId);

List<BigQueryTable> tables = client.ListTables(datasetId).ToList();

如前所述,可以在 Azure Functions 中使用 Google.Cloud.BigQuery.V2 (1.0.0-beta10) API。只需使用上面的代码。如果您想使用多个依赖项,只需添加它们并用逗号分隔即可。就我而言,我也需要 Google.Apis 。代码如下所示:

{
"frameworks": {
    "net46":{
      "dependencies": {
        "Google.Cloud.BigQuery.V2": "1.0.0-beta10",
        "Google.Apis": "1.24.1.0"
        }
    }
   }
}

对于身份验证,重要的是生成服务帐户密钥作为 JSON 文件(Google Cloud Platform -> API Manager -> Credentials)。 在 Azure Functions 中转到平台功能 -> 应用程序设置 -> 应用程序设置。输入一个新键(如 "GoogleCloudKey")并将 JSON 文件的内容复制到值的单元格中。

在 "run.csx" 中,您必须创建并提交您的凭据:

GoogleCredential credential = null;

using (var credentialStream = new MemoryStream(Encoding.UTF8.GetBytes(ConfigurationManager.AppSettings["GoogleCloudKey"])))
{
credential = GoogleCredential.FromStream(credentialStream);
}

var client = BigQueryClient.Create(projectId, credential);