0
votes

I have a Azure HTTP trigger POST function. When ever it gets the POST request, it insert the data into text file and store it in Azure BLOB storage. I want to store that file into File sharing storage instead BLOB storage. I don't the Azure File sharing storage in output binding of the function. Does it even possible?

run.csx

#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
using System.Net;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Blob;

 public static async Task<IActionResult> Run(HttpRequest req, CloudBlockBlob outputBlob)
    {
        var requestBody = await new StreamReader(req.Body).ReadToEndAsync();

        await outputBlob.UploadTextAsync(requestBody);
    } 

function.json

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "blob",
      "name": "outputBlob",
      "path": "customers/CUST_{DateTime}.txt",
      "connection": "AzureWebJobsStorage",
      "direction": "out"
    }
  ]
}
1

1 Answers

0
votes

As far as I know, currently Azure function doesn't support Azure File share output binding. You may refer to this tutorial: https://docs.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings#binding-direction

But you can also use Azure File storage sdk to do that, I post some code below for your reference:

var storageAccount = CloudStorageAccount.Parse("your connection string");
var fileClient = storageAccount.CreateCloudFileClient();
string baseShareName = "myazurefileshare";
var share = fileClient.GetShareReference(baseShareName);
var rootDir = share.GetRootDirectoryReference();  
var sampleDir = rootDir.GetDirectoryReference("MyFolder");
var fileToCreate = sampleDir.GetFileReference("output.txt");
fileToCreate.UploadText(".....");

For further information about Azure File Share(.Net), please refer to this tutorial:https://docs.microsoft.com/en-us/azure/storage/files/storage-dotnet-how-to-use-files