Samhammer.AzureBlobStorage 8.0.1

dotnet add package Samhammer.AzureBlobStorage --version 8.0.1
NuGet\Install-Package Samhammer.AzureBlobStorage -Version 8.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Samhammer.AzureBlobStorage" Version="8.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Samhammer.AzureBlobStorage --version 8.0.1
#r "nuget: Samhammer.AzureBlobStorage, 8.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Samhammer.AzureBlobStorage as a Cake Addin
#addin nuget:?package=Samhammer.AzureBlobStorage&version=8.0.1

// Install Samhammer.AzureBlobStorage as a Cake Tool
#tool nuget:?package=Samhammer.AzureBlobStorage&version=8.0.1

Samhammer.AzureBlobStorage

This package provides access to the azure blob storage over the azure sdk. It includes basic access functionality to upload and download files.

How to add this to your project:

  • 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
Example Program.cs:
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDefaultAzureBlobStorage(builder.Configuration);
builder.Services.AddHealthChecks().AddDefaultAzureBlobStorage()
Example appsettings configuration:
"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,
},
Example to inject the service
public MyClass(IAzureBlobStorageService storageService)
{
}

How to use this in your project:

Here are some examples how to use the IAzureBlobStorageService in your project.

Upload blob

//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);

List blobs

//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 blob

//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);

Delete blob

//Deletes a file from the specified container or the default container if not specified.
Task DeleteBlobAsync(string blobName, string containerName = null);

Delete folder

//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);

Create container

//Creates a container with the specified name or if no container name is specified the default container.
Task CreateContainerIfNotExistsAsync(string containerName = null);

Get container list

//Returns a list of containers that are currently configured in the storage account
IAsyncEnumerable<StorageContainerContract> GetContainersAsync();

Delete container

//Deletes a specified container or if no container name is specified the default container.
Task DeleteContainerAsync(string containerName = null);

Get StorageAccount

//Returns the name of the storage account. This is the name configured in the connection string.
string GetStorageAccountName()

How to use multiple storages this in your project:

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)
{
}

Contribute

How to publish package
  • create git tag
  • The nuget package will be published automatically by a github action
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
8.0.1 93 4/11/2024
8.0.0 114 3/7/2024
6.1.7 412 11/27/2023
6.1.6 130 11/27/2023
6.1.4 1,485 7/28/2023
6.1.3 140 7/27/2023
6.1.2 2,286 6/28/2023
6.1.1 119 6/26/2023
6.1.0 121 6/26/2023
6.0.3.4 128 6/23/2023
6.0.3.3 136 6/20/2023
6.0.3.2 145 6/19/2023
6.0.3.1 110 6/16/2023
6.0.3 633 3/14/2023
6.0.2 213 3/13/2023
6.0.1.1 137 6/16/2023
6.0.1 479 2/14/2023
6.0.0 213 2/9/2023