Friday, April 28, 2023

code for azure function with storage of excel file

This function listens to HTTP POST requests and stores the Excel file in Blob storage under the "excel-files" container with a random GUID as the file name. Note that this function requires the Microsoft.Azure.WebJobs.Extensions.Storage NuGet package. When you make a POST request to this function with an Excel file in the request body, the function will store the file in Blob storage and return an HTTP 200 OK response with the message "Excel file stored successfully". You can then use this file in other Azure Functions or download it from Blob storage using the Azure Storage SDK or Azure portal.


using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.IO;
using System.Threading.Tasks;

public static class StoreExcelFunction
{
    [FunctionName("StoreExcel")]
    public static async Task Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
        [Blob("excel-files/{rand-guid}.xlsx", FileAccess.Write)] Stream excelFile,
        ILogger log)
    {
        await req.Body.CopyToAsync(excelFile);
        log.LogInformation("Excel file stored successfully");

        return new OkObjectResult("Excel file stored successfully");
    }
}

No comments:

Post a Comment

ASP.NET Core

 Certainly! Here are 10 advanced .NET Core interview questions covering various topics: 1. **ASP.NET Core Middleware Pipeline**: Explain the...