Blob 儲存體端點會形成儲存體帳戶內所有物件的基底位址。 當您建立儲存體帳戶時,您會指定要使用的端點類型。 Blob 儲存體支援兩種端點:
- 標準端點包含唯一的儲存體帳戶名稱以及固定的網域名稱。 標準端點的格式為
https://<storage-account>.blob.core.windows.net。
- Azure DNS 區域端點 (預覽版) 會動態選取 Azure DNS 區域,並在建立時將其指派給儲存體帳戶。 Azure DNS 區域端點的格式為
https://<storage-account>.z[00-99].blob.storage.azure.net。
當您的應用程式建立會連線至 Blob 儲存體資料資源的服務用戶端物件時,您會將參考端點的 URI 傳遞至服務用戶端建構函式。 您可以手動建構 URI 字串,或使用 Azure 儲存體管理程式庫在執行階段查詢服務端點。
重要
在用戶端應用程式中參考服務端點時,建議您避免相依於快取的 IP 位址。 儲存體帳戶的 IP 位址可能會變更,依賴快取的 IP 位址可能會導致非預期的行為。
與儲存體帳戶端點相關聯的 CNAME 可能會不經通知就變更。 您的應用程式不應相依於與這些 CNAME 記錄相關聯的 CNAME 記錄數目或子網域數目。
此外,建議您接受 DNS 記錄的存留時間 (TTL),並避免將其覆寫。 覆寫 DNS TTL 可能會導致非預期的行為。
如需詳細資訊,請參閱 CNAME 記錄、子網域和 IP 位址。
Azure 儲存體管理程式庫可透過程式設計方式提供 Azure 儲存體資源提供者的存取權。 資源提供者是 Azure Resource Manager 的 Azure 儲存體實作。 管理程式庫可讓開發人員管理儲存體帳戶和帳戶設定,以及設定生命週期管理原則、物件複寫原則和不變性原則。
在本文中,您會了解如何使用 Azure 儲存體管理程式庫來查詢 Blob 儲存體端點。 然後,您會使用該端點建立 BlobServiceClient 物件,以與 Blob 儲存體資料資源連線。
設定您的專案
若要使用本文中的程式碼範例,請遵循下列步驟來設定您的專案。
安裝套件
安裝套件以處理此範例中使用的程式庫。
使用 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>
然後,將下列相依性元素新增至相依性群組。 需要 azure-identity 相依性才能對 Azure 服務進行無密碼連線。
<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 訂用帳戶註冊資源提供者,才能對其進行處理。 每個訂用帳戶只需要進行一次此步驟,而且只有在目前未使用訂用帳戶註冊資源提供者 Microsoft.Storage 時才適用此步驟。
您可以使用 Azure 入口網站、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")
注意
若要執行註冊作業,您需要下列 Azure RBAC 動作的權限:Microsoft.Storage/register/action。 此權限包括在參與者和擁有者角色中。
查詢 Blob 儲存體端點
為了擷取指定儲存體帳戶的 Blob 儲存體端點,我們需要藉由呼叫取得屬性作業來取得儲存體帳戶屬性。 下列程式碼範例會使用資料存取和管理程式庫來取得指定儲存體帳戶的 Blob 儲存體端點:
若要取得指定儲存體帳戶的屬性,請從 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 儲存體端點後,便可以具現化用戶端物件來處理資料資源。 下列程式碼範例會使用先前範例中擷取的端點來建立 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):
若要深入了解如何建立用戶端物件,請參閱建立和管理與資料資源互動的用戶端端物件 (部分機器翻譯)。