Skip to content

Instantly share code, notes, and snippets.

@clivefoley
Last active February 4, 2019 17:05
Show Gist options
  • Save clivefoley/ee5eac7345eab64721cfd0438bc1ab9d to your computer and use it in GitHub Desktop.
Save clivefoley/ee5eac7345eab64721cfd0438bc1ab9d to your computer and use it in GitHub Desktop.
A simple azure function, triggered by cosmos that logs to blob storage
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Azure.Documents;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Blob;
namespace CosmosDbChangeFeedTrigger
{
public static class CosmosDbChangeFeedTrigger
{
[FunctionName("<put azure function name here")]
public static void Run([CosmosDBTrigger(
"<put database name here>",
"<put collection name here>",
ConnectionStringSetting = "<put cosmos db connection string name here",
LeaseCollectionName = "<put leases collection name here>",
CreateLeaseCollectionIfNotExists = true)]IReadOnlyList<Document> documents,
[Blob("test-database-container", FileAccess.Write)] CloudBlobContainer container,
ILogger log)
{
log.LogInformation("Starting the function....");
log.LogInformation("Saving data...");
documents.ToList().ForEach(d => writeToBlobStorage(container, d));
log.LogInformation("Data saved, exiting.");
}
private static void writeToBlobStorage(CloudBlobContainer container, Document d)
{
var fileName = string.Format("log-file-{0}.json",d.Id);
Console.WriteLine("Uploading to Blob storage as {0}...", fileName);
CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(fileName);
cloudBlockBlob.UploadTextAsync(d.ToString()).Wait();
}
}
}
@clivefoley
Copy link
Author

The "Blob" attribute is an "out" binding. Together with the FileAccess enum it gives access to a CloudBlobContainer instance. This instance can be used to access blob storage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment