跳转到内容
Docs Try Aspire
Docs Try

Connect to Azure Blob Storage

此内容尚不支持你的语言。

Azure Blob Storage logo

This page describes how consuming apps connect to an Azure Blob Storage resource that’s already modeled in your AppHost. For the AppHost API surface — adding a storage account, blob resources, blob containers, Azurite emulator, role-based access, and more — see Azure Blob Storage Hosting integration.

When you reference an Azure Blob Storage resource from your AppHost, Aspire injects the connection information into the consuming app as environment variables. Your app can either read those environment variables directly — the pattern works the same from any language — or, in C#, use the Aspire Azure Blob Storage client integration for automatic dependency injection, health checks, and telemetry.

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called blobs becomes BLOBS_URI.

The blob storage resource (AddBlobs / addBlobs) exposes the following connection properties:

Property NameDescription
UriThe blob service endpoint, with the format https://{account}.blob.core.windows.net/
ConnectionStringEmulator only. The full connection string for the Azurite emulator

Example:

Uri: https://mystorageaccount.blob.core.windows.net/

The blob container resource (AddBlobContainer / addBlobContainer) inherits all properties from its parent blob storage resource and adds:

Property NameDescription
BlobContainerNameThe name of the blob container in Azure Storage

Example:

Uri: https://mystorageaccount.blob.core.windows.net/
BlobContainerName: upload-data

Pick the language your consuming app is written in. Each example assumes your AppHost adds an Azure Storage resource, adds a blob service resource named blobs, and references it from the consuming app.

For C# apps, the recommended approach is the Aspire Azure Blob Storage client integration. It registers a BlobServiceClient through dependency injection and adds health checks and telemetry automatically. If you’d rather read environment variables directly, see the Read environment variables section at the end of this tab.

Install the 📦 Aspire.Azure.Storage.Blobs NuGet package in the client-consuming project:

.NET CLI — Add Aspire.Azure.Storage.Blobs package
dotnet add package Aspire.Azure.Storage.Blobs

In Program.cs, call AddAzureBlobServiceClient on your IHostApplicationBuilder to register a BlobServiceClient:

C# — Program.cs
builder.AddAzureBlobServiceClient(connectionName: "blobs");

Resolve the client through dependency injection:

C# — ExampleService.cs
public class ExampleService(BlobServiceClient client)
{
// Use client...
}

To register multiple BlobServiceClient instances with different connection names, use AddKeyedAzureBlobServiceClient:

C# — Program.cs
builder.AddKeyedAzureBlobServiceClient(name: "images");
builder.AddKeyedAzureBlobServiceClient(name: "documents");

Then resolve each instance by key:

C# — ExampleService.cs
public class ExampleService(
[FromKeyedServices("images")] BlobServiceClient imagesClient,
[FromKeyedServices("documents")] BlobServiceClient documentsClient)
{
// Use clients...
}

For more information, see .NET dependency injection: Keyed services.

The Aspire Azure Blob Storage client integration offers multiple ways to provide configuration.

Connection strings. When using a connection string from the ConnectionStrings configuration section, pass the connection name to AddAzureBlobServiceClient:

C# — Program.cs
builder.AddAzureBlobServiceClient("blobs");

Two connection formats are supported:

Service URI (recommended for production with managed identity or default credentials):

JSON — appsettings.json
{
"ConnectionStrings": {
"blobs": "https://{account_name}.blob.core.windows.net/"
}
}

Connection string (useful for local development with Azurite):

JSON — appsettings.json
{
"ConnectionStrings": {
"blobs": "AccountName=devstoreaccount1;AccountKey=...;BlobEndpoint=http://localhost:10000/devstoreaccount1"
}
}

Configuration providers. The client integration supports Microsoft.Extensions.Configuration. It loads AzureStorageBlobsSettings from appsettings.json using the Aspire:Azure:Storage:Blobs key:

JSON — appsettings.json
{
"Aspire": {
"Azure": {
"Storage": {
"Blobs": {
"DisableHealthChecks": false,
"DisableTracing": false,
"ClientOptions": {
"Diagnostics": {
"ApplicationId": "myapp"
}
}
}
}
}
}
}

Inline delegates. Pass an Action<AzureStorageBlobsSettings> to configure settings inline:

C# — Program.cs
builder.AddAzureBlobServiceClient(
"blobs",
settings => settings.DisableHealthChecks = true);

You can also configure BlobClientOptions:

C# — Program.cs
builder.AddAzureBlobServiceClient(
"blobs",
configureClientBuilder: clientBuilder =>
clientBuilder.ConfigureOptions(
options => options.Diagnostics.ApplicationId = "myapp"));

Aspire client integrations enable health checks by default. The Azure Blob Storage client integration:

  • Adds the health check when DisableHealthChecks is false, which attempts to connect to the Azure Blob Storage service.
  • Integrates with the /health HTTP endpoint, where all registered health checks must pass before the app is considered ready to accept traffic.

The Aspire Azure Blob Storage client integration automatically configures logging, tracing, and metrics through OpenTelemetry.

Logging categories:

  • Azure.Core
  • Azure.Identity

Tracing activities:

  • Azure.Storage.Blobs.BlobContainerClient

Metrics are not emitted by default due to limitations with the Azure SDK.

If you prefer not to use the Aspire client integration, you can read the Aspire-injected blob endpoint from the environment and create a BlobServiceClient directly using the 📦 Azure.Storage.Blobs NuGet package:

C# — Program.cs
using Azure.Identity;
using Azure.Storage.Blobs;
var blobEndpoint = Environment.GetEnvironmentVariable("BLOBS_URI");
var client = new BlobServiceClient(
new Uri(blobEndpoint!),
new DefaultAzureCredential());
// Use client to interact with Azure Blob Storage...