Это то, что я использую для загрузки файла в BLOB-объект Azure:
public interface IFileStorageService
{
Task<string> UploadImageAsync(IFormFile imageFile);
Task<string> UploadVideoAsync(IFormFile videoFile);
}
public class FileStorageService : IFileStorageService
{
private readonly Uri _baseUri = new Uri("YourBlobUrl");
private readonly CloudBlobClient _client;
public FileStorageService()
{
_client = new CloudBlobClient(_baseUri, new StorageCredentials("accoutName", "YourSecretKey"));
}
public async Task<string> UploadImageAsync(IFormFile imageFile)
{
try
{
StreamReader streamReader = new StreamReader(imageFile.OpenReadStream());
var container = _client.GetContainerReference("images");
var blob = container.GetBlockBlobReference(imageFile.FileName +".jpg");
await blob.UploadFromStreamAsync(streamReader.BaseStream);
return new Uri(_baseUri, $"/images/{imageFile.FileName}.jpg").ToString();
}
catch (Exception exception)
{
throw;
}
}
public async Task<string> UploadVideoAsync(IFormFile videoFile)
{
try
{
StreamReader videoStream = new StreamReader(videoFile.OpenReadStream());
var container = _client.GetContainerReference("videos");
var blob = container.GetBlockBlobReference(videoFile.FileName+".mp4");
await blob.UploadFromStreamAsync(videoStream.BaseStream);
return new Uri(_baseUri, $"/videos/{videoFile.FileName}.mp4").ToString();
}
catch (Exception exception)
{
throw;
}
}
}
Теперь вы можете использовать это для загрузки списка IFormFile
, просто просматривая список IFormFile
.