<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

How to write data to Azure Table Store with an Azure Function

Published on
7,624 Points
2,324 Views
3 Endorsements
Last Modified:
Shaun Vermaak
My name is Shaun Vermaak and I have always been fascinated with technology and how we use it to enhance our lives and business.
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud.
This article shows how to create one of these functions to write directly to Azure Table Storage.

1) Create Function App


From Azure Portal

Click New

Search for Function App

Click Function App in Results

Click Create



Supply App name, Subscription, Resource Group, Hosting Plan, Location and Storage and click Create


2) Create Function


After Function App is created, navigate to it.

From here we'll use a pre-made function and alter it to write to Table Storage.


Select Webhook + API, C# as the language and click Create this Function



3) Add Azure Table Storage


From the Integrate option, add Azure Table Storage. Note the Table parameter name.


4) Edit Function


Add file item.csx (Click Save and Run to ensure no errors)

This is an object that inherits TableEntity and adds ItemName

#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage.Table;
public class Item : TableEntity
{
public string ItemName {get;set;}
}


Add a reference to item.csx to run.csx(Click Save and Run to ensure no errors)

This allows Item object to be used in run.csx

#load "item.csx"


Change into Synchronous Function (Click Save and Run to ensure no errors)

This is required for the next step, out is not allowed on Asynchronous functions

...
public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log)
...
dynamic data = req.Content.ReadAsAsync<object>().Result; 
...


Add an out variable and assign value (Click Save and Run to ensure no errors)

Add out item with the name of the table parameter name

...
public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log, out Item outputTable)
...
outputTable = new Item() {PartitionKey = "some-partition-key", RowKey=Guid.NewGuid().ToString(), ItemName = name ?? "No name"};
...


Here is complete listing of run.csx

#load "item.csx"

using System.Net;

public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log, out Item outputTable)
{
log.Info("C# HTTP trigger function processed a request.");

// parse query parameter
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;

// Get request body
dynamic data = req.Content.ReadAsAsync<object>().Result;

// Set name to query string or body data
name = name ?? data?.name;

outputTable = new Item() {PartitionKey = "some-partition-key", RowKey=Guid.NewGuid().ToString(), ItemName = name ?? "No name"};

return name == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}


5) Verify Data


Every time this function runs, an entry will be created in the Azure Table Storage.

You can use Microsoft Azure Storage Explorer to view these entries.



Please do not forget to press the "Thumb's Up" button if this article was helpful and valuable for EE members.
It also provides me with positive feedback. Thank you!



3
1 Comment

Expert Comment

by:Jagadeesh Tennati
Could you help for Node js with the similar article on how to setup Azure Storage integration with node js
0

Featured Post

Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month