콘텐츠로 이동

Getting started with the Azure Blob Storage integration

이 콘텐츠는 아직 번역되지 않았습니다.

Azure Blob Storage logo

Azure Blob Storage is a service for storing large amounts of unstructured data. The Aspire Azure Blob Storage Hosting integration provides methods to create Azure Blob Storage resources from code in your Aspire AppHost project.

In this introduction, you’ll see how to install and use the Aspire Azure Blob Storage integrations in a simple configuration. If you already have this knowledge, see Azure Blob Storage Hosting integration for full reference details.

To begin, install the Aspire Azure Storage Hosting integration in your Aspire AppHost project. This integration allows you to create and manage Azure Blob Storage resources from your Aspire hosting projects:

Aspire CLI — Aspire.Hosting.Azure.Storage 패키지 추가
aspire add azure-storage

Aspire CLI는 대화형입니다. 프롬프트 시 알맞은 검색 결과 선택:

Aspire CLI — 출력 예시
Select an integration to add:
> azure-storage (Aspire.Hosting.Azure.Storage)
> Other results listed as selectable options...

Next, in the AppHost project, create an Azure Blob Storage resource and pass it to the consuming client projects:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var blobs = builder.AddAzureStorage("storage")
.AddBlobs("blobs");
var myService = builder.AddProject<Projects.MyService>()
.WithReference(blobs);
// After adding all resources, run the app...
builder.Build().Run();

The preceding code adds an Azure Storage resource named storage to the AppHost project, adds a blob container to it named blobs, and passes the blob storage connection information to the consuming project.

To use Azure Blob Storage from your client applications, install the Aspire Azure Blob Storage client integration in your client project:

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

In the Program.cs file of your client-consuming project, call the AddAzureBlobServiceClient extension method to register a BlobServiceClient for use via the dependency injection container:

builder.AddAzureBlobServiceClient(connectionName: "blobs");

Use injected Azure Blob Storage properties

Section titled “Use injected Azure Blob Storage properties”

In the AppHost, when you used the WithReference method to pass an Azure Blob Storage resource to a consuming client project, Aspire injects several configuration properties that you can use in the consuming project.

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

Use the GetValue() method to obtain these environment variables in consuming projects:

C# — Obtain configuration properties
string blobUri = builder.Configuration.GetValue<string>("BLOBS_URI");

Add Azure Blob Storage resources in client code

Section titled “Add Azure Blob Storage resources in client code”

After adding the BlobServiceClient, you can retrieve the connection instance using dependency injection:

public class ExampleService(BlobServiceClient client)
{
// Use client...
}

For full details on using the client integration, see Azure Blob Storage Client integration.