Azure Storage Helpers
Azure Storage helpers for your dotnet core project.
IStorageService.cs
and StorageService.cs
to your project.Azure.Storage.Blobs
package.StorageConnectionString
key and value.Startup.cs
file : Add services.AddSingleton<IStorageService, StorageService>();
List files:
private readonly IStorageService StorageService;
public HomeController(IStorageService StorageService) => StorageService = storageService;
public async Task<IActionResult> ListFiles()
{
List<BlobFile> results = await StorageService.ListFilesAsync("folder/folder", "containerName");
if (results != null)
{
ViewBag.Results = results;
}
return View();
}
Upload file:
private readonly IStorageService StorageService;
public HomeController(IStorageService StorageService) => StorageService = storageService;
[HttpPost]
public async Task<IActionResult> UploadFile([FromForm] IFormFile file)
{
using Stream stream = file.OpenReadStream();
string extension = Path.GetExtension(file.FileName).Trim();
string fileLocation = $"folder/folder/{file.FileName}.{extension}";
await StorageService.UploadStreamAsync(stream, fileLocation, "containerName");
return View();
}
Delete file:
Simply call
await StorageService.DeleteFileAsync("folder/folder/file.extension", "containerName");