Azure Blob Storage is a scalable object storage service for unstructured data, such as text or binary data. It's designed to store large amounts of data, including images, documents, media files, and backup data. Azure Blob Storage provides a cost-effective and highly available storage solution, making it ideal for various applications like content distribution, data backup, and big data analytics.
Integrating Azure Blob Storage with an ASP.NET Core MVC application involves several steps.
dotnet add package Azure.Storage.Blobs
{ "AzureBlobStorage": { "ConnectionString": "DefaultEndpointsProtocol=https;AccountName=<your-account-name>;AccountKey=<your-account-key>;EndpointSuffix=core.windows.net" } }
public class ImagesController : Controller { private readonly BlobServiceClient _blobClient; private readonly BlobContainerClient _containerClient; const string ContainerName = "my-images-profile"; public ImagesController() { _blobClient = new BlobServiceClient("UseDevelopmentStorage=true"); _containerClient = _blobClient.GetBlobContainerClient(ContainerName); _containerClient.CreateIfNotExists(); } // GET: ImagesController public ActionResult Index() { var blobs = _containerClient.GetBlobs(); return View(blobs); } // GET: ImagesController/Details/5 public ActionResult Details(string blobName) { var blobClient = _containerClient.GetBlobClient(blobName); var url = blobClient.GenerateSasUri(Azure.Storage.Sas.BlobSasPermissions.Read, DateTimeOffset.UtcNow.AddDays(1)); ViewBag.url = url; return View(); } // GET : Downloading file public ActionResult Download(string blobName) { var blobClient = _containerClient.GetBlobClient(blobName); var ms = new MemoryStream(); blobClient.DownloadTo(ms); ms.Position = 0; return File(ms, "application/octet-stream", blobName); } // GET: ImagesController/Upload public ActionResult Upload() { return View(); } // POST: ImagesController/Upload [HttpPost] [ValidateAntiForgeryToken] public ActionResult Upload(IFormFile file) { try { if (file is null || file.Length == 0) { return BadRequest(); } _containerClient.UploadBlob(file.FileName, file.OpenReadStream()); return RedirectToAction(nameof(Index)); } catch { return View(); } } // POST: ImagesController/Delete/5 [HttpGet] public ActionResult Delete(string blobName) { try { var blobClient = _containerClient.GetBlobClient(blobName); blobClient.DeleteIfExists(); return RedirectToAction(nameof(Index)); } catch { return View(); } } }
@using Azure.Storage.Blobs.Models @model Azure.Pageable<BlobItem> <table class="table table-bordered"> <tr> <th>Blob Name</th> <th>Actions</th> </tr> @foreach (var item in Model) { <tr> <td>@item.Name</td> <td> <a asp-action="Details" asp-route-blobName="@item.Name">Preview</a> <a asp-action="Download" asp-route-blobName="@item.Name">Download</a> <a asp-action="Delete" asp-route-blobName="@item.Name">Delete</a> </td> </tr> } </table>
<img src="@ViewBag.url" />
<form asp-action="Upload" enctype="multipart/form-data"> <input type="file" name="file" class="form-control" /> <button type="submit">Save</button> </form>
Azure Blob Storage is a powerful, scalable, and cost-effective storage solution for unstructured data. Integrating it with an ASP.NET Core MVC application is straightforward, allowing developers to build robust applications that can handle large amounts of data efficiently. By following the steps outlined above, you can leverage Azure Blob Storage's capabilities to enhance your web applications with secure and scalable storage solutions.
You can access the code from my AzureEssentialSeries Github Repo. Please give it a start if you like it.
You can watch the Azure Essentials show Episode 3 on CSharpTV for this topic. You can also watch this video on my LinkedIn.