DevelopmentHelpers.Storage.Core
9.0.4
dotnet add package DevelopmentHelpers.Storage.Core --version 9.0.4
NuGet\Install-Package DevelopmentHelpers.Storage.Core -Version 9.0.4
<PackageReference Include="DevelopmentHelpers.Storage.Core" Version="9.0.4" />
<PackageVersion Include="DevelopmentHelpers.Storage.Core" Version="9.0.4" />
<PackageReference Include="DevelopmentHelpers.Storage.Core" />
paket add DevelopmentHelpers.Storage.Core --version 9.0.4
#r "nuget: DevelopmentHelpers.Storage.Core, 9.0.4"
#addin nuget:?package=DevelopmentHelpers.Storage.Core&version=9.0.4
#tool nuget:?package=DevelopmentHelpers.Storage.Core&version=9.0.4
DevelopmentHelpers.Storage.Core
This is a library designed to simplify interactions with Azure Storage, Azure FileShare, and local directory structures. It provides an easy-to-use API for uploading and downloading entire directory structures with minimal commands.
Code Example Following example shows how to use Azure Storage Classes
Supports both Local & Azure Storage: You can work with local directories or Azure Storage seamlessly. Dependency Injection Support: The library allows integration into applications using dependency injection. FileShare Management: Provides functionality to interact with Azure FileShare. Container Synchronization: Helps sync containers between different storage accounts. Configuration-Based Usage: You can define storage settings in appsettings.json for easy integration.
Create IStorage -- IStorage interface has two implementation local & Azure:use any dependency injection container
IStorage storage = new AzureStorage(config,logger);
IStorage storage = new FileSystemStorage(config,logger);
Create IAzureFileStorage to use FileShare use dependency injection container
services.AddAzureStorage(configuration);
IAzureFileStorage fileStorage = new AzureFileStorage(AzureFileStorageConfiguration,logger);
Use methods available on the interface
UploadDirectoryAsync(DirectoryInfo directory, string container);
To Add in the Web Application add the configuration in appsettings.json file
"DevelopmentHelpers" :
{
"AzureConfiguration": {
"AccountName": "Account-Name",
"AccountKey": "Account-Key",
"UseHttps": "True"
},
"AzureFileStorageConfiguration": {
"ConnectionString": "Connection-String",
"ShareName": "Share-Name"
}
}
Add the services in program.cs file
services.AddAzureStorage(Configuration);
To Use in a constructor
private readonly IStorage _storage;
private readonly IAzureFileStorage _fileStorage;
public IndexModel(IStorage storage,IAzureFileStorage fileStorage)
{
_storage = storage; AzureStorage azureStorage = new(new Models.AzureConfiguration
{
AccountKey = ConfigHelper.Instance.AzureConfiguration.AccountKey,
AccountName = ConfigHelper.Instance.AzureConfiguration.AccountName,
UseHttps = true
}, ConfigHelper.Instance.LoggerFactory.CreateLogger<AzureStorage>());
string tempDirectoryPath = Path.Combine(_localDirectoryPath, sourceContainerName);
if (Directory.Exists(tempDirectoryPath))
{
Directory.Delete(tempDirectoryPath, true);
}
if (!await azureStorage.ContainerExistsAsync(sourceContainerName))
{
var tempPath = _dirHelper.CreateTempDirectory(tempDirectoryPath);
_ = await az
_fileStorage= fileStorage;
}
To Sync Containers between storage accounts
ureStorage.UploadDirectoryAsync(tempPath, sourceContainerName);
}
AzureStorageSyncHelper syncHelper = new(azureStorage, azureStorage);
var (Success, _) = await syncHelper.SyncContainersAsync(sourceContainerName, targetContainerName, false, _localDirectoryPath);
To Get files exist in Target Container but not in local directory use
AzureStorage azureStorage = new(new Models.AzureConfiguration
{
AccountKey = ConfigHelper.Instance.AzureConfiguration.AccountKey,
AccountName = ConfigHelper.Instance.AzureConfiguration.AccountName,
UseHttps = true
}, ConfigHelper.Instance.LoggerFactory.CreateLogger<AzureStorage>());
IDirectoryToContainerSyncHelper directorySyncHelper = new DirectoryToContainerSyncHelper(azureStorage);
var response = await directorySyncHelper.GetBlobUrlListExistInTargetButNotInSourceAsync(sourceDirectoryPath, targetContainerName);
Assert.IsNotNull(response.blobUrls);
To Get blobs exist in Target but not in Source use, create target storage and source storage separately if needed, sample uses same storage account
AzureStorage azureStorage = new(new Models.AzureConfiguration
{
AccountKey = ConfigHelper.Instance.AzureConfiguration.AccountKey,
AccountName = ConfigHelper.Instance.AzureConfiguration.AccountName,
UseHttps = true
}, ConfigHelper.Instance.LoggerFactory.CreateLogger<AzureStorage>());
IContainerToContainerSyncHelper syncHelper = new ContainerToContainerSyncHelper(azureStorage, azureStorage);
var (BlobUrls, Message) = await syncHelper.GetBlobUrlListExistInTargetButNotInSourceAsync(sourceContainerName, targetContainerName);
Motivation
I needed a consistent and easy to use library in .net Standard which I can use it with any project, and be able to download and upload complete directories.
Tests
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using FileHelper = DevelopmentHelpers.Storage.Core.Tests.Helpers.FileHelper;
namespace DevelopmentHelpers.Storage.Core.Tests
{
[TestClass]
public class AzureStorageCoreTest
{
static IStorage _storage;
static StringHelper _stringHelper;
private static string _localDirectoryPath;
static DirectoryHelper _dirHelper;
[ClassInitialize]
public static void AzureStorageTestInit(TestContext context)
{
_storage = new AzureStorage(ConfigHelper.Instance.AzureConfiguration, ConfigHelper.Instance.LoggerFactory.CreateLogger<AzureStorage>());
_stringHelper = new StringHelper();
_dirHelper = new DirectoryHelper();
_localDirectoryPath = Path.Combine("C:\\temp", "AzureStorageCoreTest");
if (Directory.Exists(_localDirectoryPath))
{
Directory.Delete(_localDirectoryPath, true);
}
Directory.CreateDirectory(_localDirectoryPath);
Console.WriteLine("AzureStorageTestInit " + context.TestName);
}
[TestMethod]
public void ValidateTest()
{
Assert.IsTrue(_storage.Validate());
}
[TestMethod]
public void CreateContainerAndDeleteContainerTest()
{
string testContainerName = $"{Guid.NewGuid():N}";
var response = _storage.CreateContainerAsync(testContainerName, false).Result;
Assert.IsNotNull(response);
response = _storage.CreateContainerAsync(testContainerName, false).Result;
Assert.IsNull(response);//because container already exists
Assert.IsTrue(_storage.DeleteAsync(testContainerName).Result);
}
[TestMethod]
[DataRow(5, 1024)]
[DataRow(3, 36870637)]
public void AzureUploadFileStreamTest(int count, long size)
{
string containerName = $"{Guid.NewGuid():N}";
string tempDirectoryPath = Path.Combine(_localDirectoryPath, containerName);
FileHelper fileHelper = new(tempDirectoryPath);
for (int i = 0; i < count; i++)
{
string fileName = $"{Guid.NewGuid()}.txt";
fileHelper.CreateFile(size, fileName);
FileInfo fileInfo = new(Path.Combine(tempDirectoryPath, fileName));
FileStream fileStream = fileInfo.OpenRead();
MemoryStream memoryStream = new();
fileStream.CopyTo(memoryStream);
Assert.IsNotNull(_storage.UploadFileStreamAsync(memoryStream, MimeTypes.txt, containerName, fileInfo.Name).Result);
var blobUri = _storage.GetUri(containerName, fileName);
Assert.IsNotNull(blobUri);
}
}
[TestMethod]
[DataRow(1, 1024)]
[DataRow(1, 36870637)]
public void AzureUploadFileInfoTest(int count, long size)
{
string containerName = $"{Guid.NewGuid():N}";
string tempDirectoryPath = Path.Combine(_localDirectoryPath, containerName);
FileHelper fileHelper = new(tempDirectoryPath);
for (int i = 0; i < count; i++)
{
string fileName = $"{Guid.NewGuid()}.txt";
fileHelper.CreateFile(size, fileName);
FileInfo fileInfo = new(Path.Combine(tempDirectoryPath, fileName));
Assert.IsNotNull(_storage.UploadFileAsync(fileInfo, MimeTypes.txt, containerName).Result);
var blobUri = _storage.GetUri(containerName, fileName);
Assert.IsNotNull(blobUri);
bool exists = _storage.BlobExistsAsync(blobUri).Result;
Assert.IsTrue(exists);
}
}
[TestMethod]
public void CheckBlobExists()
{
bool exists = _storage.BlobExistsAsync("https://thisisdummybloburl.azurewebsites.net").Result;
Assert.IsFalse(exists);
}
[TestMethod]
[DataRow(1, 0)]
[DataRow(1, 1024)]
[DataRow(1, 4194304)] //4mb
[DataRow(1, 36870637)]
[DataRow(1, 536870637)]
// [DataRow(1, 1073741824)] //1 GB
// [DataRow(1, 2147483648)]// 2 GB
// [DataRow(1, 5368709120)]// 5 gb
// [DataRow(1, 10737418240)]// 10 GB
public void AzureUploadFileInChunksTest(int count, long size)
{
string containerName = $"{Guid.NewGuid():N}";
string tempDirectoryPath = Path.Combine(_localDirectoryPath, containerName);
FileHelper fileHelper = new(tempDirectoryPath);
for (int i = 0; i < count; i++)
{
string fileName = $"{Guid.NewGuid()}.txt";
fileHelper.CreateFile(size, fileName);
string completeFilePath = Path.Combine(tempDirectoryPath, fileName);
var uri = _storage.UploadInChunks(completeFilePath, containerName, fileName, MimeTypes.txt).Result;
Assert.IsNotNull(uri);
}
}
[TestMethod]
[DataRow(1, 1024)]
[DataRow(1, 4194304)] //4mb
[DataRow(1, 36870637)]
[DataRow(1, 536870637)]
//[DataRow(1, 1073741824)] //1 GB
//[DataRow(1, 2147483648)]// 2 GB
//[DataRow(1, 5368709120)]// 5 gb
//[DataRow(1, 10737418240)]// 10 GB
public void AzureDownloadFileTest(int count, long size)
{
string containerName = $"{Guid.NewGuid():N}";
string tempDirectoryPath = Path.Combine(_localDirectoryPath, containerName);
string downloadDirectoryPath = Path.Combine(_localDirectoryPath, $"{Guid.NewGuid():N}");
Directory.CreateDirectory(downloadDirectoryPath);
FileHelper fileHelper = new(tempDirectoryPath);
for (int i = 0; i < count; i++)
{
string fileName = $"{Guid.NewGuid()}.txt";
fileHelper.CreateFile(size, fileName);
FileInfo fileInfo = new(Path.Combine(tempDirectoryPath, fileName));
Assert.IsNotNull(_storage.UploadFileAsync(fileInfo, MimeTypes.txt, containerName).Result);
var blobUri = _storage.GetUri(containerName, fileName);
Assert.IsNotNull(blobUri);
//Download
var file = _storage.DownloadToFileAsync(blobUri, downloadDirectoryPath).Result;
Assert.IsNotNull(file);
}
}
[TestMethod]
public void AzureDownloadFileTest()
{
string containerName = $"{Guid.NewGuid():N}";
string tempDirectoryPath = Path.Combine(_localDirectoryPath, containerName);
Directory.CreateDirectory(tempDirectoryPath);
string testfile = Path.Combine(tempDirectoryPath, $"{Guid.NewGuid()}.txt");
_stringHelper.CreatetestFile(testfile);
FileInfo fileInfo = new(testfile);
var url = _storage.UploadFileAsync(fileInfo, MimeTypes.txt, containerName).Result;
var file = _storage.DownloadToFileAsync(url, tempDirectoryPath).Result;
Assert.IsNotNull(file);
}
[TestMethod]
public void AzureDownloadToStreamAsyncTest()
{
string containerName = $"{Guid.NewGuid():N}";
string tempDirectoryPath = Path.Combine(_localDirectoryPath, containerName);
Directory.CreateDirectory(tempDirectoryPath);
string testfile = Path.Combine(tempDirectoryPath, $"{Guid.NewGuid()}.txt");
_stringHelper.CreatetestFile(testfile);
FileInfo fileInfo = new(testfile);
var url = _storage.UploadFileAsync(fileInfo, MimeTypes.txt, containerName).Result;
var file = _storage.DownloadToStreamAsync(url, tempDirectoryPath);
Assert.IsNotNull(file);
}
[TestMethod]
public void GetStorageContainerTest()
{
string tempDirectory = $"{Guid.NewGuid():N}";
string tempDirectoryPath = Path.Combine(_localDirectoryPath, tempDirectory);
_dirHelper.CreateTempDirectory(tempDirectoryPath);
StorageContainer storageContainer = _storage.GetStorageContainerAsync(tempDirectory).Result;
Assert.IsNotNull(storageContainer);
}
[TestMethod]
public void DownloadContainerTest()
{
string containerName = $"{Guid.NewGuid():N}";
string tempDirectoryPath = Path.Combine(_localDirectoryPath, containerName);
_dirHelper.CreateTempDirectory(tempDirectoryPath);
//Upload the directory
var created = _storage.UploadDirectoryAsync(tempDirectoryPath, containerName).Result;
Assert.IsNotNull(created);
string downloadDirectory = $"{Guid.NewGuid():N}";
string downloadDirectoryPath = Path.Combine(_localDirectoryPath, downloadDirectory);
_ = _storage.DownloadContainer(containerName, downloadDirectoryPath).Result;
bool areIdentical = DirectoryHelper.CompareDirectories(tempDirectoryPath, downloadDirectoryPath);
Assert.IsTrue(areIdentical);
}
[TestMethod]
public void AzureUploadDirectoryAndZipAsyncTest()
{
string containerName = $"{Guid.NewGuid():N}";
string tempDirectoryPath = Path.Combine(_localDirectoryPath, containerName);
var tempPath = _dirHelper.CreateTempDirectory(tempDirectoryPath);
DirectoryInfo info = new(tempPath);
var created = _storage.UploadDirectoryAsync(info, containerName).Result;
Assert.IsNotNull(created);
string zipFolder = "Zip";
string zipFolderPath = Path.Combine(_localDirectoryPath, zipFolder);
if (!Directory.Exists(zipFolderPath))
Directory.CreateDirectory(zipFolderPath);
var zipFile = _storage.CreateZipFromContainerAsync(info.Name, zipFolderPath, $"{containerName}.zip")
.Result;
Assert.IsNotNull(zipFile);
}
[TestMethod]
public void AzureUploadDirectoryAsyncTest()
{
AzureStorage azureStorage = new(new Models.AzureConfiguration
{
AccountKey = ConfigHelper.Instance.AzureConfiguration.AccountKey,
AccountName = ConfigHelper.Instance.AzureConfiguration.AccountName,
UseHttps = true
}, ConfigHelper.Instance.LoggerFactory.CreateLogger<AzureStorage>());
string containerName = $"{Guid.NewGuid():N}";
string tempDirectoryPath = Path.Combine(_localDirectoryPath, containerName);
var tempPath = _dirHelper.CreateTempDirectory(tempDirectoryPath);
var created = azureStorage.UploadDirectoryAsync(tempPath, containerName).Result;
Assert.IsNotNull(created);
}
[TestMethod]
public void AzureDownloadStorageContainerTest()
{
string containerName = $"{Guid.NewGuid():N}";
string tempDirectoryPath = Path.Combine(_localDirectoryPath, containerName);
var tempPath = _dirHelper.CreateTempDirectory(tempDirectoryPath);
DirectoryInfo info = new(tempPath);
var created = _storage.UploadDirectoryAsync(info, containerName).Result;
Assert.IsNotNull(created);
var storageContainer = _storage.GetStorageContainerAsync(containerName).Result;
Assert.IsNotNull(storageContainer);
}
[TestMethod]
public void AzureSystemUploadFileAsyncInfoTest()
{
//Test File
string fileName = $"{Guid.NewGuid()}.txt";
string testFile = Path.Combine(Path.GetTempPath(), fileName);
_stringHelper.CreatetestFile(testFile);
FileInfo fileInfo = new(testFile);
//Container
string containerName = $"{Guid.NewGuid():N}";
var url = _storage.UploadFileAsync(fileInfo, MimeTypes.txt, containerName).Result;
Assert.IsNotNull(url);
Assert.IsNotNull(_storage.GetUri(containerName, fileName));
var file = _storage.DownloadToFileAsync(url, _localDirectoryPath).Result;
Assert.IsNotNull(file);
var stream = _storage.DownloadToStreamAsync(url, _localDirectoryPath).Result;
var tempFilePath = Path.Combine(_localDirectoryPath, "test.txt");
if (File.Exists(tempFilePath))
File.Delete(tempFilePath);
using (FileStream fileStream = new(tempFilePath, FileMode.Create, FileAccess.Write))
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
fileStream.Write(bytes, 0, bytes.Length);
stream.Close();
}
Assert.IsNotNull(stream);
}
[TestMethod]
[DataRow("/help/new/add/")]
public async Task AzureBlobUploadToNewContainerAsyncInfoTest(string prefix)
{
//Test File
string fileName = $"{Guid.NewGuid()}.txt";
string testFile = Path.Combine(Path.GetTempPath(), fileName);
_stringHelper.CreatetestFile(testFile);
FileInfo fileInfo = new(testFile);
//Container
string containerName = $"{Guid.NewGuid():N}";
containerName= containerName.ToLower();
var blobUrl = await _storage.UploadFileAsync($"{prefix}{fileName}",fileInfo.FullName, MimeTypes.txt, containerName);
Assert.IsNotNull(blobUrl);
Assert.IsNotNull(_storage.GetUri(containerName, fileName));
var downloadedFile = await _storage.DownloadToFileAsync(blobUrl, _localDirectoryPath);
Assert.IsNotNull(downloadedFile);
BlobContainerClient blobContainerClient = _storage.Account.GetBlobContainerClient(containerName);
BlobClient blobClient = blobContainerClient.GetBlobClient(blobUrl);
var blobNameWithPrefix = blobClient.Name.Replace(blobContainerClient.Uri.AbsoluteUri, "");
string newContainerName = $"{containerName.ToLower()}new";
var newBlobUrl = await _storage.UploadFileAsync($"{blobNameWithPrefix}", downloadedFile, MimeTypes.txt, newContainerName);
Assert.IsNotNull(newBlobUrl);
BlobContainerClient blobContainerClientNew = _storage.Account.GetBlobContainerClient(newContainerName);
BlobClient blobClientNew = blobContainerClientNew.GetBlobClient(newBlobUrl);
var blobNameWithPrefixNew = blobClientNew.Name.Replace(blobContainerClientNew.Uri.AbsoluteUri, "");
await _storage.DeleteAsync(containerName);
await _storage.DeleteAsync(newContainerName);
Assert.IsTrue(blobNameWithPrefixNew.Equals(blobNameWithPrefix));
}
[TestMethod]
[DataRow("/help/new/add/")]
[DataRow("")]
public async Task AzureBlobUploadInchunksToNewContainerAsyncInfoTest(string prefix)
{
AzureStorage azureStorage = _storage as AzureStorage;
//Test File
string fileName = $"{Guid.NewGuid()}.txt";
string testFile = Path.Combine(Path.GetTempPath(), fileName);
_stringHelper.CreatetestFile(testFile);
FileInfo fileInfo = new(testFile);
//Container
string containerName = $"{Guid.NewGuid():N}";
containerName = containerName.ToLower();
var blobUrl = await _storage.UploadInChunks(fileInfo.FullName, containerName, $"{prefix}{fileName}", MimeTypes.txt);
Assert.IsNotNull(blobUrl);
Assert.IsNotNull(_storage.GetUri(containerName, fileName));
var downloadedFile = await _storage.DownloadToFileAsync(blobUrl, _localDirectoryPath);
Assert.IsNotNull(downloadedFile);
var blobNameWithPrefix = azureStorage.GetBlobNameWithPrefix(blobUrl, containerName); string newContainerName = $"{containerName.ToLower()}new";
string newBlobUrl = await _storage.UploadInChunks(downloadedFile, newContainerName,blobNameWithPrefix, MimeTypes.txt);
Assert.IsNotNull(newBlobUrl);
var blobNameWithPrefixNew = azureStorage.GetBlobNameWithPrefix(newBlobUrl, newContainerName);
await _storage.DeleteAsync(containerName);
await _storage.DeleteAsync(newContainerName);
Assert.IsTrue(blobNameWithPrefixNew.Equals(blobNameWithPrefix));
}
[TestMethod]
public void CreateLargeFileZipTest()
{
//Create local Temp Directory where Container will be downloaded
string zipFolder = "LargeFolder";
string zipFolderPath = Path.Combine(_localDirectoryPath, zipFolder);
if (Directory.Exists(zipFolderPath))
Directory.Delete(zipFolderPath, true);
//Create and upload temp directory to Azure Container
string tempContainer = $"{Guid.NewGuid():N}";
string tempDirectoryPath = Path.Combine(_localDirectoryPath, tempContainer);
var tempPath = _dirHelper.CreateTempDirectory(tempDirectoryPath, 1);
DirectoryInfo info = new DirectoryInfo(tempPath);
var uploaded = _storage.UploadDirectoryAsync(info, tempContainer).Result;
Assert.IsNotNull(uploaded);
var zipFile = _storage.CreateZipFromContainerAsync(tempContainer, zipFolderPath, $"{tempContainer}.zip")
.Result;
Assert.IsNotNull(zipFile);
}
[TestMethod]
public void AzureGetContainerTest()
{
var blobContainerList = _storage.GetContainersAsync().Result;
Assert.IsInstanceOfType(blobContainerList, typeof(List<BlobContainerItem>));
Assert.IsTrue(true);
}
//
[TestMethod]
[DataRow(5, 1024)]
public void ListBlobsAsyncTest(int count, long size)
{
string containerName = $"{Guid.NewGuid():N}";
string tempDirectoryPath = Path.Combine(_localDirectoryPath, containerName);
FileHelper fileHelper = new(tempDirectoryPath);
for (int i = 0; i < count; i++)
{
string fileName = $"{Guid.NewGuid()}.txt";
fileHelper.CreateFile(size, fileName);
}
DirectoryInfo infos = new(tempDirectoryPath);
foreach (var fileInfo in infos.GetFiles())
{
var url = _storage.UploadFileAsync(fileInfo, MimeTypes.txt, containerName).Result;
Assert.IsNotNull(url);
}
var blobItems = _storage.ListBlobsAsync(containerName).Result;
Assert.IsInstanceOfType(blobItems, typeof(List<BlobItem>));
//Delete the container
_storage.DeleteAsync(containerName);
Assert.IsTrue(true);
}
[TestMethod]
public void LocalGetContainerTest()
{
//Create local Temp Directory where Container will be downloaded
string localTempDirectoryDownload = "localTempDirectoryDownload";
string localTempDirectoryDownloadPath = Path.Combine(_localDirectoryPath, localTempDirectoryDownload);
if (Directory.Exists(localTempDirectoryDownloadPath))
Directory.Delete(localTempDirectoryDownloadPath);
//Create and upload temp directory to Azure Container
string tempContainer = $"{Guid.NewGuid():N}";
string tempDirectoryPath = Path.Combine(_localDirectoryPath, tempContainer);
var tempPath = _dirHelper.CreateTempDirectory(tempDirectoryPath);
DirectoryInfo info = new(tempPath);
var created = _storage.UploadDirectoryAsync(info, tempContainer).Result;
Assert.IsNotNull(created);
var storageContainer = _storage.GetStorageContainerAsync(tempContainer).Result;
Assert.IsNotNull(storageContainer);
//Save Container to Local Directory
var localSavedPath = _storage.SaveStorageContainerAsync(storageContainer, localTempDirectoryDownloadPath).Result;
Assert.IsNotNull(localSavedPath);
}
[TestMethod]
public async Task AzureUploadFileNameWithSpacesAndDownloadTestAsync()
{
string containerName = $"{Guid.NewGuid():N}";
string TestFileDirectory = Path.Combine(Environment.CurrentDirectory, "TestFiles");
string downloadFileDirectory = Path.Combine(_localDirectoryPath, "DownloadedFiles");
if (!Directory.Exists(downloadFileDirectory))
Directory.CreateDirectory(downloadFileDirectory);
DirectoryInfo directoryInfo = new(TestFileDirectory);
foreach(FileInfo fileInfo in directoryInfo.GetFiles())
{
var blobUri = await _storage.UploadFileAsync(fileInfo, MimeTypes.txt, containerName);
Assert.IsNotNull(blobUri);
var downloadedFile = await _storage.DownloadToFileAsync(blobUri, downloadFileDirectory);
Assert.IsNotNull(downloadedFile);
}
}
[TestMethod]
public async Task AzureUploadDirectoryWithSpacesAndDownloadTestAsync()
{
string containerName = $"{Guid.NewGuid():N}";
string TestDirectory = Path.Combine(Environment.CurrentDirectory, "TestFiles");
string downloadDirectory = Path.Combine(_localDirectoryPath, "DownloadedDirectory");
var uploadedDirectory = await _storage.UploadDirectoryAsync(TestDirectory, containerName);
Assert.IsNotNull(uploadedDirectory);
var downloadedDirectory = await _storage.DownloadContainer(containerName, downloadDirectory);
Assert.IsNotNull(downloadedDirectory);
StorageContainer storageContainer = await _storage.GetStorageContainerAsync(containerName);
Assert.IsNotNull(storageContainer);
}
[TestMethod]
[DataTestMethod]
[DataRow("source","target")]
public async Task SyncContainersTestWhenTargetDoesnotExistAsync(string sourceContainerName, string targetContainerName)
{
AzureStorage azureStorage = new(new Models.AzureConfiguration
{
AccountKey = ConfigHelper.Instance.AzureConfiguration.AccountKey,
AccountName = ConfigHelper.Instance.AzureConfiguration.AccountName,
UseHttps = true
}, ConfigHelper.Instance.LoggerFactory.CreateLogger<AzureStorage>());
string tempDirectoryPath = Path.Combine(_localDirectoryPath, sourceContainerName);
if (Directory.Exists(tempDirectoryPath))
{
Directory.Delete(tempDirectoryPath, true);
}
if (!await azureStorage.ContainerExistsAsync(sourceContainerName))
{
var tempPath = _dirHelper.CreateTempDirectory(tempDirectoryPath);
_ = await azureStorage.UploadDirectoryAsync(tempPath, sourceContainerName);
}
ContainerToContainerSyncHelper syncHelper = new(azureStorage, azureStorage);
var (Success, _) = await syncHelper.SyncSourceToTargetContainersAsync(sourceContainerName, targetContainerName, false, _localDirectoryPath);
Assert.IsTrue(Success);
}
[TestMethod]
[DataTestMethod]
public async Task SyncContainersTestWhenTargetExistAsync()
{
string sourceContainerName = $"{Guid.NewGuid():N}";
string targetContainerName = $"{Guid.NewGuid():N}";
AzureStorage azureStorage = new(new Models.AzureConfiguration
{
AccountKey = ConfigHelper.Instance.AzureConfiguration.AccountKey,
AccountName = ConfigHelper.Instance.AzureConfiguration.AccountName,
UseHttps = true
}, ConfigHelper.Instance.LoggerFactory.CreateLogger<AzureStorage>());
string sourceDirectoryPath = Path.Combine(_localDirectoryPath, sourceContainerName);
if (Directory.Exists(sourceDirectoryPath))
{
Directory.Delete(sourceDirectoryPath, true);
}
string targetDirectoryPath = Path.Combine(_localDirectoryPath, targetContainerName);
if (Directory.Exists(targetDirectoryPath))
{
Directory.Delete(targetDirectoryPath, true);
}
if (!await azureStorage.ContainerExistsAsync(sourceContainerName))
{
sourceDirectoryPath = _dirHelper.CreateDirectoryWithTempData(sourceDirectoryPath);
_ = await azureStorage.UploadDirectoryAsync(sourceDirectoryPath, sourceContainerName);
_ = await azureStorage.UploadDirectoryAsync(sourceDirectoryPath, targetContainerName);
//add some files so containers do not match
for (int i = 0; i < 3; i++)
{
string fileName = $"deleteme_{i}.txt";
FileHelper fileHelper = new(targetDirectoryPath);
fileHelper.CreateFile(1024, fileName);
FileInfo fileInfo = new(Path.Combine(targetDirectoryPath, fileName));
await azureStorage.UploadFileAsync(fileInfo, MimeTypes.txt, targetContainerName);
}
}
ContainerToContainerSyncHelper syncHelper = new(azureStorage, azureStorage);
var (Success, Message) = await syncHelper.SyncSourceToTargetContainersAsync(sourceContainerName, targetContainerName, false, _localDirectoryPath);
Assert.IsNotNull(Message);
Assert.IsTrue(Success);
}
[TestMethod]
[DataTestMethod]
public async Task GetBlobsRequireDeletionFromTargetContainerAsync()
{
string sourceContainerName = $"{Guid.NewGuid():N}";
string targetContainerName = $"{Guid.NewGuid():N}";
AzureStorage azureStorage = new(new Models.AzureConfiguration
{
AccountKey = ConfigHelper.Instance.AzureConfiguration.AccountKey,
AccountName = ConfigHelper.Instance.AzureConfiguration.AccountName,
UseHttps = true
}, ConfigHelper.Instance.LoggerFactory.CreateLogger<AzureStorage>());
string sourceDirectoryPath = Path.Combine(_localDirectoryPath, sourceContainerName);
if (Directory.Exists(sourceDirectoryPath))
{
Directory.Delete(sourceDirectoryPath, true);
}
string targetDirectoryPath = Path.Combine(_localDirectoryPath, targetContainerName);
if (Directory.Exists(targetDirectoryPath))
{
Directory.Delete(targetDirectoryPath, true);
}
if (!await azureStorage.ContainerExistsAsync(sourceContainerName))
{
sourceDirectoryPath = _dirHelper.CreateDirectoryWithTempData(sourceDirectoryPath);
_ = await azureStorage.UploadDirectoryAsync(sourceDirectoryPath, sourceContainerName);
_ = await azureStorage.UploadDirectoryAsync(sourceDirectoryPath, targetContainerName);
//add some files so containers do not match
for (int i = 0; i < 3; i++)
{
string fileName = $"deleteme_{i}.txt";
FileHelper fileHelper = new(targetDirectoryPath);
fileHelper.CreateFile(1024, fileName);
FileInfo fileInfo = new(Path.Combine(targetDirectoryPath, fileName));
await azureStorage.UploadFileAsync(fileInfo, MimeTypes.txt, targetContainerName);
}
}
IContainerToContainerSyncHelper syncHelper = new ContainerToContainerSyncHelper(azureStorage, azureStorage);
var (BlobUrls, Message) = await syncHelper.GetBlobUrlListExistInTargetButNotInSourceAsync(sourceContainerName, targetContainerName);
Assert.IsNotNull(BlobUrls);
Assert.IsTrue(BlobUrls.Count == 3);
}
[TestMethod]
[DataTestMethod]
public async Task GetFilesRequireDeletionFromTargetContainerAsync()
{
string sourceContainerName = $"{Guid.NewGuid():N}";
string targetContainerName = $"{Guid.NewGuid():N}";
AzureStorage azureStorage = new(new Models.AzureConfiguration
{
AccountKey = ConfigHelper.Instance.AzureConfiguration.AccountKey,
AccountName = ConfigHelper.Instance.AzureConfiguration.AccountName,
UseHttps = true
}, ConfigHelper.Instance.LoggerFactory.CreateLogger<AzureStorage>());
string sourceDirectoryPath = Path.Combine(_localDirectoryPath, sourceContainerName);
if (Directory.Exists(sourceDirectoryPath))
{
Directory.Delete(sourceDirectoryPath, true);
}
string targetDirectoryPath = Path.Combine(_localDirectoryPath, targetContainerName);
if (Directory.Exists(targetDirectoryPath))
{
Directory.Delete(targetDirectoryPath, true);
}
if (!await azureStorage.ContainerExistsAsync(sourceContainerName))
{
sourceDirectoryPath = _dirHelper.CreateDirectoryWithTempData(sourceDirectoryPath);
_ = await azureStorage.UploadDirectoryAsync(sourceDirectoryPath, targetContainerName);
//add some files so containers do not match
for (int i = 0; i < 3; i++)
{
string fileName = $"deleteme_{i}.txt";
FileHelper fileHelper = new(targetDirectoryPath);
fileHelper.CreateFile(1024, fileName);
FileInfo fileInfo = new(Path.Combine(targetDirectoryPath, fileName));
await azureStorage.UploadFileAsync(fileInfo, MimeTypes.txt, targetContainerName);
}
}
IDirectoryToContainerSyncHelper directorySyncHelper = new DirectoryToContainerSyncHelper(azureStorage);
var response = await directorySyncHelper.GetBlobUrlListExistInTargetButNotInSourceAsync(sourceDirectoryPath, targetContainerName);
Assert.IsNotNull(response.blobUrls);
Assert.IsTrue(response.blobUrls.Count == 3);
}
[ClassCleanup]
public static void ClassCleanup()
{
var containers = _storage.GetContainersAsync().Result;
foreach (BlobContainerItem container in containers)
{
var deleted = _storage.DeleteAsync(container.Name).Result;
Assert.IsTrue(deleted);
}
//Delete temp directory
if (Directory.Exists(_localDirectoryPath))
Directory.Delete(_localDirectoryPath, true);
Console.WriteLine("AzureStorageTestInit Cleanup");
}
[AssemblyCleanup]
public static void AssemblyCleanup()
{
Console.WriteLine("Assembly Cleanup");
}
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- Azure.Storage.Blobs (>= 12.24.0)
- Azure.Storage.Files.Shares (>= 12.22.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.5)
- Microsoft.Extensions.DependencyInjection (>= 9.0.5)
- Microsoft.Extensions.Logging (>= 9.0.5)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
9.0.4 | 174 | 5/20/2025 |
9.0.3 | 240 | 5/13/2025 |
9.0.2 | 173 | 4/22/2025 |
9.0.1 | 192 | 4/6/2025 |
9.0.0 | 196 | 11/20/2024 |
7.0.19 | 139 | 11/14/2024 |
7.0.18 | 386 | 10/11/2024 |
7.0.17 | 132 | 10/9/2024 |
7.0.16 | 154 | 9/30/2024 |
7.0.15 | 232 | 8/13/2024 |
7.0.14 | 145 | 8/2/2024 |
7.0.11 | 139 | 5/31/2024 |
7.0.10 | 136 | 5/23/2024 |
7.0.9 | 125 | 5/23/2024 |
7.0.8 | 298 | 4/10/2024 |
7.0.7 | 315 | 2/16/2024 |
7.0.6 | 159 | 2/7/2024 |
7.0.5 | 134 | 1/29/2024 |
7.0.4 | 152 | 1/16/2024 |
7.0.3 | 176 | 12/26/2023 |
7.0.2 | 170 | 12/15/2023 |
7.0.1 | 146 | 12/15/2023 |
7.0.0 | 216 | 11/14/2023 |
6.0.5 | 220 | 10/23/2023 |
6.0.4 | 276 | 8/2/2023 |
6.0.3 | 173 | 7/27/2023 |
6.0.2 | 187 | 7/11/2023 |
6.0.1 | 191 | 6/22/2023 |
6.0.0 | 202 | 5/4/2023 |
5.0.3 | 237 | 4/27/2023 |
5.0.2 | 273 | 4/11/2023 |
5.0.1 | 301 | 2/22/2023 |
5.0.0 | 428 | 11/10/2022 |
4.0.11 | 381 | 11/10/2022 |
4.0.10 | 373 | 11/10/2022 |
4.0.9 | 494 | 9/6/2022 |
4.0.8 | 545 | 7/25/2022 |
4.0.7 | 491 | 7/25/2022 |
4.0.6 | 484 | 7/25/2022 |
4.0.5 | 513 | 7/14/2022 |
4.0.4 | 194 | 7/14/2022 |
4.0.3 | 607 | 3/24/2022 |
4.0.2 | 260 | 12/16/2021 |
4.0.1 | 218 | 12/16/2021 |
4.0.0 | 966 | 11/29/2021 |
3.0.3 | 668 | 10/15/2020 |
3.0.2 | 544 | 9/30/2020 |
3.0.1 | 511 | 9/29/2020 |
3.0.0 | 556 | 9/16/2020 |
2.0.2 | 688 | 7/8/2020 |
2.0.1 | 664 | 12/25/2019 |
2.0.0 | 595 | 12/25/2019 |
1.0.6 | 626 | 12/5/2019 |
1.0.5 | 729 | 6/2/2019 |
1.0.4 | 836 | 11/30/2018 |
1.0.3 | 876 | 11/12/2018 |
1.0.2 | 847 | 11/12/2018 |
1.0.1 | 853 | 11/12/2018 |
1.0.0 | 820 | 11/12/2018 |
Updated nuget packages