Last active
January 17, 2021 22:00
-
-
Save MrMikeFloyd/dacb049eaa1d35bd4f93eb02728a9f39 to your computer and use it in GitHub Desktop.
Sample for binding an Azure Function (v2) to an external Table Storage using CloudTable instead of IQueryable. Tested on Visual Studio Code 1.19.3 and .NET Core 2.0.5 on macOS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Azure.WebJobs.Host; | |
using Newtonsoft.Json; | |
using System; | |
using Microsoft.WindowsAzure.Storage.Table; | |
using System.Collections.Generic; | |
using System.Net.Http; | |
using System.Net; | |
using System.Text; | |
namespace Company.Function | |
{ | |
public class TabEntity : TableEntity | |
{ | |
public Int32 rowX { get; set; } | |
public Int32 rowY { get; set; } | |
public Int32 rowZ { get; set; } | |
} | |
public static class HttpTrigger | |
{ | |
[FunctionName("HttpTrigger")] | |
public static HttpResponseMessage Run( | |
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequest req, | |
[Table("sampletable")] CloudTable tab, | |
TraceWriter log) | |
{ | |
log.Info("Kicking off."); | |
// Load data from TableStorage using CloudTable | |
var querySegment = tab.ExecuteQuerySegmentedAsync(new TableQuery<TabEntity>(), null); | |
StringContent responseContent = null; | |
foreach (TabEntity item in querySegment.Result) | |
{ | |
log.Info($"Data loaded: '{item.PartitionKey}' | '{item.RowKey}' | '{item.rowX}' | '{item.rowY}'"); | |
responseContent = new StringContent(JsonConvert.SerializeObject(item), Encoding.UTF8, "application/json"); | |
} | |
log.Info("Done."); | |
return new HttpResponseMessage(HttpStatusCode.OK){ | |
Content = responseContent | |
}; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"IsEncrypted": false, | |
"Values": { | |
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=xyz;AccountKey=01234567890", | |
"AzureWebJobsDashboard": "" | |
} | |
} |
it was very useful for me. solved my problem. also tell how can I add condition in query like filter in this statement tab.ExecuteQuerySegmentedAsync(new TableQuery(), null);
it was very useful for me. solved my problem. also tell how can I add condition in query like filter in this statement tab.ExecuteQuerySegmentedAsync(new TableQuery(), null);
Glad to hear it helped @Preeti1910! TableQuery
supports "Builder Pattern"-Style Where clauses like so (Example taken from the official docs found here: var tableQuery = new TableQuery() .where('Name == ? or Name <= ?', 'Person1', 'Person2'); .or('Age >= ?', 18);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@yzorg sorry for the late response. Your assumption is accurate. This is a very minimal example to demonstrate accessing TableStorage, so don't expect any serious functionality here :)