Connect to Azure Blob Storage
此内容尚不支持你的语言。
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.
Connection properties
Section titled “Connection properties”Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called blobs becomes BLOBS_URI.
Blob storage resource
Section titled “Blob storage resource”The blob storage resource (AddBlobs / addBlobs) exposes the following connection properties:
| Property Name | Description |
|---|---|
Uri | The blob service endpoint, with the format https://{account}.blob.core.windows.net/ |
ConnectionString | Emulator only. The full connection string for the Azurite emulator |
Example:
Uri: https://mystorageaccount.blob.core.windows.net/Blob container resource
Section titled “Blob container resource”The blob container resource (AddBlobContainer / addBlobContainer) inherits all properties from its parent blob storage resource and adds:
| Property Name | Description |
|---|---|
BlobContainerName | The name of the blob container in Azure Storage |
Example:
Uri: https://mystorageaccount.blob.core.windows.net/BlobContainerName: upload-dataConnect from your app
Section titled “Connect from your app”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 client integration
Section titled “Install the client integration”Install the 📦 Aspire.Azure.Storage.Blobs NuGet package in the client-consuming project:
dotnet add package Aspire.Azure.Storage.Blobs#:package Aspire.Azure.Storage.Blobs@*<PackageReference Include="Aspire.Azure.Storage.Blobs" Version="*" />Add the Blob Storage client
Section titled “Add the Blob Storage client”In Program.cs, call AddAzureBlobServiceClient on your IHostApplicationBuilder to register a BlobServiceClient:
builder.AddAzureBlobServiceClient(connectionName: "blobs");Resolve the client through dependency injection:
public class ExampleService(BlobServiceClient client){ // Use client...}Add keyed Blob Storage clients
Section titled “Add keyed Blob Storage clients”To register multiple BlobServiceClient instances with different connection names, use AddKeyedAzureBlobServiceClient:
builder.AddKeyedAzureBlobServiceClient(name: "images");builder.AddKeyedAzureBlobServiceClient(name: "documents");Then resolve each instance by key:
public class ExampleService( [FromKeyedServices("images")] BlobServiceClient imagesClient, [FromKeyedServices("documents")] BlobServiceClient documentsClient){ // Use clients...}For more information, see .NET dependency injection: Keyed services.
Configuration
Section titled “Configuration”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:
builder.AddAzureBlobServiceClient("blobs");Two connection formats are supported:
Service URI (recommended for production with managed identity or default credentials):
{ "ConnectionStrings": { "blobs": "https://{account_name}.blob.core.windows.net/" }}Connection string (useful for local development with Azurite):
{ "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:
{ "Aspire": { "Azure": { "Storage": { "Blobs": { "DisableHealthChecks": false, "DisableTracing": false, "ClientOptions": { "Diagnostics": { "ApplicationId": "myapp" } } } } } }}Inline delegates. Pass an Action<AzureStorageBlobsSettings> to configure settings inline:
builder.AddAzureBlobServiceClient( "blobs", settings => settings.DisableHealthChecks = true);You can also configure BlobClientOptions:
builder.AddAzureBlobServiceClient( "blobs", configureClientBuilder: clientBuilder => clientBuilder.ConfigureOptions( options => options.Diagnostics.ApplicationId = "myapp"));Client integration health checks
Section titled “Client integration health checks”Aspire client integrations enable health checks by default. The Azure Blob Storage client integration:
- Adds the health check when
DisableHealthChecksisfalse, which attempts to connect to the Azure Blob Storage service. - Integrates with the
/healthHTTP endpoint, where all registered health checks must pass before the app is considered ready to accept traffic.
Observability and telemetry
Section titled “Observability and telemetry”The Aspire Azure Blob Storage client integration automatically configures logging, tracing, and metrics through OpenTelemetry.
Logging categories:
Azure.CoreAzure.Identity
Tracing activities:
Azure.Storage.Blobs.BlobContainerClient
Metrics are not emitted by default due to limitations with the Azure SDK.
Read environment variables in C#
Section titled “Read environment variables in C#”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:
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...Install the Azure Storage Blobs SDK for Go:
go get github.com/Azure/azure-sdk-for-go/sdk/storage/azblobgo get github.com/Azure/azure-sdk-for-go/sdk/azidentityRead the injected environment variable and connect:
package main
import ( "os" "github.com/Azure/azure-sdk-for-go/sdk/azidentity" "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob")
func main() { // Read the Aspire-injected blob service endpoint blobEndpoint := os.Getenv("BLOBS_URI")
cred, err := azidentity.NewDefaultAzureCredential(nil) if err != nil { panic(err) }
client, err := azblob.NewClient(blobEndpoint, cred, nil) if err != nil { panic(err) }
_ = client // Use client to interact with Azure Blob Storage...}When running against the Azurite emulator, use the ConnectionString environment variable instead:
package main
import ( "os" "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob")
func main() { connStr := os.Getenv("BLOBS_CONNECTIONSTRING")
client, err := azblob.NewClientFromConnectionString(connStr, nil) if err != nil { panic(err) }
_ = client}Install the Azure Storage Blobs SDK for Python:
pip install azure-storage-blob azure-identityRead the injected environment variable and connect:
import osfrom azure.identity import DefaultAzureCredentialfrom azure.storage.blob import BlobServiceClient
# Read the Aspire-injected blob service endpointblob_endpoint = os.getenv("BLOBS_URI")
credential = DefaultAzureCredential()client = BlobServiceClient(account_url=blob_endpoint, credential=credential)
# Use client to interact with Azure Blob Storage...When running against the Azurite emulator, use the ConnectionString environment variable instead:
import osfrom azure.storage.blob import BlobServiceClient
conn_str = os.getenv("BLOBS_CONNECTIONSTRING")client = BlobServiceClient.from_connection_string(conn_str)
# Use client to interact with Azure Blob Storage...Install the Azure Storage Blobs SDK for JavaScript:
npm install @azure/storage-blob @azure/identityRead the injected environment variables and connect:
import { BlobServiceClient } from '@azure/storage-blob';import { DefaultAzureCredential } from '@azure/identity';
// Read the Aspire-injected blob service endpointconst blobEndpoint = process.env.BLOBS_URI!;
const credential = new DefaultAzureCredential();const client = new BlobServiceClient(blobEndpoint, credential);
// Use client to interact with Azure Blob Storage...When running against the Azurite emulator, use the ConnectionString environment variable instead:
import { BlobServiceClient } from '@azure/storage-blob';
const connStr = process.env.BLOBS_CONNECTIONSTRING!;const client = BlobServiceClient.fromConnectionString(connStr);
// Use client to interact with Azure Blob Storage...