このページでは、Service Connector を使用した Azure Blob Storage のサポートされている認証の種類、クライアントの種類、サンプル コードを紹介します。 このページには、サービス接続を作成するときに取得する既定の環境変数の名前と値 (つまり、Spring Boot 構成) も示されています。
サポートされているコンピューティング サービス
Service Connector を使うと、次のコンピューティング サービスを Azure Blob Storage に接続できます。
- Azure App Service
- Azure Container Apps
- Azure Functions
- Azure Kubernetes Service (AKS)
- Azure Spring Apps
サポートされている認証の種類とクライアントの種類
次の表は、サービス コネクタを使った 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
SpringBoot クライアント
システム割り当てマネージド ID を使う認証は、Spring Cloud Azure バージョン 4.0 以降でのみ使用できます。
既定の環境変数名 |
説明 |
値の例 |
spring.cloud.azure.storage.blob.credential.managed-identity-enabled |
マネージド ID を有効にするかどうか |
True |
spring.cloud.azure.storage.blob.account-name |
ストレージ アカウントの名前 |
storage-account-name |
spring.cloud.azure.storage.blob.endpoint |
Blob Storage エンドポイント |
https://<storage-account-name>.blob.core.windows.net/ |
その他のクライアント
既定の環境変数名 |
説明 |
例値 |
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 エンドポイントを取得します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
依存関係のインストール
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);
pom.xml ファイルに次の依存関係を追加します。
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.1.5</version>
</dependency>
azure-identity
を使用して認証し、Service Connector によって追加された環境変数からエンドポイント URL を取得します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
String url = System.getenv("AZURE_STORAGEBLOB_RESOURCEENDPOINT");
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system managed identity
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder().build();
// for user assigned managed identity
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_STORAGEBLOB_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential defaultCredential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("<AZURE_STORAGEBLOB_CLIENTID>"))
// .clientSecret(System.getenv("<AZURE_STORAGEBLOB_CLIENTSECRET>"))
// .tenantId(System.getenv("<AZURE_STORAGEBLOB_TENANTID>"))
// .build();
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint(url)
.credential(defaultCredential)
.buildClient();
依存関係のインストール
pip install azure-identity
pip install azure-storage-blob
azure-identity
ライブラリを使用して認証し、Service Connector によって追加された環境変数からエンドポイント URL を取得します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
from azure.storage.blob import BlobServiceClient
import os
account_url = os.getenv('AZURE_STORAGEBLOB_RESOURCEENDPOINT')
# Uncomment the following lines corresponding to the authentication type you want to use.
# system assigned managed identity
# cred = ManagedIdentityCredential()
# user assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_STORAGEBLOB_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_STORAGEBLOB_TENANTID')
# client_id = os.getenv('AZURE_STORAGEBLOB_CLIENTID')
# client_secret = os.getenv('AZURE_STORAGEBLOB_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
blob_service_client = BlobServiceClient(account_url, credential=cred)
依存関係をインストールします。
pip install azure-identity
pip install django-storages[azure]
azure-identity
ライブラリ経由で認証します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
import os
# Uncomment the following lines corresponding to the authentication type you want to use.
# system assigned managed identity
# cred = ManagedIdentityCredential()
# user assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_STORAGEBLOB_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_STORAGEBLOB_TENANTID')
# client_id = os.getenv('AZURE_STORAGEBLOB_CLIENTID')
# client_secret = os.getenv('AZURE_STORAGEBLOB_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
設定ファイルで、次の行を追加します。 詳しくは、「django-storages[azure]」をご覧ください。
# in your setting file, eg. settings.py
AZURE_CUSTOM_DOMAIN = os.getenv('AZURE_STORAGEBLOB_RESOURCEENDPOINT')
AZURE_ACCOUNT_NAME = AZURE_CUSTOM_DOMAIN.split('.')[0].removeprefix('https://')
AZURE_TOKEN_CREDENTIAL = cred # this is the cred acquired from above step.
依存関係をインストールします。
go get "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
go get "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
コードで、azidentity
ライブラリ経由で認証します。 Service Connector によって追加された環境変数から Azure Blob Storage エンドポイントを取得します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
import (
"context"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
)
func main() {
account_endpoint = os.Getenv("AZURE_STORAGEBLOB_RESOURCEENDPOINT")
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// for user-assigned managed identity
// clientid := os.Getenv("AZURE_STORAGEBLOB_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// for service principal
// clientid := os.Getenv("AZURE_STORAGEBLOB_CLIENTID")
// tenantid := os.Getenv("AZURE_STORAGEBLOB_TENANTID")
// clientsecret := os.Getenv("AZURE_STORAGEBLOB_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
if err != nil {
// error handling
}
client, err := azblob.NewBlobServiceClient(account_endpoint, cred, nil)
}
依存関係のインストール
npm install --save @azure/identity
npm install @azure/storage-blob
Service Connector によって追加された環境変数から Azure Blob Storage エンドポイントを取得します。 @azure/identity
ライブラリ経由で認証します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
const { BlobServiceClient } = require("@azure/storage-blob");
const account_url = process.env.AZURE_STORAGEBLOB_RESOURCEENDPOINT;
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system assigned managed identity
// const credential = new DefaultAzureCredential();
// for user assigned managed identity
// const clientId = process.env.AZURE_STORAGEBLOB_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_STORAGEBLOB_TENANTID;
// const clientId = process.env.AZURE_STORAGEBLOB_CLIENTID;
// const clientSecret = process.env.AZURE_STORAGEBLOB_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const blobServiceClient = new BlobServiceClient(account_url, credential);
ユーザー割り当てマネージド ID
SpringBoot クライアント
ユーザー割り当てマネージド ID を使う認証は、Spring Cloud Azure バージョン 4.0 以降でのみ使用できます。
既定の環境変数名 |
説明 |
値の例 |
spring.cloud.azure.storage.blob.credential.managed-identity-enabled |
マネージド ID を有効にするかどうか |
True |
spring.cloud.azure.storage.blob.account-name |
ストレージ アカウントの名前 |
storage-account-name |
spring.cloud.azure.storage.blob.endpoint |
Blob Storage エンドポイント |
https://<storage-account-name>.blob.core.windows.net/ |
spring.cloud.azure.storage.blob.credential.client-id |
ユーザー割り当てマネージド ID のクライアント ID |
00001111-aaaa-2222-bbbb-3333cccc4444 |
その他のクライアント
既定の環境変数名 |
説明 |
例値 |
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 エンドポイントを取得します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
依存関係のインストール
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);
pom.xml ファイルに次の依存関係を追加します。
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.1.5</version>
</dependency>
azure-identity
を使用して認証し、Service Connector によって追加された環境変数からエンドポイント URL を取得します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
String url = System.getenv("AZURE_STORAGEBLOB_RESOURCEENDPOINT");
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system managed identity
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder().build();
// for user assigned managed identity
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_STORAGEBLOB_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential defaultCredential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("<AZURE_STORAGEBLOB_CLIENTID>"))
// .clientSecret(System.getenv("<AZURE_STORAGEBLOB_CLIENTSECRET>"))
// .tenantId(System.getenv("<AZURE_STORAGEBLOB_TENANTID>"))
// .build();
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint(url)
.credential(defaultCredential)
.buildClient();
依存関係のインストール
pip install azure-identity
pip install azure-storage-blob
azure-identity
ライブラリを使用して認証し、Service Connector によって追加された環境変数からエンドポイント URL を取得します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
from azure.storage.blob import BlobServiceClient
import os
account_url = os.getenv('AZURE_STORAGEBLOB_RESOURCEENDPOINT')
# Uncomment the following lines corresponding to the authentication type you want to use.
# system assigned managed identity
# cred = ManagedIdentityCredential()
# user assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_STORAGEBLOB_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_STORAGEBLOB_TENANTID')
# client_id = os.getenv('AZURE_STORAGEBLOB_CLIENTID')
# client_secret = os.getenv('AZURE_STORAGEBLOB_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
blob_service_client = BlobServiceClient(account_url, credential=cred)
依存関係をインストールします。
pip install azure-identity
pip install django-storages[azure]
azure-identity
ライブラリ経由で認証します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
import os
# Uncomment the following lines corresponding to the authentication type you want to use.
# system assigned managed identity
# cred = ManagedIdentityCredential()
# user assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_STORAGEBLOB_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_STORAGEBLOB_TENANTID')
# client_id = os.getenv('AZURE_STORAGEBLOB_CLIENTID')
# client_secret = os.getenv('AZURE_STORAGEBLOB_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
設定ファイルで、次の行を追加します。 詳しくは、「django-storages[azure]」をご覧ください。
# in your setting file, eg. settings.py
AZURE_CUSTOM_DOMAIN = os.getenv('AZURE_STORAGEBLOB_RESOURCEENDPOINT')
AZURE_ACCOUNT_NAME = AZURE_CUSTOM_DOMAIN.split('.')[0].removeprefix('https://')
AZURE_TOKEN_CREDENTIAL = cred # this is the cred acquired from above step.
依存関係をインストールします。
go get "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
go get "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
コードで、azidentity
ライブラリ経由で認証します。 Service Connector によって追加された環境変数から Azure Blob Storage エンドポイントを取得します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
import (
"context"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
)
func main() {
account_endpoint = os.Getenv("AZURE_STORAGEBLOB_RESOURCEENDPOINT")
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// for user-assigned managed identity
// clientid := os.Getenv("AZURE_STORAGEBLOB_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// for service principal
// clientid := os.Getenv("AZURE_STORAGEBLOB_CLIENTID")
// tenantid := os.Getenv("AZURE_STORAGEBLOB_TENANTID")
// clientsecret := os.Getenv("AZURE_STORAGEBLOB_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
if err != nil {
// error handling
}
client, err := azblob.NewBlobServiceClient(account_endpoint, cred, nil)
}
依存関係のインストール
npm install --save @azure/identity
npm install @azure/storage-blob
Service Connector によって追加された環境変数から Azure Blob Storage エンドポイントを取得します。 @azure/identity
ライブラリ経由で認証します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
const { BlobServiceClient } = require("@azure/storage-blob");
const account_url = process.env.AZURE_STORAGEBLOB_RESOURCEENDPOINT;
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system assigned managed identity
// const credential = new DefaultAzureCredential();
// for user assigned managed identity
// const clientId = process.env.AZURE_STORAGEBLOB_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_STORAGEBLOB_TENANTID;
// const clientId = process.env.AZURE_STORAGEBLOB_CLIENTID;
// const clientSecret = process.env.AZURE_STORAGEBLOB_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const blobServiceClient = new BlobServiceClient(account_url, credential);
接続文字列
警告
Microsoft では、使用可能な最も安全な認証フローを使用することをお勧めします。 この手順で説明されている認証フローでは、アプリケーションで非常に高い信頼度が要求されるため、他のフローには存在しないリスクが伴います。 このフローは、マネージド ID など、より安全なフローが実行可能ではない場合にのみ使用してください。
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 アカウント名 |
<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);
pom.xml ファイルに次の依存関係を追加します。
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
</dependency>
環境変数から Azure Blob Storage に接続するための接続文字列を取得します。
String connectionStr = System.getenv("AZURE_STORAGEBLOB_CONNECTIONSTRING");
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.connectionString(connectionStr)
.buildClient();
- 依存関係のインストール
pip install azure-storage-blob
- Service Connector によって追加された環境変数から Azure Blob Storage 接続文字列を取得します。
from azure.storage.blob import BlobServiceClient
import os
connection_str = os.getenv('AZURE_STORAGEBLOB_CONNECTIONSTRING')
blob_service_client = BlobServiceClient.from_connection_string(connection_str)
依存関係をインストールします。
pip install django-storages[azure]
django のバージョンに応じて Django 設定ファイルで Azure Blob Storage バックエンドを構成して設定します。 詳しくは、「django-storages[azure]」をご覧ください。
設定ファイルで、次の行を追加します。
# in your setting file, eg. settings.py
AZURE_CONNECTION_STRING = os.getenv('AZURE_STORAGEBLOB_CONNECTIONSTRING')
- 依存関係をインストールします。
go get "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
- Service Connector によって追加された環境変数から Azure Blob Storage 接続文字列を取得します。
import (
"context"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
)
func main() {
connection_str = os.LookupEnv("AZURE_STORAGEBLOB_CONNECTIONSTRING")
client, err := azblob.NewClientFromConnectionString(connection_str, nil);
}
- 依存関係のインストール
npm install @azure/storage-blob
- Service Connector によって追加された環境変数から Azure Blob Storage 接続文字列を取得します。
const { BlobServiceClient } = require("@azure/storage-blob");
const connection_str = process.env.AZURE_STORAGEBLOB_CONNECTIONSTRING;
const blobServiceClient = BlobServiceClient.fromConnectionString(connection_str);
サービス プリンシパル
SpringBoot クライアント
サービス プリンシパルを使う認証は、Spring Cloud Azure バージョン 4.0 以降でのみ使用できます。
既定の環境変数名 |
説明 |
値の例 |
spring.cloud.azure.storage.blob.account-name |
ストレージ アカウントの名前 |
storage-account-name |
spring.cloud.azure.storage.blob.endpoint |
Blob Storage エンドポイント |
https://<storage-account-name>.blob.core.windows.net/ |
spring.cloud.azure.storage.blob.credential.client-id |
サービス プリンシパルのクライアント ID |
00001111-aaaa-2222-bbbb-3333cccc4444 |
spring.cloud.azure.storage.blob.credential.client-secret |
サービス プリンシパル認証を実行するためのクライアント シークレット |
Aa1Bb~2Cc3.-Dd4Ee5Ff6Gg7Hh8Ii9_Jj0Kk1Ll2 |
その他のクライアント
既定の環境変数名 |
説明 |
例値 |
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 エンドポイントを取得します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
依存関係のインストール
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);
pom.xml ファイルに次の依存関係を追加します。
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.1.5</version>
</dependency>
azure-identity
を使用して認証し、Service Connector によって追加された環境変数からエンドポイント URL を取得します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
String url = System.getenv("AZURE_STORAGEBLOB_RESOURCEENDPOINT");
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system managed identity
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder().build();
// for user assigned managed identity
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_STORAGEBLOB_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential defaultCredential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("<AZURE_STORAGEBLOB_CLIENTID>"))
// .clientSecret(System.getenv("<AZURE_STORAGEBLOB_CLIENTSECRET>"))
// .tenantId(System.getenv("<AZURE_STORAGEBLOB_TENANTID>"))
// .build();
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint(url)
.credential(defaultCredential)
.buildClient();
依存関係のインストール
pip install azure-identity
pip install azure-storage-blob
azure-identity
ライブラリを使用して認証し、Service Connector によって追加された環境変数からエンドポイント URL を取得します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
from azure.storage.blob import BlobServiceClient
import os
account_url = os.getenv('AZURE_STORAGEBLOB_RESOURCEENDPOINT')
# Uncomment the following lines corresponding to the authentication type you want to use.
# system assigned managed identity
# cred = ManagedIdentityCredential()
# user assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_STORAGEBLOB_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_STORAGEBLOB_TENANTID')
# client_id = os.getenv('AZURE_STORAGEBLOB_CLIENTID')
# client_secret = os.getenv('AZURE_STORAGEBLOB_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
blob_service_client = BlobServiceClient(account_url, credential=cred)
依存関係をインストールします。
pip install azure-identity
pip install django-storages[azure]
azure-identity
ライブラリ経由で認証します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
import os
# Uncomment the following lines corresponding to the authentication type you want to use.
# system assigned managed identity
# cred = ManagedIdentityCredential()
# user assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_STORAGEBLOB_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_STORAGEBLOB_TENANTID')
# client_id = os.getenv('AZURE_STORAGEBLOB_CLIENTID')
# client_secret = os.getenv('AZURE_STORAGEBLOB_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
設定ファイルで、次の行を追加します。 詳しくは、「django-storages[azure]」をご覧ください。
# in your setting file, eg. settings.py
AZURE_CUSTOM_DOMAIN = os.getenv('AZURE_STORAGEBLOB_RESOURCEENDPOINT')
AZURE_ACCOUNT_NAME = AZURE_CUSTOM_DOMAIN.split('.')[0].removeprefix('https://')
AZURE_TOKEN_CREDENTIAL = cred # this is the cred acquired from above step.
依存関係をインストールします。
go get "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
go get "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
コードで、azidentity
ライブラリ経由で認証します。 Service Connector によって追加された環境変数から Azure Blob Storage エンドポイントを取得します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
import (
"context"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
)
func main() {
account_endpoint = os.Getenv("AZURE_STORAGEBLOB_RESOURCEENDPOINT")
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// for user-assigned managed identity
// clientid := os.Getenv("AZURE_STORAGEBLOB_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// for service principal
// clientid := os.Getenv("AZURE_STORAGEBLOB_CLIENTID")
// tenantid := os.Getenv("AZURE_STORAGEBLOB_TENANTID")
// clientsecret := os.Getenv("AZURE_STORAGEBLOB_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
if err != nil {
// error handling
}
client, err := azblob.NewBlobServiceClient(account_endpoint, cred, nil)
}
依存関係のインストール
npm install --save @azure/identity
npm install @azure/storage-blob
Service Connector によって追加された環境変数から Azure Blob Storage エンドポイントを取得します。 @azure/identity
ライブラリ経由で認証します。 次のコードを使用する場合は、使用する認証の種類のコード スニペットの部分をコメント解除します。
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
const { BlobServiceClient } = require("@azure/storage-blob");
const account_url = process.env.AZURE_STORAGEBLOB_RESOURCEENDPOINT;
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system assigned managed identity
// const credential = new DefaultAzureCredential();
// for user assigned managed identity
// const clientId = process.env.AZURE_STORAGEBLOB_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_STORAGEBLOB_TENANTID;
// const clientId = process.env.AZURE_STORAGEBLOB_CLIENTID;
// const clientSecret = process.env.AZURE_STORAGEBLOB_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const blobServiceClient = new BlobServiceClient(account_url, credential);
次のステップ
Service Connector の詳細については、チュートリアルに従ってください。