Azure Storage helpers for your dotnet core project.
- Add
IStorageService.cs
andStorageService.cs
to your project. - Install nuget package
Azure.Storage.Blobs
package. - Configure your configuration file: Add
StorageConnectionString
key and value. - Configure your
Startup.cs
file : Addservices.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");