DuckTyping.Azure.BlobService 1.1.0

dotnet add package DuckTyping.Azure.BlobService --version 1.1.0
                    
NuGet\Install-Package DuckTyping.Azure.BlobService -Version 1.1.0
                    
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="DuckTyping.Azure.BlobService" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DuckTyping.Azure.BlobService" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="DuckTyping.Azure.BlobService" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DuckTyping.Azure.BlobService --version 1.1.0
                    
#r "nuget: DuckTyping.Azure.BlobService, 1.1.0"
                    
#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.
#:package DuckTyping.Azure.BlobService@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DuckTyping.Azure.BlobService&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=DuckTyping.Azure.BlobService&version=1.1.0
                    
Install as a Cake Tool

DuckTyping.Azure.BlobService

A service that lets you easily connect to the azure blob service.

Configuration

Add the Blob storage:

Find configuration values in Azure

In Azure, you can find the account name in the "Overview" section. Replace "your-account-name" in the URL. In this example the URL should be "https://ducktypingnameexample.blob.core.windows.net"

Account name

The access key is available in the "Security + networking" - "Access keys" section.

Account name

Create the Container

The Blob storage container should have the access level set to Private.

Access level

Configure Program.cs

In the configuration section of Program.cs add the following lines:

// Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddBlobClient(options =>
{
    options.AccountName = Environment.GetEnvironmentVariable("BLOB_STORAGE_ACCOUNTNAME");
    options.Url = Environment.GetEnvironmentVariable("BLOB_STORAGE_URL");
    options.Key = Environment.GetEnvironmentVariable("BLOB_STORAGE_KEY");
});

var app = builder.Build();

Or if you prefer to use the appsettings.json Configurations

//appsettings.json

{
  "BlobStorage": {
    "AccountName": "your-account-name",
    "Url": "https://your-account-name.blob.core.windows.net",
    "Key": "your-access-key"
  }
}

Then add these lines in Program.cs

// Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddBlobClient(options =>
{
    options.AccountName = builder.Configuration["BlobStorage:AccountName"]!;
    options.Url = builder.Configuration["BlobStorage:Url"]!;
    options.Key = builder.Configuration["BlobStorage:Key"]!;
});

var app = builder.Build();

Use the client

Inject client

Import the namespace ducktyping.azure.blobservice and inject the service with dependency injection.

// SomeClass.cs

using DuckTyping.Azure.BlobService;

public class MyService(IBlobClient blobClient)
{
    private readonly IBlobClient _blobClient = blobClient;

    public async Task UploadFileAsync()
    {
        ...
    }
}

Upload file

You can upload a file to a specified blob container using the UploadAsync method. The file will be stored in Azure Blob Storage with the name specified by fileName.

If the container specified by containerName does not exist in your storage account, a BlobStorageException will be thrown.

// SomeClass.cs

using DuckTyping.Azure.BlobService;

public class MyService(IBlobClient blobClient)
{
    private readonly IBlobClient _blobClient = blobClient;

    public async Task UploadFileAsync()
    {
        using var stream = File.OpenRead("example.pdf");

        await _blobClient.UploadAsync(
            containerName: "my-container",
            fileName: "example.pdf",
            contentType: "application/pdf",
            stream: stream
        );
    }
}

Generate URI with SAS token

A Shared Access Signature (SAS) token is a secure, time-limited access token for a blob resource in Azure Blob Storage.

By default, the generated SAS URI:

  • Grants read-only access to the specified blob.
  • Is valid for 5 minutes from the time of generation.

To enable write access in addition to read, set writePermission = true.

// SomeClass.cs

using DuckTyping.Azure.BlobService;

public class MyService(IBlobClient blobClient)
{
    private readonly IBlobClient _blobClient = blobClient;

    public async Task<Uri> GenerateSasUriAsync()
    {
        var sasUri = await _blobClient.GenerateBlobSasUriAsync(
            containerName: containerName,
            blobName: blobName,
            writePermission: false // Set to true if you need write access
        );

        return sasUri;
    }
}

You can access the uploaded file with the returned URI

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  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. 
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
1.1.0 140 7/6/2025
1.0.3 72 7/4/2025
1.0.2 73 7/4/2025

- Added support for generating SAS URIs with read or read/write permissions.
- Improved exception handling for better diagnostics and error clarity.
- Enhanced XML documentation for public APIs.
- Multi-targeted the library to support both .NET 8.0 and .NET 9.0.