Created
December 3, 2018 15:57
-
-
Save caseywatson/9b88f5c2c44948592b1b6f49e341c785 to your computer and use it in GitHub Desktop.
Write to Append Blob from Azure Function for Logic App Integration
This file contains hidden or 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
#r "Newtonsoft.Json" | |
#r "Microsoft.WindowsAzure.Storage" | |
using System.Configuration; | |
using System.Net; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Primitives; | |
using Microsoft.WindowsAzure.Storage; | |
using Microsoft.WindowsAzure.Storage.Blob; | |
using Newtonsoft.Json; | |
public static async Task<IActionResult> Run(HttpRequest req, ILogger log) | |
{ | |
// Expects JSON body: | |
// { | |
// "blobName": "[Blob name here]", | |
// "containerName": "[Container name here]", | |
// "text": "\"Column A\","Column B\",\\n,\"A1\",\"B1\",\\n,\"A2\",\"B2\"" | |
// } | |
var connectionString = System.Environment.GetEnvironmentVariable("StorageConnectionString"); | |
// StorageConnectionString configured as app setting. | |
string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); | |
dynamic data = JsonConvert.DeserializeObject(requestBody); | |
var blobName = data?.blobName; | |
var containerName = data?.containerName; | |
var text = data?.text; | |
if (blobName == null) | |
{ | |
return new BadRequestObjectResult("blobName is required."); | |
} | |
if (containerName == null) | |
{ | |
return new BadRequestObjectResult("containerName is required."); | |
} | |
if (text == null) | |
{ | |
return new BadRequestObjectResult("text is required."); | |
} | |
var storageAccount = CloudStorageAccount.Parse(connectionString); | |
var blobClient = storageAccount.CreateCloudBlobClient(); | |
var container = blobClient.GetContainerReference(containerName.ToString()); | |
await container.CreateIfNotExistsAsync(); | |
var appendBlob = container.GetAppendBlobReference(blobName.ToString()); | |
if ((await appendBlob.ExistsAsync()) == false) | |
{ | |
await appendBlob.CreateOrReplaceAsync(); | |
} | |
await appendBlob.AppendTextAsync(text.ToString()); | |
return new OkResult(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment