Reputation: 3
I'm trying to create a function to download blobs from Azure Blob Storage.
I'm developing a web application using ASP.NET Core (latest version).
This application will be deployed to an Azure Web App service later and due to this, I don't want to specify the directory for the downloaded file (since users may have different file/folder structures in their computers).
I just want the file to be downloaded to the 'Downloads' folder directly from the browser.
I tried something like this:
public string DownloadBlob(string name, string containerName){
BlobContainerClient container = GetContainer(containerName);
BlobClient blobClient = container.GetBlobClient(name);
blobClient.DownloadContent();
return name;
}
But this is definitely not working. Just need the file to be downloaded normally without refreshing the site.
Thanks in advance.
Upvotes: 0
Views: 362
Reputation: 1556
I create an Asp.net Core MVC Project for downloading Blob files from Azure Blob Storage, which directly downloads files to the Download folder from the browser.
I deployed this project on Azure App Service.
The complete code for downloading Blobs to the Download folder directly from the browser is provided below.
BlobController:
using Microsoft.AspNetCore.Mvc;
using MyBlobStorageApp.Services;
using System.Threading.Tasks;
namespace MyBlobStorageApp.Controllers
{
public class BlobController : Controller
{
private readonly BlobService _blobService;
public BlobController(BlobService blobService)
{
_blobService = blobService;
}
[HttpGet]
public async Task<IActionResult> DownloadBlob(string containerName, string blobName)
{
var blobStream = await _blobService.DownloadBlobAsync(containerName, blobName);
return new FileStreamResult(blobStream, "application/octet-stream")
{
FileDownloadName = blobName
};
}
[HttpGet]
public IActionResult Index()
{
return View();
}
}
}
Service/BlobService:
using Azure.Storage.Blobs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Threading.Tasks;
namespace MyBlobStorageApp.Services
{
public class BlobService
{
private readonly string _connectionString;
private readonly ILogger<BlobService> _logger;
public BlobService(IConfiguration configuration, ILogger<BlobService> logger)
{
_logger = logger;
_connectionString = configuration["AzureBlobStorage:ConnectionString"];
if (string.IsNullOrEmpty(_connectionString))
{
_logger.LogError("Azure Blob Storage connectionstring is null or empty.");
throw new ArgumentNullException(nameof(_connectionString), "Azure Blob Storage connectionstring is null or empty.");
}
_logger.LogInformation("Azure Blob Storage connection string successfully retrieved.");
}
public async Task<Stream> DownloadBlobAsync(string containerName, string blobName)
{
try
{
var blobServiceClient = new BlobServiceClient(_connectionString);
var containerClient = blobServiceClient.GetBlobContainerClient(containerName);
var blobClient = containerClient.GetBlobClient(blobName);
var downloadInfo = await blobClient.DownloadAsync();
return downloadInfo.Value.Content;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error downloading blob");
throw;
}
}
}
}
View/Blob/Index.cshtml:
@{
ViewData["Title"] = "Blob Index";
}
<h2>Blob Index</h2>
<form method="get" action="/Blob/DownloadBlob">
<div>
<label>Container Name:</label>
<input type="text" name="containerName" />
</div>
<div>
<label>Blob Name:</label>
<input type="text" name="blobName" />
</div>
<button type="submit">Download Blob</button>
</form>
appsettings.json:
{
"AzureBlobStorage": {
"ConnectionString": "DefaultEndpointsProtocol=https;AccountName=<StorageName>;AccountKey=<KeyValue>EndpointSuffix=core.windows.net"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Below is the file I want to download.
By using the above code, I successfully downloaded the file, as shown below.
Loacal Output:
It directly saves to the Downloads folder, as shown below.
After deploying to the Azure App Service, I can download the file without any issues, as shown below.
Azure Web App Output:
Upvotes: 0