Search code examples
c#.netazuregoogle-bigqueryazure-functions

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


Is there a way to use Google.Cloud.BigQuery.V2 (1.0.0-beta10) API in Azure Functions?

I like to use this API and my C# code in Azure Functions but get this error:

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.

In my project.json I have the following code:

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

I think the API is in .NET version 4.5 and Azure Functions needs 4.6. So it seems there is no way to use this API in AF? I tried it also with "net45" but get the same errors.

UPDATE: This API is working on AF (see also comment below). Compilation succeeded. But my function is still not working because of missing credentials. In my Visual Studio the code is running and working like it should but in AF I got the following error:

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.

I already generated a service account key as json file an uploaded to my AF. Normally, I think, I get access to my tables on BigQuery through knowing the project and dadaset IDs like this in my C# code:

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

var client = BigQueryClient.Create(projectId);

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

Solution

  • Like mentioned before it is possible to use Google.Cloud.BigQuery.V2 (1.0.0-beta10) API in Azure Functions. Just use the code above. If you want to use more than one dependency just add them and seperate them with a comma. In my case I needed the Google.Apis as well. The code looks like this:

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

    For the authentification it is important to generate a service account key as JSON file (Google Cloud Platform -> API Manager -> Credentials). In the Azure Function go to Platform features -> Application settings -> App settings. Type in a new key (like "GoogleCloudKey") and copy the content of the JSON file into the cell for the value.

    In "run.csx" you have to create and hand over your credentials:

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