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
<PackageReference Include="DuckTyping.Azure.BlobService" Version="1.1.0" />
<PackageVersion Include="DuckTyping.Azure.BlobService" Version="1.1.0" />
<PackageReference Include="DuckTyping.Azure.BlobService" />
paket add DuckTyping.Azure.BlobService --version 1.1.0
#r "nuget: DuckTyping.Azure.BlobService, 1.1.0"
#:package DuckTyping.Azure.BlobService@1.1.0
#addin nuget:?package=DuckTyping.Azure.BlobService&version=1.1.0
#tool nuget:?package=DuckTyping.Azure.BlobService&version=1.1.0
DuckTyping.Azure.BlobService
A service that lets you easily connect to the azure blob service.
Configuration
Add the Blob storage:
- AccountName
- URL (e.g. https://your-account-name.blob.core.windows.net)
- Access key
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"
The access key is available in the "Security + networking" - "Access keys" section.
Create the Container
The Blob storage container should have the access level set to Private.
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 | Versions 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. |
-
net8.0
- Azure.Storage.Blobs (>= 12.24.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
-
net9.0
- Azure.Storage.Blobs (>= 12.24.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
- 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.