Azure Blob Storage を Service Connector と統合する

このページでは、Service Connector を使用した Azure Blob Storage のサポートされている認証の種類、クライアントの種類、サンプル コードを紹介します。 このページには、サービス接続を作成するときに取得する既定の環境変数の名前と値 (つまり、Spring Boot 構成) も示されています。

サポートされているコンピューティング サービス

Service Connector を使用して、次のコンピューティング サービスを Azure Blob Storage に接続できます。

  • Azure App Service
  • Azure Functions
  • Azure Container Apps
  • Azure Spring Apps

サポートされている認証の種類とクライアントの種類

次の表は、Service Connector を使用してコンピューティング サービスを Azure Blob Storage に接続するためにサポートされている認証方法とクライアントの組み合わせを示しています。 "はい" は組み合わせがサポートされていることを示し、"いいえ" はサポートされていないことを示します。

クライアント タイプ システム割り当てマネージド ID ユーザー割り当てマネージド ID シークレット/接続文字列 サービス プリンシパル
.NET はい イエス イエス はい
Java はい イエス イエス はい
Java - Spring Boot いいえ 番号 イエス いいえ
Node.js はい イエス イエス はい
Python はい イエス イエス はい
Go はい イエス イエス はい
なし 有効 イエス イエス はい

この表は、シークレット/接続文字列 メソッドのみをサポートする Java - Spring Boot クライアントの種類を除き、クライアントの種類と認証方法のすべての組み合わせがサポートされていることを明確に示しています。 他のすべてのクライアントの種類では、任意の認証方法を使用して、Service Connector を使用して Azure Blob Storage に接続できます。

既定の環境変数名またはアプリケーション プロパティとサンプル コード

接続の認証の種類とクライアントの種類に基づき、次の表の接続詳細とサンプル コードを参照して、Azure Blob Storage にコンピューティング サービスを接続します。 Service Connector 環境変数の名前付け規則の詳細を参照してください。

システム割り当てマネージド ID

他の認証の種類の既定の環境変数とサンプル コードについては、ドキュメントの最初から選択してください。

既定の環境変数名 説明 例値
AZURE_STORAGEBLOB_RESOURCEENDPOINT Blob Storage エンドポイント https://<storage-account-name>.blob.core.windows.net/

サンプル コード

システム割り当てマネージド ID を使用して Azure Blob Storage に接続するには、次の手順とコードを参照してください。

azure-identity を使用し、マネージド ID またはサービス プリンシパル経由で認証できます。 Service Connector によって追加された環境変数から Azure Blob Storage エンドポイントの URL を取得します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。

依存関係のインストール

dotnet add package Azure.Identity

マネージド ID またはサービス プリンシパルを使用して Blob Storage に接続するサンプル コードを次に示します。

using Azure.Identity;
using Azure.Storage.Blobs;

// get Blob endpoint
var blobEndpoint = Environment.GetEnvironmentVariable("AZURE_STORAGEBLOB_RESOURCEENDPOINT");

// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();

// user-assigned managed identity
// var credential = new DefaultAzureCredential(
//     new DefaultAzureCredentialOptions
//     {
//         ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_STORAGEBLOB_CLIENTID");
//     });

// service principal 
// var tenantId = Environment.GetEnvironmentVariable("AZURE_STORAGEBLOB_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_STORAGEBLOB_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_STORAGEBLOB_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);

var blobServiceClient = new BlobServiceClient(
        new Uri(blobEndpoint),
        credential);

ユーザー割り当てマネージド ID

他の認証の種類の既定の環境変数とサンプル コードについては、ドキュメントの最初から選択してください。

既定の環境変数名 説明 例値
AZURE_STORAGEBLOB_RESOURCEENDPOINT Blob Storage エンドポイント https://<storage-account-name>.blob.core.windows.net/
AZURE_STORAGEBLOB_CLIENTID クライアント ID <client-ID>

サンプル コード

ユーザー割り当てマネージド ID を使用して Azure Blob Storage に接続するには、次の手順とコードを参照してください。

azure-identity を使用し、マネージド ID またはサービス プリンシパル経由で認証できます。 Service Connector によって追加された環境変数から Azure Blob Storage エンドポイントの URL を取得します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。

依存関係のインストール

dotnet add package Azure.Identity

マネージド ID またはサービス プリンシパルを使用して Blob Storage に接続するサンプル コードを次に示します。

using Azure.Identity;
using Azure.Storage.Blobs;

// get Blob endpoint
var blobEndpoint = Environment.GetEnvironmentVariable("AZURE_STORAGEBLOB_RESOURCEENDPOINT");

// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();

// user-assigned managed identity
// var credential = new DefaultAzureCredential(
//     new DefaultAzureCredentialOptions
//     {
//         ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_STORAGEBLOB_CLIENTID");
//     });

// service principal 
// var tenantId = Environment.GetEnvironmentVariable("AZURE_STORAGEBLOB_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_STORAGEBLOB_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_STORAGEBLOB_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);

var blobServiceClient = new BlobServiceClient(
        new Uri(blobEndpoint),
        credential);

接続文字列

他の認証の種類の既定の環境変数とサンプル コードについては、ドキュメントの最初から選択してください。

SpringBoot クライアントの種類

Application properties 説明 例値
azure.storage.account-name Blob Storage アカウント名 <storage-account-name>
azure.storage.account-key Blob Storage アカウント キー <account-key>
azure.storage.blob-endpoint Blob Storage エンドポイント https://<storage-account-name>.blob.core.windows.net/
spring.cloud.azure.storage.blob.account-name Spring Cloud Azure バージョン 4.0 以降の BLOB ストレージ アカウント名 <storage-account-name>
spring.cloud.azure.storage.blob.account-key Spring Cloud Azure バージョン 4.0 以降の Blob Storage アカウント キー <account-key>
spring.cloud.azure.storage.blob.endpoint Spring Cloud Azure バージョン 4.0 以降の Blob Storage エンドポイント https://<storage-account-name>.blob.core.windows.net/

その他のクライアントの種類

既定の環境変数名 説明 例値
AZURE_STORAGEBLOB_CONNECTIONSTRING Blob Storage の接続文字列 DefaultEndpointsProtocol=https;AccountName=<account name>;AccountKey=<account-key>;EndpointSuffix=core.windows.net

サンプル コード

接続文字列を使用して Azure Blob Storage に接続するには、次の手順とコードを参照してください。

Service Connector によって追加された環境変数から Azure Blob Storage 接続文字列を取得します。

依存関係のインストール

dotnet add package Azure.Storage.Blob
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System; 

// get Blob connection string
var connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGEBLOB_CONNECTIONSTRING");

// Create a BlobServiceClient object 
var blobServiceClient = new BlobServiceClient(connectionString);

サービス プリンシパル

他の認証の種類の既定の環境変数とサンプル コードについては、ドキュメントの最初から選択してください。

既定の環境変数名 説明 例値
AZURE_STORAGEBLOB_RESOURCEENDPOINT Blob Storage エンドポイント https://<storage-account-name>.blob.core.windows.net/
AZURE_STORAGEBLOB_CLIENTID クライアント ID <client-ID>
AZURE_STORAGEBLOB_CLIENTSECRET クライアント シークレット <client-secret>
AZURE_STORAGEBLOB_TENANTID テナント ID <tenant-ID>

サンプル コード

サービス プリンシパルを使用して Azure Blob Storage に接続するには、次の手順とコードを参照してください。

azure-identity を使用し、マネージド ID またはサービス プリンシパル経由で認証できます。 Service Connector によって追加された環境変数から Azure Blob Storage エンドポイントの URL を取得します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。

依存関係のインストール

dotnet add package Azure.Identity

マネージド ID またはサービス プリンシパルを使用して Blob Storage に接続するサンプル コードを次に示します。

using Azure.Identity;
using Azure.Storage.Blobs;

// get Blob endpoint
var blobEndpoint = Environment.GetEnvironmentVariable("AZURE_STORAGEBLOB_RESOURCEENDPOINT");

// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();

// user-assigned managed identity
// var credential = new DefaultAzureCredential(
//     new DefaultAzureCredentialOptions
//     {
//         ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_STORAGEBLOB_CLIENTID");
//     });

// service principal 
// var tenantId = Environment.GetEnvironmentVariable("AZURE_STORAGEBLOB_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_STORAGEBLOB_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_STORAGEBLOB_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);

var blobServiceClient = new BlobServiceClient(
        new Uri(blobEndpoint),
        credential);

次のステップ

Service Connector の詳細については、チュートリアルに従ってください。