DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Attachments/Providers/AzureBlobService.cs

44 lines
1.5 KiB
C#
Raw Normal View History

2023-08-15 22:52:30 -05:00

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
using DamageAssesment.Api.Attachments.Interfaces;
namespace DamageAssesment.Api.Attachments.Providers
{
public class AzureBlobService: IAzureBlobService
{
BlobServiceClient _blobClient;
BlobContainerClient _containerClient;
string azureConnectionString = "<Primary Connection String>";
public AzureBlobService()
{
_blobClient = new BlobServiceClient(azureConnectionString);
_containerClient = _blobClient.GetBlobContainerClient("apiimages");
}
public async Task<List<Azure.Response<BlobContentInfo>>> UploadFiles(List<IFormFile> files)
{
var azureResponse = new List<Azure.Response<BlobContentInfo>>();
foreach (var file in files)
{
string fileName = file.FileName;
using (var memoryStream = new MemoryStream())
{
file.CopyTo(memoryStream);
memoryStream.Position = 0;
var client = await _containerClient.UploadBlobAsync(fileName, memoryStream, default);
azureResponse.Add(client);
}
};
return azureResponse;
}
public void DeleteFile(string url)
{
var blob = _containerClient.GetBlockBlobClient(url);
blob.DeleteIfExists();
}
}
}