【问题标题】:Using Google.Cloud.BigQuery.V2 API in Azure Functions在 Azure Functions 中使用 Google.Cloud.BigQuery.V2 API
【发布时间】:2017-04-18 14:23:24
【问题描述】:

有没有办法在 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();

【问题讨论】:

  • 包目标很好,可以在net46上工作。你能分享你的代码吗?你看到包恢复过程发生了吗?你是不是偶然部署了 project.lock.json 文件?
  • 感谢您的评论。我不知道为什么,但几分钟后它就没有这些错误。编译成功。但是由于缺少凭据,我的功能仍然无法正常工作。我会更新我的问题。

标签: .net api azure google-bigquery azure-functions


【解决方案1】:

如前所述,可以在 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 Function 中,转到平台功能 -> 应用程序设置 -> 应用程序设置。输入一个新键(如“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);

【讨论】:

  • 什么包包含 GoogleCredential?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多