透過 Python 使用 Blob 索引標籤來管理和尋找資料
本文說明如何利用適用於 Python 的 Azure 儲存體用戶端程式庫來使用 Blob 索引標籤以管理和尋找資料。
若要了解使用非同步 API 設定 Blob 索引標籤,請參閱非同步地設定 Blob 索引標籤 (部分機器翻譯)。
必要條件
- 本文假設您已有專案準備好要使用適用於 Python 的 Azure Blob 儲存體用戶端程式庫。 若要了解設定您的專案,包括套件安裝、新增
import
陳述式以及建立授權的用戶端物件,請參閱開始使用 Azure Blob 儲存體和 Python。 - 授權機制必須具有使用 Blob 索引標籤的權限。 若要深入了解,請參閱下列 REST API 作業的授權指導:
關於 Blob 索引標籤
Blob 索引標記會使用索引鍵/值標記屬性,將儲存體帳戶中的資料分類。 這些標記會自動編製索引,並公開為可搜尋的多維度索引,以便輕鬆地尋找資料。 本文說明如何使用 Blob 索引標記來設定、取得及尋找資料。
已啟用階層命名空間的儲存體帳戶不支援 Blob 索引標籤。 若要深入了解 Blob 索引標籤功能以及已知問題和限制,請參閱使用 Blob 索引標籤來管理及尋找 Azure Blob 資料。
設定標記
如果您的程式碼透過以下其中一種機制獲得授權可以存取 Blob 資料,則您可以設定索引標籤:
- 獲指派 Azure RBAC 角色,並具有 Microsoft.Storage/storageAccounts/blobServices/containers/blobs/tags/write 動作的安全性主體。 儲存體 Blob 資料擁有者是包含此動作的內建角色。
- 具有存取 Blob 標籤之權限 (
t
權限) 的共用存取簽章 (SAS) - 帳戶金鑰
如需詳細資訊,請參閱設定 Blob 索引標籤。
您可以使用下列方法來設定標籤:
此方法中的指定標籤會取代現有的標籤。 如果必須保留舊值,則必須下載它們並將其包含在對此方法的呼叫中。 下列範例示範如何設定標籤:
def set_blob_tags(self, blob_service_client: BlobServiceClient, container_name):
blob_client = blob_service_client.get_blob_client(container=container_name, blob="sample-blob.txt")
# Get any existing tags for the blob if they need to be preserved
tags = blob_client.get_blob_tags()
# Add or modify tags
updated_tags = {'Sealed': 'false', 'Content': 'image', 'Date': '2022-01-01'}
tags.update(updated_tags)
blob_client.set_blob_tags(tags)
您可以將空的 dict
物件傳遞至 set_blob_tags
方法來刪除所有標籤:
def clear_blob_tags(self, blob_service_client: BlobServiceClient, container_name):
blob_client = blob_service_client.get_blob_client(container=container_name, blob="sample-blob.txt")
# Pass in empty dict object to clear tags
tags = dict()
blob_client.set_blob_tags(tags)
取得標籤
如果您的程式碼透過以下其中一種機制獲得授權可以存取 Blob 資料,則您可以取得索引標籤:
- 獲指派 Azure RBAC 角色,並具有 Microsoft.Storage/storageAccounts/blobServices/containers/blobs/tags/read 動作的安全性主體。 儲存體 Blob 資料擁有者是包含此動作的內建角色。
- 具有存取 Blob 標籤之權限 (
t
權限) 的共用存取簽章 (SAS) - 帳戶金鑰
如需詳細資訊,請參閱擷取和列出 Blob 索引標籤。
您可以使用下列方法來取得標籤:
下列範例示範如何擷取和逐一查看 Blob 的標籤:
def get_blob_tags(self, blob_service_client: BlobServiceClient, container_name):
blob_client = blob_service_client.get_blob_client(container=container_name, blob="sample-blob.txt")
tags = blob_client.get_blob_tags()
print("Blob tags: ")
for k, v in tags.items():
print(k, v)
使用 Blob 索引標記篩選和尋找資料
如果您的程式碼透過以下其中一種機制獲得授權可以存取 Blob 資料,則您可以使用索引標籤來尋找和篩選資料:
- 獲指派 Azure RBAC 角色,並具有 Microsoft.Storage/storageAccounts/blobServices/containers/blobs/filter/action 動作的安全性主體。 儲存體 Blob 資料擁有者是包含此動作的內建角色。
- 具有依標籤篩選 Blob 之權限 (
f
權限) 的共用存取簽章 (SAS) - 帳戶金鑰
如需詳細資訊,請參閱使用 Blob 索引標籤來尋找資料。
注意
您無法使用索引標籤來擷取先前的版本。 先前版本的標記不會傳遞至 Blob 索引引擎。 如需詳細資訊,請參閱條件和已知問題。
您可以使用下列方法來尋找資料:
下列範例會尋找並列出標記為影像的所有 Blob:
def find_blobs_by_tags(self, blob_service_client: BlobServiceClient, container_name):
container_client = blob_service_client.get_container_client(container=container_name)
query = "\"Content\"='image'"
blob_list = container_client.find_blobs_by_tags(filter_expression=query)
print("Blobs tagged as images")
for blob in blob_list:
print(blob.name)
非同步地設定 Blob 索引標籤
適用於 Python 的 Azure Blob 儲存體用戶端程式庫支援非同步地使用 Blob 索引標籤。 若要深入了解專案設定需求,請參閱非同步程式設計。
依照下列步驟來使用非同步 API 設定 Blob 索引標籤:
新增下列匯入陳述式:
import asyncio from azure.identity.aio import DefaultAzureCredential from azure.storage.blob.aio import BlobServiceClient
新增程式碼以使用
asyncio.run
執行程式。 此函式會執行傳遞的協同程式 (在我們的範例中為main()
),並管理asyncio
事件重複。 協同程式會以 async/await 語法宣告。 在此範例中,main()
協同程式會先使用async with
建立最上層BlobServiceClient
,然後呼叫設定 Blob 索引標籤的方法。 請注意,只有最上層用戶端需要使用async with
,因為從中建立的其他用戶端會共用相同的連線集區。async def main(): sample = BlobSamples() # TODO: Replace <storage-account-name> with your actual storage account name account_url = "https://<storage-account-name>.blob.core.windows.net" credential = DefaultAzureCredential() async with BlobServiceClient(account_url, credential=credential) as blob_service_client: await sample.set_blob_tags(blob_service_client, "sample-container") if __name__ == '__main__': asyncio.run(main())
新增程式碼以設定 Blob 索引標籤。 程式碼與同步範例相同,不同之處在於方法是以
async
關鍵字宣告,而且await
關鍵字是在呼叫get_blob_tags
和set_blob_tags
方法時才會使用。async def set_blob_tags(self, blob_service_client: BlobServiceClient, container_name): blob_client = blob_service_client.get_blob_client(container=container_name, blob="sample-blob.txt") # Get any existing tags for the blob if they need to be preserved tags = await blob_client.get_blob_tags() # Add or modify tags updated_tags = {'Sealed': 'false', 'Content': 'image', 'Date': '2022-01-01'} tags.update(updated_tags) await blob_client.set_blob_tags(tags)
完成這個基本設定後,您可以使用 async/await 語法將本文中的其他範例實作為協同程式。
資源
若要深入了解如何利用適用於 Python 的 Azure Blob 儲存體用戶端程式庫來使用索引標籤管理及尋找資料,請參閱下列資源。
REST API 操作
Azure SDK for Python 包含建置在 Azure REST API 之上的程式庫,可讓您透過熟悉的 Python 範例與 REST API 作業進行互動。 用來管理和使用 Blob 索引標籤的用戶端程式庫方法會使用下列 REST API 作業:
- 取得 Blob 標籤 (REST API)
- 設定 Blob 標籤 (REST API)
- 依標籤來尋找 Blob (REST API)
程式碼範例
用戶端程式庫資源
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應