Libraries required for C# code to export files to Azure Blob Storage from websites hosted on Azure App Service

Amit Khobragade 20 Reputation points
2023-03-28T14:47:12.6366667+00:00

We were using IIS for deploying .NET web application.

One export to excel button had been given on UI to export details from database to Excel file and then it will be downloaded to local disc storage.

We used Microsoft Office Interop Excel to export file.

It was working fine locally. But when we deployed application on Azure App service, it was not downloading files in local disk.

Now, we want to export files to Azure Blob Storage.

Which libraries is required for writing C# code for such application?

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,427 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,233 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,865 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,601 Reputation points
    2023-03-29T15:54:51.6066667+00:00

    Microsoft Office Interop Excel will not work when hosted in Azure. the library requires office be installed on the server, and is not designed for server use. you will need to replace with another library or build a custom VM hosted in azure.

    to access azure blog storage you can use the HttpClient and the azure blob rest api

    https://learn.microsoft.com/en-us/rest/api/storageservices/blob-service-rest-api

    or a the client library:

    https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-dotnet?tabs=visual-studio%2Cmanaged-identity%2Croles-azure-portal%2Csign-in-azure-cli%2Cidentity-visual-studio

    0 comments No comments

  2. Ali Sufyan Butt 86 Reputation points MVP
    2023-04-13T12:26:48.1633333+00:00

    Export from data base to browser can be done very easily without any database using below method

    In you asp.net web api project, in the method
    a- import data into local DTO List
    b- Convert that to CSV string
    c- Return with mime type CSV

    To export files to Azure Blob Storage from your .NET web application, you will need to use the Azure Blob Storage client library for .NET. You can install this library by adding the "Azure.Storage.Blobs" NuGet package to your project. Here is some sample code that shows how to create a Blob container, upload a file to it, and generate a shared access signature (SAS) URI for the uploaded file. This sample code assumes that you have already obtained the file to upload and have its file path in a variable called filePath. You will also need to replace the connection string and container and file names with the appropriate values for your storage account.

    using Azure.Storage.Blobs;
    using Azure.Storage.Blobs.Models;
    using Azure.Storage.Sas;
    
    // Get a reference to your storage account
    string connectionString = "DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=mykey;EndpointSuffix=core.windows.net";
    BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
    
    // Create a Blob container to store your files
    string containerName = "mycontainer";
    BlobContainerClient containerClient = await blobServiceClient.CreateBlobContainerAsync(containerName);
    
    // Upload a file to the container
    string fileName = "myfile.xlsx";
    BlobClient blobClient = containerClient.GetBlobClient(fileName);
    using (var fileStream = File.OpenRead(filePath))
    {
        await blobClient.UploadAsync(fileStream, new BlobUploadOptions { HttpHeaders = new BlobHttpHeaders { ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" } });
    }
    
    // Generate a SAS URI for the uploaded file
    var sasBuilder = new BlobSasBuilder
    {
        BlobContainerName = containerName,
        BlobName = fileName,
        Resource = "b",
        ExpiresOn = DateTimeOffset.UtcNow.AddHours(1)
    };
    sasBuilder.SetPermissions(BlobSasPermissions.Read);
    string sasUri = blobClient.GenerateSasUri(sasBuilder);
    
    
    0 comments No comments