Blob Storage エンドポイントは、ストレージ アカウント内のすべてのオブジェクトのベース アドレスを形成します。 ストレージ アカウントを作成するときは、使用するエンドポイントの種類を指定します。 Blob Storage では、2 種類のエンドポイントがサポートされています。
- 標準エンドポイントには、一意のストレージ アカウント名と固定ドメイン名が含まれます。 標準エンドポイントの形式は
https://<storage-account>.blob.core.windows.net
です。
- Azure DNS ゾーン エンドポイント (プレビュー) では、Azure DNS ゾーンが動的に選択され、作成時にストレージ アカウントに割り当てられます。 Azure DNS ゾーン エンドポイントの形式は
https://<storage-account>.z[00-99].blob.storage.azure.net
です。
Blob Storage データ リソースに接続するサービス クライアント オブジェクトをアプリケーションで作成する場合は、エンドポイントを参照する URI をサービス クライアント コンストラクターに渡します。 URI 文字列は手動で作成することも、Azure Storage 管理ライブラリを使用して実行時にサービス エンドポイントに対してクエリを実行することもできます。
重要
クライアント アプリケーションでサービス エンドポイントを参照する場合は、キャッシュされた IP アドレスに依存しないようにすることをお勧めします。 ストレージ アカウントの IP アドレスは変更される可能性があるため、キャッシュされた IP アドレスに依存すると、予期しない動作が発生する可能性があります。
ストレージ アカウント エンドポイントに関連付けられている CNAME は、予告なしに変更される可能性があります。 アプリケーションは、CNAME レコードの数や、それらの CNAME レコードに関連付けられているサブドメインに依存しないようにする必要があります。
さらに、DNS レコードの有効期限 (TTL) を守り、オーバーライドしないようにすることをお勧めします。 DNS TTL をオーバーライドすると、予期しない動作が発生する可能性があります。
詳細については、「CNAME レコード、サブドメイン、および IP アドレス」を参照してください。
Azure Storage 管理ライブラリは、Azure Storage リソース プロバイダーへのプログラムによるアクセスを提供します。 リソース プロバイダーは、Azure Resource Manager の Azure Storage 実装です。 管理ライブラリを使用すると、開発者はストレージ アカウントとアカウント構成を管理したり、ライフサイクル管理ポリシー、オブジェクト レプリケーション ポリシー、不変ポリシーを構成したりできます。
この記事では、Azure Storage 管理ライブラリを使用して Blob Storage エンドポイントのクエリを実行する方法について説明します。 その後、そのエンドポイントを使用して、Blob Storage データ リソースに接続する BlobServiceClient
オブジェクトを作成します。
プロジェクトの設定
この記事のコード例を使用するには、次の手順に従ってプロジェクトを設定します。
パッケージをインストールする
この例で使用されているライブラリを操作するためのパッケージをインストールします。
dotnet add package
を使用して次のパッケージをインストールしてください。
dotnet add package Azure.Identity
dotnet add package Azure.ResourceManager.Storage
dotnet add package Azure.Storage.Blobs
テキスト エディターで pom.xml
ファイルを開きます。
azure-sdk-bom を追加して、最新バージョンのライブラリへの依存関係を設定します。 次のスニペットでは、{bom_version_to_target}
プレースホルダーをバージョン番号に置き換えます。 azure-sdk-bom を使用すると、個々の依存関係のバージョンを指定する必要がなくなります。 BOM の詳細については、AZURE SDK BOM に関するページを参照してください。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-sdk-bom</artifactId>
<version>{bom_version_to_target}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
その後、依存関係のグループに、次の dependency 要素を追加します。 Azure サービスへのパスワードレス接続には、azure-identity 依存関係が必要です。
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
</dependency>
<dependency>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager</artifactId>
<version>2.24.0</version>
</dependency>
<dependency>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager-storage</artifactId>
<version>2.24.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-management</artifactId>
<version>1.10.2</version>
</dependency>
npm install
を使用して次のパッケージをインストールしてください。
npm install @azure/identity
npm install @azure/storage-blob
npm install @azure/arm-resources
npm install @azure/arm-storage
pip install
を使用して次のパッケージをインストールしてください。
pip install azure-identity
pip install azure-storage-blob
pip install azure-mgmt-resource
pip install azure-mgmt-storage
アプリ コードを設定する
必要な using
または import
ディレクティブをコードに追加します。 コード例ではファイル間で機能が分割される場合がありますが、このセクションではすべてのディレクティブが一緒に一覧表示されます。
次の using
ディレクティブを追加します。
using Azure.Core;
using Azure.Identity;
using Azure.Storage.Blobs;
using Azure.ResourceManager;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Storage;
クライアント ライブラリ情報:
次の import
ディレクティブを追加します。
import com.azure.identity.*;
import com.azure.storage.blob.*;
import com.azure.resourcemanager.*;
import com.azure.resourcemanager.storage.models.*;
import com.azure.core.management.*;
import com.azure.core.management.profile.*;
クライアント ライブラリ情報:
次の require
ステートメントを追加してモジュールを読み込みます。
const { DefaultAzureCredential } = require("@azure/identity");
const { BlobServiceClient } = require("@azure/storage-blob");
const { ResourceManagementClient } = require("@azure/arm-resources");
const { StorageManagementClient } = require("@azure/arm-storage");
クライアント ライブラリ情報:
次の import
ステートメントを追加します。
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.storage import StorageManagementClient
クライアント ライブラリ情報:
ストレージ リソースプロバイダーをサブスクリプションに登録する
リソース プロバイダーを使用するには、Azure サブスクリプションに登録しておく必要があります。 この手順は、サブスクリプションごとに 1 回だけ実行する必要があり、リソース プロバイダー Microsoft.Storage が現在サブスクリプションに登録されていない場合にのみ適用されます。
ストレージ リソース プロバイダーを登録するか、Azure portal、Azure CLI、または Azure PowerShell を使用して登録状態をチェックできます。
次の例に示すように、Azure 管理ライブラリを使用して登録状態をチェックし、ストレージ リソース プロバイダーを登録することもできます。
public static async Task RegisterSRPInSubscription(SubscriptionResource subscription)
{
ResourceProviderResource resourceProvider =
await subscription.GetResourceProviderAsync("Microsoft.Storage");
// Check the registration state of the resource provider and register, if needed
if (resourceProvider.Data.RegistrationState == "NotRegistered")
resourceProvider.Register();
}
public void RegisterSRPInSubscription(AzureResourceManager armClient) {
// Check the registration state of the resource provider and register, if needed
if (armClient.providers().getByName("Microsoft.Storage").registrationState() == "NotRegistered")
armClient.providers().register("Microsoft.Storage");
}
async function registerSRPInSubscription(resourceMgmtClient /*: ResourceManagementClient*/) {
// Check the registration state of the resource provider and register, if needed
if (resourceMgmtClient.providers.get("Microsoft.Storage").registrationState == "NotRegistered")
resourceMgmtClient.providers.register("Microsoft.Storage");
}
def register_srp_in_subscription(self, resource_mgmt_client: ResourceManagementClient):
if (resource_mgmt_client.providers.get("Microsoft.Storage").registration_state == "NotRegistered"):
resource_mgmt_client.providers.register("Microsoft.Storage")
Note
登録操作を実行するには、次の Azure RBAC アクション Microsoft.Storage/register/action に対するアクセス許可が必要です。 このアクセス許可は、共同作成者と所有者ロールに含まれます。
Blob Storage エンドポイントのクエリ
特定のストレージ アカウントの Blob Storage エンドポイントを取得するには、Get Properties 操作を呼び出してストレージ アカウントのプロパティを取得する必要があります。 次のコード サンプルでは、データ アクセス ライブラリと管理ライブラリの両方を使用して、指定されたストレージ アカウントの Blob Storage エンドポイントを取得します。
指定したストレージ アカウントのプロパティを取得するには、StorageAccountCollection オブジェクトから次のメソッドを使用します。
このメソッドは、ストレージ アカウントを表す StorageAccountResource オブジェクトを返します。
public static async Task<Uri> GetBlobServiceEndpoint(
string storageAccountName,
TokenCredential credential)
{
// TODO: replace with your subscription ID and resource group name
// You can locate your subscription ID on the Subscriptions blade
// of the Azure portal (https://portal.azure.com)
const string subscriptionId = "<subscription-id>";
const string rgName = "<resource-group-name>";
ArmClient armClient = new(credential);
// Create a resource identifier, then get the subscription resource
ResourceIdentifier resourceIdentifier = new($"/subscriptions/{subscriptionId}");
SubscriptionResource subscription = armClient.GetSubscriptionResource(resourceIdentifier);
// Get a resource group
ResourceGroupResource resourceGroup = await subscription.GetResourceGroupAsync(rgName);
// Get a collection of storage account resources
StorageAccountCollection accountCollection = resourceGroup.GetStorageAccounts();
// Get the properties for the specified storage account
StorageAccountResource storageAccount = await accountCollection.GetAsync(storageAccountName);
// Return the primary endpoint for the blob service
return storageAccount.Data.PrimaryEndpoints.BlobUri;
}
指定したストレージ アカウントのプロパティを取得するには、AzureResourceManager オブジェクトから次のメソッドを使用します。
このメソッドは、ストレージ アカウントの不変のクライアント側表現である StorageAccount インターフェイスを返します。
public String GetBlobServiceEndpoint(String saName, DefaultAzureCredential credential) {
String subscriptionID = "<subscription-id>";
String rgName = "<resource-group-name>";
AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
AzureResourceManager azureResourceManager = AzureResourceManager
.configure()
.authenticate(credential, profile)
.withSubscription(subscriptionID);
StorageAccount storageAccount = azureResourceManager.storageAccounts()
.getByResourceGroup(rgName, saName);
String endpoint = storageAccount.endPoints().primary().blob();
return endpoint;
}
指定したストレージ アカウントのプロパティを取得するには、StorageManagementClient オブジェクトから次のメソッドを使用します。
このメソッドは、ストレージ アカウントを表す Promise<StorageAccountsGetPropertiesResponse>
を返します。
async function getBlobServiceEndpoint(saName, credential) {
const subscriptionId = "<subscription-id>";
const rgName = "<resource-group-name>";
const storageMgmtClient = new StorageManagementClient(
credential,
subscriptionId
);
// Get the properties for the specified storage account
const storageAccount = await storageMgmtClient.storageAccounts.getProperties(
rgName,
saName
);
// Get the primary endpoint for the blob service
const endpoint = storageAccount.primaryEndpoints.blob;
return endpoint;
}
指定したストレージ アカウントのプロパティを取得するには、StorageManagementClient オブジェクトから次のメソッドを使用します。
このメソッドは、ストレージ アカウントを表す StorageAccount
オブジェクトを返します。
def get_blob_service_endpoint(self, storage_account_name, credential: DefaultAzureCredential) -> str:
subscription_id = "<subscription-id>"
rg_name = "<resource-group-name>"
storage_mgmt_client = StorageManagementClient(
credential=credential,
subscription_id=subscription_id
)
# Get the properties for the specified storage account
storage_account = storage_mgmt_client.storage_accounts.get_properties(
resource_group_name=rg_name,
account_name=storage_account_name
)
# Get blob service endpoint
endpoint = storage_account.primary_endpoints.blob
return endpoint
エンドポイントを使用してクライアント オブジェクトを作成する
ストレージ アカウントの Blob Storage エンドポイントを取得したら、クライアント オブジェクトをインスタンス化してデータ リソースを操作できます。 次のコード サンプルでは、前の例で取得したエンドポイントを使用して BlobServiceClient
オブジェクトを作成します。
// Create an instance of DefaultAzureCredential for authorization
TokenCredential credential = new DefaultAzureCredential();
// TODO: replace with your storage account name
string storageAccountName = "<storage-account-name>";
// Call out to our function that retrieves the blob service endpoint for the given storage account
Uri blobURI = await AccountProperties.GetBlobServiceEndpoint(storageAccountName, credential);
Console.WriteLine($"URI: {blobURI}");
// Now that we know the endpoint, create the client object
BlobServiceClient blobServiceClient = new(blobURI, credential);
// Do something with the storage account or its resources ...
String saName = "<storage-account-name>";
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
AccountProperties accountProps = new AccountProperties();
String blobEndpoint = accountProps.GetBlobServiceEndpoint(saName, credential);
System.out.printf("URI: %s", blobEndpoint);
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint(blobEndpoint)
.credential(credential)
.buildClient();
// Do something with the storage account or its resources ...
// For client-side applications running in the browser, use InteractiveBrowserCredential instead of DefaultAzureCredential.
// See https://aka.ms/azsdk/js/identity/examples for more details.
const saName = "<storage-account-name>";
const credential = new DefaultAzureCredential();
// Call out to our function that retrieves the blob service endpoint for a storage account
const endpoint = await getBlobServiceEndpoint(saName, credential)
console.log(endpoint);
// Now that we know the endpoint, create the client object
const blobServiceClient = new BlobServiceClient(
endpoint,
credential);
// Do something with the storage account or its resources ...
storage_account_name = "<storage-account-name>"
credential = DefaultAzureCredential()
sample = BlobEndpointSample()
# Call out to our function that retrieves the blob service endpoint for a storage account
endpoint = sample.get_blob_service_endpoint(storage_account_name, credential)
print(f"URL: {endpoint}")
# Now that we know the endpoint, create the client object
blob_service_client = BlobServiceClient(account_url=endpoint, credential=credential)
# Do something with the storage account or its resources ...
次のステップ
完全なコード サンプルを表示する (GitHub):
クライアント オブジェクトの作成の詳細については、「データ リソースを操作するクライアント オブジェクトを作成および管理する」を参照してください。