重要
Microsoft Purview データ ガバナンス機能のみの廃止
Microsoft Purview 統合カタログと従来の Microsoft Purview データ ガバナンス機能 (旧称 Azure Purview) は廃止されます。 Microsoft Purview 全体は廃止されません。 Microsoft Purview のデータ セキュリティとコンプライアンス機能は、この発表の影響を受けず、Microsoft は引き続き投資します。
Microsoft は、Microsoft Fabric のデータ ガバナンス 機能を統合しています。 新しい Microsoft Purview データ ガバナンス アカウントは作成できなくなり、既存のデータ ガバナンス機能は 2027 年 11 月 12 日までメンテナンス モードのままになります。
廃止のタイムライン、移行ガイダンス、推奨される次の手順については、 Microsoft Purview データ ガバナンスの廃止のお知らせを参照してください。
このチュートリアルでは、Microsoft Purview Python SDK の使用方法を紹介します。 SDK を使用すると、Microsoft Purview ガバナンス ポータルを使用するのではなく、プログラムで最も一般的な Microsoft Purview 操作をすべて実行できます。
このチュートリアルでは、SDK を使用して次のことを行う方法について説明します。
- Microsoft Purview をプログラムで操作するために必要な権限を付与する
- Blob Storage コンテナーをデータ ソースとして Microsoft Purview に登録する
- スキャンの定義と実行
- カタログを検索する
- データ ソースを削除する
前提条件
このチュートリアルには、次のものが必要です。
- Python 3.6 以降
- アクティブな Azure サブスクリプション。 お持ちでない場合は、 無料で作成できます。
- サブスクリプションに関連付けられている Microsoft Entra テナント。
- Azure Storage アカウント。 まだお持ちでない場合は、 クイックスタート ガイドに従って作成できます。
- Microsoft Purview アカウント。 まだお持ちでない場合は、 クイックスタート ガイドに従って作成できます。
- クライアント シークレットを持つサービス プリンシパル。
重要
これらのスクリプトの場合、エンドポイントの値は使用している Microsoft Purview ポータルによって異なります。 従来の Microsoft Purview ガバナンス ポータルのエンドポイント: purview.azure.com/ 新しい Microsoft Purview ポータルのエンドポイント: purview.microsoft.com/
そのため、新しいポータルを使用している場合、エンドポイントの値は "https://consotopurview.scan.purview.microsoft.com"
Microsoft Purview にストレージ アカウントへのアクセス権を付与する
ストレージ アカウントのコンテンツをスキャンできるようにするには、Microsoft Purview に適切な役割を与える必要があります。
Azure portal からストレージ アカウントにアクセスします。
アクセス制御 (IAM) を選択します。
[追加] ボタンを選択し、[ ロール割り当ての追加] を選択します。
次のウィンドウで、 ストレージ BLOB 閲覧者 ロールを検索して選択します。
次に、[ メンバー] タブに移動し、[ メンバーの選択] を選択します。
右側に新しいウィンドウが表示されます。 既存の Microsoft Purview インスタンスの名前を検索して選択します。
その後、[ レビュー + 割り当て] を選択できます。
Microsoft Purview には、BLOB ストレージのスキャンに必要な読み取り権限があります。
アプリケーションに Microsoft Purview アカウントへのアクセス権を付与する
まず、サービス プリンシパルからクライアント ID、テナント ID、およびクライアント シークレットが必要です。 この情報を見つけるには、 Microsoft Entra ID を選択します。
次に、[ アプリの登録] を選択します。
アプリケーションを選択し、必要な情報を見つけます。
名前
クライアント ID (またはアプリケーション ID)
テナント ID (またはディレクトリ ID)
-
ここで、関連する Microsoft Purview ロールをサービス プリンシパルに付与する必要があります。 これを行うには、Microsoft Purview インスタンスにアクセスします。 [ Microsoft Purview ガバナンス ポータルを開く ] を選択するか、 Microsoft Purview のガバナンス ポータルを直接 開いて、デプロイしたインスタンスを選択します。
Microsoft Purview ガバナンス ポータル内で、[データ マップ]、[コレクション] の順に選択します。
使用するコレクションを選択し、[ ロールの割り当て] タブに移動します。次のロールにサービス プリンシパルを追加します。
- コレクション管理者
- データ ソース管理者
- データキュレーター
- データ リーダー
ロールごとに、[ ロールの割り当ての編集 ] ボタンを選択し、サービス プリンシパルを追加するロールを選択します。 または、各ロールの横にある [追加 ] ボタンを選択し、次に示すように、サービス プリンシパルの名前またはクライアント ID を検索してサービス プリンシパルを追加します。
Python パッケージをインストールする
- 新しいコマンド プロンプトまたはターミナルを開きます
- 認証用の Azure ID パッケージをインストールします。
pip install azure-identity - Microsoft Purview スキャン クライアント パッケージをインストールします。
pip install azure-purview-scanning - Microsoft Purview 管理クライアント パッケージをインストールします。
pip install azure-purview-administration - Microsoft Purview クライアント パッケージをインストールします。
pip install azure-purview-catalog - Microsoft Purview アカウント パッケージをインストールします。
pip install azure-purview-account - Azure Core パッケージをインストールします。
pip install azure-core
Python スクリプト ファイルを作成する
プレーン テキスト ファイルを作成し、サフィックス .py付きの Python スクリプトとして保存します。 例: tutorial.py。
スキャン、カタログ、および管理クライアントをインスタンス化する
このセクションでは、以下をインスタンス化する方法について説明します。
- データソースの登録、スキャンルールの作成と管理、スキャンのトリガーなどに役立つスキャンクライアント。
- 検索、検出されたアセットの参照、データの機密性の識別などを通じてカタログを操作する場合に役立つカタログクライアント。
- 管理クライアントは、コレクションの一覧表示などの操作のために、Microsoft Purview データ マップ自体を操作するのに役立ちます。
まず、Microsoft Entra ID に対して認証する必要があります。 このためには、 作成したクライアント シークレットを使用します。
必要な import ステートメント (3 つのクライアント、資格情報ステートメント、Azure 例外ステートメント) から始めます。
from azure.purview.scanning import PurviewScanningClient from azure.purview.catalog import PurviewCatalogClient from azure.purview.administration.account import PurviewAccountClient from azure.identity import ClientSecretCredential from azure.core.exceptions import HttpResponseErrorコードで次の情報を指定します。
- クライアント ID (またはアプリケーション ID)
- テナント ID (またはディレクトリ ID)
- クライアント シークレット
client_id = "<your client id>" client_secret = "<your client secret>" tenant_id = "<your tenant id>"エンドポイントを指定します。
重要
エンドポイントの値は、使用している Microsoft Purview ポータルによって異なります。 従来の Microsoft Purview ガバナンス ポータルのエンドポイント:
https://{your_purview_account_name}.purview.azure.com/新しい Microsoft Purview ポータルのエンドポイント:https://api.purview-service.microsoft.com従来の Microsoft Purview ガバナンス ポータルのスキャン エンドポイント: 新しい Microsoft Purview ポータルの
https://{your_purview_account_name}.scan.purview.azure.com/エンドポイント:https://api.scan.purview-service.microsoft.compurview_endpoint = "<endpoint>" purview_scan_endpoint = "<scan endpoint>"これにより、次の 3 つのクライアントをインスタンス化できます。
def get_credentials(): credentials = ClientSecretCredential(client_id=client_id, client_secret=client_secret, tenant_id=tenant_id) return credentials def get_purview_client(): credentials = get_credentials() client = PurviewScanningClient(endpoint=purview_scan_endpoint, credential=credentials, logging_enable=True) return client def get_catalog_client(): credentials = get_credentials() client = PurviewCatalogClient(endpoint=purview_endpoint, credential=credentials, logging_enable=True) return client def get_admin_client(): credentials = get_credentials() client = PurviewAccountClient(endpoint=purview_endpoint, credential=credentials, logging_enable=True) return client
スクリプトの多くは、これらのクライアントがアカウントを操作する必要があるため、これらの同じ手順から始めます。
データ ソースの登録
このセクションでは、Blob Storage を登録します。
前のセクションで説明したように、まず、Microsoft Purview アカウントにアクセスするために必要なクライアントをインポートします。 また、トラブルシューティングのために Azure エラー応答パッケージをインポートし、ClientSecretCredential をインポートして Azure 資格情報を構築します。
from azure.purview.administration.account import PurviewAccountClient from azure.purview.scanning import PurviewScanningClient from azure.core.exceptions import HttpResponseError from azure.identity import ClientSecretCredential次のガイドに従って、ストレージ アカウントのリソース ID を収集します : ストレージ アカウントのリソース ID を取得します。
次に、Python ファイルで、プログラムで BLOB ストレージを登録できるように次の情報を定義します。
storage_name = "<name of your Storage Account>" storage_id = "<id of your Storage Account>" rg_name = "<name of your resource group>" rg_location = "<location of your resource group>" reference_name_purview = "<name of your Microsoft Purview account>"BLOB ストレージを登録するコレクションの名前を指定します。 (前にアクセス許可を適用したのと同じコレクションである必要があります。インストールされていない場合は、最初にこのコレクションにアクセス許可を適用します。)ルート コレクションの場合は、Microsoft Purview インスタンスと同じ名前を使用します。
collection_name = "<name of your collection>"Microsoft Purview アカウントにアクセスするための資格情報を作成する関数を作成します。
client_id = "<your client id>" client_secret = "<your client secret>" tenant_id = "<your tenant id>" def get_credentials(): credentials = ClientSecretCredential(client_id=client_id, client_secret=client_secret, tenant_id=tenant_id) return credentialsMicrosoft Purview データ マップ内のすべてのコレクションには、 フレンドリ名 と 名前があります。
- フ レンドリ名 は、コレクションに表示される名前です。 例: Sales。
- すべてのコレクション (ルート コレクションを除く ) の名前は 、データ マップによって割り当てられる 6 文字の名前です。
Python では、サブコレクションを参照するためにこの 6 文字の名前が必要です。 フレンドリ名をスクリプトに必要な 6 文字のコレクション名に自動的に変換するには、次のコード ブロックを追加します。
重要
エンドポイントの値は、使用している Microsoft Purview ポータルによって異なります。 従来の Microsoft Purview ガバナンス ポータルのエンドポイント: purview.azure.com/ 新しい Microsoft Purview ポータルのエンドポイント: purview.microsoft.com/
そのため、新しいポータルを使用している場合、エンドポイントの値は "https://consotopurview.scan.purview.microsoft.com"
def get_admin_client(): credentials = get_credentials() client = PurviewAccountClient(endpoint=purview_endpoint, credential=credentials, logging_enable=True) return client try: admin_client = get_admin_client() except ValueError as e: print(e) collection_list = client.collections.list_collections() for collection in collection_list: if collection["friendlyName"].lower() == collection_name.lower(): collection_name = collection["name"]両方のクライアントの場合、また操作によっては、入力本文も指定する必要があります。 ソースを登録するには、データ ソース登録用の入力本文を指定する必要があります。
ds_name = "<friendly name for your data source>" body_input = { "kind": "AzureStorage", "properties": { "endpoint": f"https://{storage_name}.blob.core.windows.net/", "resourceGroup": rg_name, "location": rg_location, "resourceName": storage_name, "resourceId": storage_id, "collection": { "type": "CollectionReference", "referenceName": collection_name }, "dataUseGovernance": "Disabled" } }これで、Microsoft Purview クライアントを呼び出して、データ ソースを登録できます。
重要
エンドポイントの値は、使用している Microsoft Purview ポータルによって異なります。 従来の Microsoft Purview ガバナンス ポータルのエンドポイント:
https://{your_purview_account_name}.purview.azure.com/新しい Microsoft Purview ポータルのエンドポイント:https://api.purview-service.microsoft.comクラシック ポータルを使用している場合、エンドポイントの値は次のようになります:
https://{your_purview_account_name}.scan.purview.azure.com新しいポータルを使用している場合、エンドポイントの値は次のようになります:https://scan.api.purview-service.microsoft.comdef get_purview_client(): credentials = get_credentials() client = PurviewScanningClient(endpoint={{ENDPOINT}}, credential=credentials, logging_enable=True) return client try: client = get_purview_client() except ValueError as e: print(e) try: response = client.data_sources.create_or_update(ds_name, body=body_input) print(response) print(f"Data source {ds_name} successfully created or updated") except HttpResponseError as e: print(e)
登録プロセスが成功すると、クライアントからのエンリッチされた本文の応答が表示されます。
次のセクションでは、登録したデータ ソースをスキャンし、カタログを検索します。 これらの各スクリプトは、この登録スクリプトと同じように構成されます。
完全なコード
from azure.purview.scanning import PurviewScanningClient
from azure.identity import ClientSecretCredential
from azure.core.exceptions import HttpResponseError
from azure.purview.administration.account import PurviewAccountClient
client_id = "<your client id>"
client_secret = "<your client secret>"
tenant_id = "<your tenant id>"
purview_endpoint = "<endpoint>"
purview_scan_endpoint = "<scan endpoint>"
storage_name = "<name of your Storage Account>"
storage_id = "<id of your Storage Account>"
rg_name = "<name of your resource group>"
rg_location = "<location of your resource group>"
collection_name = "<name of your collection>"
ds_name = "<friendly data source name>"
def get_credentials():
credentials = ClientSecretCredential(client_id=client_id, client_secret=client_secret, tenant_id=tenant_id)
return credentials
def get_purview_client():
credentials = get_credentials()
client = PurviewScanningClient(endpoint=purview_scan_endpoint, credential=credentials, logging_enable=True)
return client
def get_admin_client():
credentials = get_credentials()
client = PurviewAccountClient(endpoint=purview_endpoint, credential=credentials, logging_enable=True)
return client
try:
admin_client = get_admin_client()
except ValueError as e:
print(e)
collection_list = admin_client.collections.list_collections()
for collection in collection_list:
if collection["friendlyName"].lower() == collection_name.lower():
collection_name = collection["name"]
body_input = {
"kind": "AzureStorage",
"properties": {
"endpoint": f"https://{storage_name}.blob.core.windows.net/",
"resourceGroup": rg_name,
"location": rg_location,
"resourceName": storage_name,
"resourceId": storage_id,
"collection": {
"type": "CollectionReference",
"referenceName": collection_name
},
"dataUseGovernance": "Disabled"
}
}
try:
client = get_purview_client()
except ValueError as e:
print(e)
try:
response = client.data_sources.create_or_update(ds_name, body=body_input)
print(response)
print(f"Data source {ds_name} successfully created or updated")
except HttpResponseError as e:
print(e)
データ ソースをスキャンする
データ ソースのスキャンは、次の 2 つの手順で実行できます。
- スキャン定義を作成する
- スキャンの実行のトリガー
このチュートリアルでは、BLOB ストレージ コンテナーの既定のスキャン ルールを使用します。 ただし、 Microsoft Purview スキャン クライアントを使用して、プログラムでカスタム スキャン ルールを作成することもできます。
次に、上記で登録したデータソースをスキャンしてみましょう。
import ステートメントを追加して 一意の識別子を生成し、Microsoft Purview スキャン クライアント、Microsoft Purview 管理クライアント、Azure エラー応答パッケージを呼び出してトラブルシューティングを行い、クライアント シークレット資格情報を呼び出して Azure 資格情報を収集します。
import uuid from azure.purview.scanning import PurviewScanningClient from azure.purview.administration.account import PurviewAccountClient from azure.core.exceptions import HttpResponseError from azure.identity import ClientSecretCredential資格情報を使用してスキャン クライアントを作成します。
client_id = "<your client id>" client_secret = "<your client secret>" tenant_id = "<your tenant id>" def get_credentials(): credentials = ClientSecretCredential(client_id=client_id, client_secret=client_secret, tenant_id=tenant_id) return credentials def get_purview_client(): credentials = get_credentials() client = PurviewScanningClient(endpoint=f"https://{reference_name_purview}.scan.purview.azure.com", credential=credentials, logging_enable=True) return client try: client = get_purview_client() except ValueError as e: print(e)コレクションの内部名を収集するコードを追加します。 (詳細については、前のセクションを参照してください):
collection_name = "<name of the collection where you will be creating the scan>" def get_admin_client(): credentials = get_credentials() client = PurviewAccountClient(endpoint=purview_endpoint, credential=credentials, logging_enable=True) return client try: admin_client = get_admin_client() except ValueError as e: print(e) collection_list = client.collections.list_collections() for collection in collection_list: if collection["friendlyName"].lower() == collection_name.lower(): collection_name = collection["name"]次に、スキャン定義を作成します。
ds_name = "<name of your registered data source>" scan_name = "<name of the scan you want to define>" reference_name_purview = "<name of your Microsoft Purview account>" body_input = { "kind":"AzureStorageMsi", "properties": { "scanRulesetName": "AzureStorage", "scanRulesetType": "System", #We use the default scan rule set "collection": { "referenceName": collection_name, "type": "CollectionReference" } } } try: response = client.scans.create_or_update(data_source_name=ds_name, scan_name=scan_name, body=body_input) print(response) print(f"Scan {scan_name} successfully created or updated") except HttpResponseError as e: print(e)これでスキャンが定義されたので、一意の ID を使用してスキャンの実行をトリガーできます。
run_id = uuid.uuid4() #unique id of the new scan try: response = client.scan_result.run_scan(data_source_name=ds_name, scan_name=scan_name, run_id=run_id) print(response) print(f"Scan {scan_name} successfully started") except HttpResponseError as e: print(e)
完全なコード
import uuid
from azure.purview.scanning import PurviewScanningClient
from azure.purview.administration.account import PurviewAccountClient
from azure.identity import ClientSecretCredential
ds_name = "<name of your registered data source>"
scan_name = "<name of the scan you want to define>"
reference_name_purview = "<name of your Microsoft Purview account>"
client_id = "<your client id>"
client_secret = "<your client secret>"
tenant_id = "<your tenant id>"
collection_name = "<name of the collection where you will be creating the scan>"
def get_credentials():
credentials = ClientSecretCredential(client_id=client_id, client_secret=client_secret, tenant_id=tenant_id)
return credentials
def get_purview_client():
credentials = get_credentials()
client = PurviewScanningClient(endpoint=purview_scan_endpoint, credential=credentials, logging_enable=True)
return client
def get_admin_client():
credentials = get_credentials()
client = PurviewAccountClient(endpoint=purview_endpoint, credential=credentials, logging_enable=True)
return client
try:
admin_client = get_admin_client()
except ValueError as e:
print(e)
collection_list = admin_client.collections.list_collections()
for collection in collection_list:
if collection["friendlyName"].lower() == collection_name.lower():
collection_name = collection["name"]
try:
client = get_purview_client()
except AzureError as e:
print(e)
body_input = {
"kind":"AzureStorageMsi",
"properties": {
"scanRulesetName": "AzureStorage",
"scanRulesetType": "System",
"collection": {
"type": "CollectionReference",
"referenceName": collection_name
}
}
}
try:
response = client.scans.create_or_update(data_source_name=ds_name, scan_name=scan_name, body=body_input)
print(response)
print(f"Scan {scan_name} successfully created or updated")
except HttpResponseError as e:
print(e)
run_id = uuid.uuid4() #unique id of the new scan
try:
response = client.scan_result.run_scan(data_source_name=ds_name, scan_name=scan_name, run_id=run_id)
print(response)
print(f"Scan {scan_name} successfully started")
except HttpResponseError as e:
print(e)
カタログの検索
スキャンが完了すると、資産が検出され、分類された可能性があります。 このプロセスはスキャン後に完了するまでに時間がかかる場合があるため、コードの次の部分が実行されるまで待機する必要がある場合があります。 スキャンが 完了し、アセットが Microsoft Purview 統合カタログに表示されるまで待ちます。
アセットの準備ができたら、Microsoft Purview カタログ クライアントを使用してカタログ全体を検索できます。
今回は、スキャンの代わりに カタログ クライアントをインポートする必要があります。 また、HTTPResponse エラーと ClientSecretCredential も含めます。
from azure.purview.catalog import PurviewCatalogClient from azure.identity import ClientSecretCredential from azure.core.exceptions import HttpResponseErrorMicrosoft Purview アカウントにアクセスするための資格情報を取得する関数を作成し、カタログ クライアントをインスタンス化します。
client_id = "<your client id>" client_secret = "<your client secret>" tenant_id = "<your tenant id>" reference_name_purview = "<name of your Microsoft Purview account>" def get_credentials(): credentials = ClientSecretCredential(client_id=client_id, client_secret=client_secret, tenant_id=tenant_id) return credentials def get_catalog_client(): credentials = get_credentials() client = PurviewCatalogClient(endpoint=f"https://{reference_name_purview}.scan.purview.azure.com", credential=credentials, logging_enable=True) return client try: client_catalog = get_catalog_client() except ValueError as e: print(e)入力本文で検索条件とキーワードを構成します。
keywords = "keywords you want to search" body_input={ "keywords": keywords }ここではキーワードのみを指定しますが、 クエリをさらに指定するために他の多くのフィールドを追加できることに注意してください。
カタログを検索します:
try: response = client_catalog.discovery.query(search_request=body_input) print(response) except HttpResponseError as e: print(e)
完全なコード
from azure.purview.catalog import PurviewCatalogClient
from azure.identity import ClientSecretCredential
from azure.core.exceptions import HttpResponseError
client_id = "<your client id>"
client_secret = "<your client secret>"
tenant_id = "<your tenant id>"
reference_name_purview = "<name of your Microsoft Purview account>"
keywords = "<keywords you want to search for>"
def get_credentials():
credentials = ClientSecretCredential(client_id=client_id, client_secret=client_secret, tenant_id=tenant_id)
return credentials
def get_catalog_client():
credentials = get_credentials()
client = PurviewCatalogClient(endpoint=purview_endpoint, credential=credentials, logging_enable=True)
return client
body_input={
"keywords": keywords
}
try:
catalog_client = get_catalog_client()
except ValueError as e:
print(e)
try:
response = catalog_client.discovery.query(search_request=body_input)
print(response)
except HttpResponseError as e:
print(e)
データ ソースを削除する
このセクションでは、以前に登録したデータ ソースを削除する方法を学習します。 この操作は非常に単純で、スキャン クライアントを使用して行われます。
スキャン クライアントをインポートします。 また、HTTPResponse エラーと ClientSecretCredential も含めます。
from azure.purview.scanning import PurviewScanningClient from azure.identity import ClientSecretCredential from azure.core.exceptions import HttpResponseErrorMicrosoft Purview アカウントにアクセスするための資格情報を取得する関数を作成し、スキャン クライアントをインスタンス化します。
client_id = "<your client id>" client_secret = "<your client secret>" tenant_id = "<your tenant id>" reference_name_purview = "<name of your Microsoft Purview account>" def get_credentials(): credentials = ClientSecretCredential(client_id=client_id, client_secret=client_secret, tenant_id=tenant_id) return credentials def get_scanning_client(): credentials = get_credentials() PurviewScanningClient(endpoint=f"https://{reference_name_purview}.scan.purview.azure.com", credential=credentials, logging_enable=True) return client try: client_scanning = get_scanning_client() except ValueError as e: print(e)データ ソースを削除します。
ds_name = "<name of the registered data source you want to delete>" try: response = client_scanning.data_sources.delete(ds_name) print(response) print(f"Data source {ds_name} successfully deleted") except HttpResponseError as e: print(e)
完全なコード
from azure.purview.scanning import PurviewScanningClient
from azure.identity import ClientSecretCredential
from azure.core.exceptions import HttpResponseError
client_id = "<your client id>"
client_secret = "<your client secret>"
tenant_id = "<your tenant id>"
reference_name_purview = "<name of your Microsoft Purview account>"
ds_name = "<name of the registered data source you want to delete>"
def get_credentials():
credentials = ClientSecretCredential(client_id=client_id, client_secret=client_secret, tenant_id=tenant_id)
return credentials
def get_scanning_client():
credentials = get_credentials()
client = PurviewScanningClient(endpoint=f"https://{reference_name_purview}.scan.purview.azure.com", credential=credentials, logging_enable=True)
return client
try:
client_scanning = get_scanning_client()
except ValueError as e:
print(e)
try:
response = client_scanning.data_sources.delete(ds_name)
print(response)
print(f"Data source {ds_name} successfully deleted")
except HttpResponseError as e:
print(e)