This package provides access to the azure blob storage over the azure sdk. It includes basic access functionality to upload and download files.
- reference this package to your main project: https://www.nuget.org/packages/Samhammer.AzureBlobStorage/
- initialize the connection in Program.cs
- add the health check to Program.cs (optional)
- add the connection configuration to the appsettings (if the lib is initialized with IConfiguration in Program.cs)
- inject IAzureBlobStorageService to your service
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDefaultAzureBlobStorage(builder.Configuration);
builder.Services.AddHealthChecks().AddDefaultAzureBlobStorage()"AzureBlobStorageOptions": {
"ConnectionString": "DefaultEndpointsProtocol=https;AccountName=xxxxxx;AccountKey=xxxxxx;EndpointSuffix=core.windows.net",
"ContainerName": "DefaultContainerName",
//The time for expiring the SAS url download file. Format value is "d.HH:mm". Default is 1 day.
"FileUrlExpires ": "1.00:00",
},//Reference https://github.com/microsoft/Microsoft.IO.RecyclableMemoryStream.
"StreamManagerOptions": {
//Maximum number of bytes to keep available in the small pool before future buffers get dropped for garbage collection.
"MaxSmallPoolFreeBytes": 1000000,
//Maximum number of bytes to keep available in the large pool before future buffers get dropped for garbage collection.
"MaxLargePoolFreeBytes": 10000000,
},public MyClass(IAzureBlobStorageService storageService)
{
}Here are some examples how to use the IAzureBlobStorageService in your project.
//Upload a file to the specified container or the default container if not specified and to root if no folder name is specified.
Task UploadBlobAsync(string blobName, string contentType, Stream content, string containerName = null, string folderName = null);Example:
await _service.CreateContainerIfNotExistsAsync(containerName);
await using var inputStream = new FileStream("file.txt", FileMode.Open, FileAccess.Read);
await _service.UploadBlobAsync("file.txt", "text/plain", inputStream, containerName);//Lists all files inside the specified container or if not specified the default container. Lists files inside of a folder if folder name is specified.
IAsyncEnumerable<BlobInfoContract> ListBlobsInContainerAsync(string containerName = null, string folderName = null);
public class BlobInfoContract
{
public string Name { get; set; }
public string BlobType { get; set; }
public string ContentEncoding { get; set; }
public string ContentType { get; set; }
public long? Size { get; set; }
public DateTimeOffset? DateCreated { get; set; }
public string AccessTier { get; set; }
}Example:
var files = await _service.ListBlobsInContainerAsync(containerName).ToListAsync(); // with nuget package System.Linq.Async
foreach (var f in files)
{
Console.WriteLine(f.Name);
}//Get the blob content. If no container name is specifeid the default container is used.
Task<BlobContract> GetBlobContentsAsync(string blobName, string containerName = null);
public class BlobContract : BlobInfoContract
{
public Stream Content { get; set; }
}Example:
var file = await _service.GetBlobContentsAsync("file.txt", containerName);
var azureStream = file.Content;
await using var outputStream = File.Create("file.txt");
CopyStream(azureStream, outputStream);//Deletes a file from the specified container or the default container if not specified.
Task DeleteBlobAsync(string blobName, string containerName = null);//Deletes all files from the specified container or the default container if not specified inside of a specific folder.
Task DeleteFolderAsync(string folderName, string containerName = null);//Creates a container with the specified name or if no container name is specified the default container.
Task CreateContainerIfNotExistsAsync(string containerName = null);//Returns a list of containers that are currently configured in the storage account
IAsyncEnumerable<StorageContainerContract> GetContainersAsync();//Deletes a specified container or if no container name is specified the default container.
Task DeleteContainerAsync(string containerName = null);//Returns the name of the storage account. This is the name configured in the connection string.
string GetStorageAccountName()Having multiple storages is also supported by implementing multiple client factories.
public class MyClientFactory : IMyClientFactory
{
private IOptions<MyStorageOptions> Options { get; }
public MyClientFactory(IOptions<MyStorageOptions> options)
{
Options = options;
}
public BlobServiceClient GetClient(BlobClientOptions options = null)
{
return new BlobServiceClient(Options.Value.ConnectionString, options);
}
public string GetDefaultContainerName()
{
return Options.Value.ContainerName;
}
}
public interface IMyClientFactory : IAzureBlobStorageClientFactory
{
}The client and a matching service is then registered like that:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOptions<MyStorageOptions>();
builder.Services.AddAzureBlobStorage<IMyClientFactory, MyClientFactory>(builder.Configuration);
builder.Services.AddHealthChecks().AddAzureBlobStorage<IMyClientFactory>()To use it just inject it like that:
public MyClass(IAzureBlobStorageService<IMyClientFactory> storageService)
{
}- create git tag
- The nuget package will be published automatically by a github action