Skip to content

asd123ea/google-cloud-dotnet

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Google Cloud Libraries for .NET

.NET idiomatic client libraries for Google Cloud Platform services.

Build Status

The Google Cloud Libraries for .NET support the following Google Cloud Platform services:

Note: This client is a work-in-progress, and may occasionally make backwards-incompatible changes. The layout of the repository is likely to change, and names (of everything - assemblies, namespaces, types, members...) should be regarded as provisional.

If you need support for other Google APIs, check out the Google .NET API Client library.

Example Applications

Requirements

In order to build the code in this repository, you will need to install DNVM. You do not need DNVM to use the code, which is provided as NuGet packages.

Specifying a Project ID

Most Google Cloud Libraries for .NET require a project ID. If you don't remember yours (or haven't created a project yet), navigate to the Google Developers Console to view your project ID (or create a new project and then get the ID). Once done, record the value and make sure to pass it as a parameter to the methods that require it.

Authentication

Every API call needs to be authenticated. In order to successfully make a call, first ensure that the necessary Google Cloud APIs are enabled for your project and that you've downloaded the right set of keys (if it applies to you) as explained in the authentication document.

Next, choose a method for authenticating API requests from within your project:

  1. When using google-cloud-dotnet libraries from within Compute/App Engine, no additional authentication steps are necessary.
  2. When using google-cloud-dotnet libraries elsewhere, you can do one of the following:
    • Define the environment variable GOOGLE_APPLICATION_CREDENTIALS to be the location of the key. For example:

      set GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/key.json
      
    • If running locally for development/testing, you can authenticate using the Google Cloud SDK. Download the SDK if you haven't already, then login by running the following in the command line:

      gcloud auth login
      

Google BigQuery

The BigQuery client library is in pre-release state and is more likely to have significant surface changes over time. Still, if you would like to experiment with it, we would welcome your feedback.

Installation

The package is available on NuGet and can be installed in the following manner:

PM> Install-Package Google.Bigquery.V2 -Prerelease

Example

using Google.Bigquery.V2;
...

var client = BigqueryClient.Create(projectId);

// Create the dataset if it doesn't exist.
var dataset = client.GetOrCreateDataset("mydata");

// Create the table if it doesn't exist.
var table = dataset.GetOrCreateTable("scores", new SchemaBuilder
{
    { "player", SchemaFieldType.String },
    { "gameStarted", SchemaFieldType.Timestamp },
    { "score", SchemaFieldType.Integer }
}.Build());

// Insert a single row. There are many other ways of inserting
// data into a table.
table.InsertRow(new Dictionary<string, object>
{
    { "player", "Bob" },
    { "score", 85 },
    { "gameStarted", new DateTime(2000, 1, 14, 10, 30, 0, DateTimeKind.Utc) }
});

Google Cloud Datastore

Installation

The package is available on NuGet and can be installed in the following manner:

PM> Install-Package Google.Datastore.V1Beta3 -Prerelease

Examples

Inserting data:

using Google.Datastore.V1Beta3;
...

var db = DatastoreDb.Create(projectId, namespaceId);

var keyFactory = db.CreateKeyFactory("message");
var entity = new Entity
{
    Key = keyFactory.CreateIncompleteKey(),
    ["created"] = DateTime.UtcNow,
    ["text"] = "Text of the message",
    ["tags"] = new[] { "tag1", "tag2" }
};
using (var transaction = db.BeginTransaction())
{
    transaction.Insert(entity);
    var commitResponse = transaction.Commit();
    var insertedKey = commitResponse.MutationResults[0].Key;
}

Querying data:

using Google.Datastore.V1Beta3;
...

DatastoreDb db = DatastoreDb.Create(projectId, namespaceId);

// Print the messages created in the last 5 minutes, most recent first
DateTime cutoff = DateTime.UtcNow.AddMinutes(-5);
Query query = new Query("message")
{
    Filter = Filter.GreaterThanOrEqual("created", cutoff),
    Order = { { "created", Direction.Descending } }
};
foreach (Entity entity in db.RunQuery(query))
{
    DateTime created = (DateTime)entity["created"];
    string text = (string)entity["text"];
    string[] tags = (string[])entity["tags"];
    Console.WriteLine($"{created:yyyy-MM-dd'T'HH:mm:ss}: {text} [{string.Join(", ", tags)}]");
}

Google Cloud Logging

Installation

The package is available on NuGet and can be installed in the following manner:

PM> Install-Package Google.Logging.V2 -Prerelease

Example

using Google.Logging.V2;
...

Code coming soon

Google Cloud Pub/Sub

Installation

The package is available on NuGet and can be installed in the following manner:

PM> Install-Package Google.Pubsub.V1 -Prerelease

Example

using Google.Pubsub.V1;
...

// First create a topic.
PublisherClient publisher = PublisherClient.Create();
string topicName = PublisherClient.FormatTopicName(projectId, topicId);
publisher.CreateTopic(topicName);

// Subscribe to the topic.
SubscriberClient subscriber = SubscriberClient.Create();
string subscriptionName = SubscriberClient.FormatSubscriptionName(projectId, subscriptionId);
subscriber.CreateSubscription(subscriptionName, topicName, pushConfig: null, ackDeadlineSeconds: 60);

// Publish a message to the topic.
PubsubMessage message = new PubsubMessage
{
    // The data is any arbitrary ByteString. Here, we're using text.
    Data = ByteString.CopyFromUtf8("Hello, Pubsub"),
    // The attributes provide metadata in a string-to-string dictionary.
    Attributes =
    {
        { "description", "Simple text message" }
    }
};
publisher.Publish(topicName, new[] { message });

// Pull messages from the subscription. We're returning immediately, whether or not there
// are messages; in other cases you'll want to allow the call to wait until a message arrives.
PullResponse response = subscriber.Pull(subscriptionName, returnImmediately: true, maxMessages: 10);
foreach (ReceivedMessage received in response.ReceivedMessages)
{
    PubsubMessage msg = received.Message;
    Console.WriteLine($"Received message {msg.MessageId} published at {msg.PublishTime.ToDateTime()}");
    Console.WriteLine($"Text: '{msg.Data.ToStringUtf8()}'");
}

// Acknowledge that we've received the messages. If we don't do this within 60 seconds (as specified
// when we created the subscription) we'll receive the messages again when we next pull.
subscriber.Acknowledge(subscriptionName, response.ReceivedMessages.Select(m => m.AckId));

// Tidy up by deleting the subscription and the topic.
subscriber.DeleteSubscription(subscriptionName);
publisher.DeleteTopic(topicName);

Google Cloud Storage

Installation

The package is available on NuGet and can be installed in the following manner:

PM> Install-Package Google.Storage.V1 -Prerelease

Example

using Google.Storage.V1;
...

var client = StorageClient.Create();

// Create a bucket
var bucketName = Guid.NewGuid().ToString(); // must be globally unique
var bucket = client.CreateBucket(projectId, bucketName);

// Upload some files
var content = Encoding.UTF8.GetBytes("hello, world");
var obj1 = client.UploadObject(bucketName, "file1.txt", "text/plain", new MemoryStream(content));
var obj2 = client.UploadObject(bucketName, "folder1/file2.txt", "text/plain", new MemoryStream(content));

// List objects
foreach (var obj in client.ListObjects(bucketName, ""))
{
    Console.WriteLine(obj.Name);
}

// Download file
using (var stream = File.OpenWrite("file1.txt"))
{
    client.DownloadObject(bucketName, "file1.txt", stream);
}

Contributing

Contributions to this library are always welcome and highly encouraged.

See CONTRIBUTING for more information on how to get started.

Versioning

The Google Cloud Client Libraries for .NET follow Semantic Versioning.

Anything with a major version of zero (0.y.z) should not be considered stable - anything may change at any time.

Anything with a suffix after the three numbers (such as 1.0.0-beta01) is expected to work, but further API changes may occur before the next stable release.

License

Apache 2.0 - See LICENSE for more information.

Packages

No packages published

Languages

  • C# 99.9%
  • Other 0.1%