Add builder pattern API for multiple blob containers in Aspire.Azure.Storage.Blobs
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 201
Description
## Is there an existing issue for this?
- [x] I have searched the existing issues
## Is your feature request related to a problem? Please describe the problem.
Currently, if you want to work with multiple blob containers from the same Azure Storage account, you have two suboptimal approaches:
1. **Register each container separately** - Each creates its own `BlobServiceClient`, which is inefficient:
```csharp
builder.AddAzureBlobContainerClient("images");
builder.AddAzureBlobContainerClient("documents");
builder.AddAzureBlobContainerClient("videos");
// Creates 3 separate BlobServiceClient instances internally
```
2. **Register the service client and manually get containers** - Requires manual container management:
```csharp
builder.AddAzureBlobServiceClient("storage");
// Then in your service:
public MyService(BlobServiceClient serviceClient)
{
var imagesContainer = serviceClient.GetBlobContainerClient("images");
var docsContainer = serviceClient.GetBlobContainerClient("documents");
// Manual container management, not using DI for containers
}
```
The Cosmos DB integration already solves this problem with `AddAzureCosmosDatabase()` and `CosmosDatabaseBuilder`, but Blob Storage lacks an equivalent pattern.
## Describe the solution you'd like
Add a **builder pattern API** similar to `CosmosDatabaseBuilder` that allows registering multiple blob containers against a single `BlobServiceClient`:
```csharp
// Proposed API
builder.AddAzureBlobService("storage")
.AddKeyedContainer("images")
.AddKeyedContainer("documents")
.AddKeyedContainer("videos");
// Usage in services
public class MyService(
[FromKeyedServices("images")] BlobContainerClient imagesContainer,
[FromKeyedServices("documents")] BlobContainerClient docsContainer,
[FromKeyedServices("videos")] BlobContainerClient videosContainer)
{
// All containers share the same underlying BlobServiceClient
}
```
### Benefits
1. **Resource Efficiency**: Single `BlobServiceClient` instance shared across all containers
2. **Better DI Integration**: Containers registered as keyed services, easily injectable
3. **Cleaner Code**: Fluent builder API for declaring multiple containers
4. **Consistency**: Matches the pattern already established by Cosmos DB integration
5. **Performance**: Reduces connection overhead and resource usage
### Implementation Requirements
1. **New `BlobServiceBuilder` class** (similar to `CosmosDatabaseBuilder`):
- Constructor accepting `IHostApplicationBuilder`, connection name, settings, client options
- `AddService()` / `AddKeyedService()` internal methods to register the `BlobServiceClient`
- `AddKeyedContainer(string name)` public method to register `BlobContainerClient` instances
2. **New extension methods** in `AspireBlobStorageExtensions`:
- `AddAzureBlobService()` - Returns `BlobServiceBuilder`
- `AddKeyedAzureBlobService()` - Returns `BlobServiceBuilder` with keyed service
3. **Playground sample** (like `CosmosEndToEnd`):
- Example showing multiple containers registered via builder
- Demonstrates blob upload/download operations across different containers
- Shows keyed service injection pattern
### API Consistency
**Cosmos DB (existing):**
```csharp
builder.AddAzureCosmosDatabase("db")
.AddKeyedContainer("entries")
.AddKeyedContainer("users")
.AddKeyedContainer("user-todo");
```
**Blob Storage (proposed):**
```csharp
builder.AddAzureBlobService("storage")
.AddKeyedContainer("images")
.AddKeyedContainer("documents")
.AddKeyedContainer("videos");
```
## Additional context
This feature would bring the Blob Storage integration to feature parity with the Cosmos DB integration and provide a more ergonomic API for the common scenario of working with multiple blob containers within the same storage account.
Reference implementation:
- `src/Components/Aspire.Microsoft.Azure.Cosmos/CosmosDatabaseBuilder.cs`
- `src/Components/Aspire.Microsoft.Azure.Cosmos/AspireMicrosoftAzureCosmosExtensions.cs`
- `playground/CosmosEndToEnd/CosmosEndToEnd.ApiService/Program.cs`
Contributor guide
Research direction
Start by comparing src/Components/Aspire.Microsoft.Azure.Cosmos/CosmosDatabaseBuilder.cs and AspireMicrosoftAzureCosmosExtensions.cs with the existing Blob Storage integration. Then inspect playground/CosmosEndToEnd/CosmosEndToEnd.ApiService/Program.cs for the sample pattern. Done means the Blob Storage builder API supports multiple keyed containers sharing one service client and includes a corresponding playground example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, csharp
- Domain
- cloud
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100