الاستعلام عن نقطة نهاية Blob Storage باستخدام مكتبة إدارة تخزين Azure
مقالة
تشكل نقطة نهاية Blob Storage العنوان الأساسي لكافة الكائنات داخل حساب تخزين. عند إنشاء حساب تخزين، يمكنك تحديد نوع نقطة النهاية التي تريد استخدامها. يدعم Blob Storage نوعين من نقاط النهاية:
تتضمن نقطة النهاية القياسية اسم حساب التخزين الفريد مع اسم مجال ثابت. تنسيق نقطة النهاية القياسية هو https://<storage-account>.blob.core.windows.net.
تحدد نقطة نهاية منطقة Azure DNS (معاينة) منطقة Azure DNS ديناميكيا وتعينها إلى حساب التخزين عند إنشائها. تنسيق نقطة نهاية منطقة DNS Azure هو https://<storage-account>.z[00-99].blob.storage.azure.net.
عندما ينشئ التطبيق الخاص بك كائن عميل خدمة يتصل بموارد بيانات Blob Storage، يمكنك تمرير URI يشير إلى نقطة النهاية إلى منشئ عميل الخدمة. يمكنك إنشاء سلسلة URI يدويا، أو يمكنك الاستعلام عن نقطة نهاية الخدمة في وقت التشغيل باستخدام مكتبة إدارة تخزين Azure.
هام
عند الرجوع إلى نقطة نهاية خدمة في تطبيق عميل، يوصى بتجنب الاعتماد على عنوان IP مخزن مؤقتا. يخضع عنوان IP لحساب التخزين للتغيير، وقد يؤدي الاعتماد على عنوان IP المخزن مؤقتا إلى سلوك غير متوقع.
يمكن تغيير CNAMEs المقترنة بنقطة نهاية حساب تخزين دون إشعار. يجب ألا يعتمد التطبيق الخاص بك على عدد سجلات CNAME أو المجالات الفرعية المقترنة بسجلات CNAME هذه.
بالإضافة إلى ذلك، يوصى بتكريم وقت البقاء (TTL) لسجل DNS وتجنب تجاوزه. قد يؤدي تجاوز DNS TTL إلى سلوك غير متوقع.
توفر مكتبة إدارة Azure Storage وصولا برمجيا إلى موفر موارد Azure Storage. موفر الموارد هو تنفيذ Azure Storage ل Azure Resource Manager. تمكن مكتبة الإدارة المطورين من إدارة حسابات التخزين وتكوين الحساب، بالإضافة إلى تكوين نهج إدارة دورة الحياة ونهج النسخ المتماثل للكائنات ونهج عدم قابلية التغيير.
في هذه المقالة، ستتعلم كيفية الاستعلام عن نقطة نهاية Blob Storage باستخدام مكتبة إدارة تخزين Azure. ثم يمكنك استخدام نقطة النهاية هذه لإنشاء كائن BlobServiceClient للاتصال بموارد بيانات Blob Storage.
إعداد مشروعك
لاستخدام أمثلة التعليمات البرمجية في هذه المقالة، اتبع هذه الخطوات لإعداد مشروعك.
تثبيت الحزم
تثبيت الحزم للعمل مع المكتبات المستخدمة في هذا المثال.
أضف azure-sdk-bom للحصول على تبعية على أحدث إصدار من المكتبة. في القصاصة البرمجية التالية، استبدل {bom_version_to_target} العنصر النائب برقم الإصدار. استخدام azure-sdk-bom يمنعك من الاضطرار إلى تحديد إصدار كل تبعية فردية. لمعرفة المزيد حول قائمة مكونات الصنف، راجع README ل Azure SDK BOM.
أضف التوجيهات أو import الضرورية using إلى التعليمات البرمجية. لاحظ أن أمثلة التعليمات البرمجية قد تقسم الوظائف بين الملفات، ولكن في هذا القسم يتم سرد جميع التوجيهات معا.
using Azure.Core;
using Azure.Identity;
using Azure.Storage.Blobs;
using Azure.ResourceManager;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Storage;
معلومات مكتبة العميل:
Azure.Identity: يوفر دعم مصادقة الرمز المميز ل Microsoft Entra عبر Azure SDK، وهو مطلوب للاتصالات بدون كلمة مرور بخدمات Azure.
@azure/arm-storage: يدعم إدارة موارد Azure 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-identity: يوفر دعم مصادقة الرمز المميز ل Microsoft Entra عبر Azure SDK، وهو مطلوب للاتصالات بدون كلمة مرور بخدمات Azure.
azure-storage-blob: يحتوي على الفئات الأساسية التي يمكنك استخدامها للعمل مع موارد بيانات Blob Storage.
azure-mgmt-storage: يدعم إدارة موارد Azure Storage، بما في ذلك مجموعات الموارد وحسابات التخزين.
تسجيل موفر موارد التخزين مع اشتراك
يجب تسجيل موفر الموارد مع اشتراك Azure الخاص بك قبل أن تتمكن من العمل معه. يجب تنفيذ هذه الخطوة مرة واحدة فقط لكل اشتراك، ولا تنطبق إلا إذا لم يكن موفر الموارد Microsoft.Storage مسجلا حاليا في اشتراكك.
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 Storage
لاسترداد نقطة نهاية Blob Storage لحساب تخزين معين، نحتاج إلى الحصول على خصائص حساب التخزين عن طريق استدعاء عملية Get Properties . تستخدم نماذج التعليمات البرمجية التالية كلا من مكتبات الوصول إلى البيانات والإدارة للحصول على نقطة نهاية Blob Storage لحساب تخزين محدد:
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 :
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;
}
يقوم هذا الأسلوب بإرجاع كائن 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 ...