你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用异步去识别化服务对多个文档进行去识别化操作

Azure Health Data Services 去识别化服务可以通过异步作业对 Azure 存储中的文档进行去标识化处理。 如果有许多文档需要去标识化,使用作业是一个不错的选择。 作业还提供一致替代,这意味着在去标识化输出中的替代值将在所有文档中匹配。 有关去识别化的详细信息(包括一致替代),请参阅什么是去识别化服务?

当你选择在 Azure Blob 存储中存储文档时,我们会根据 Azure 存储定价向你收费。 去识别化服务定价中不包括此成本。 浏览 Azure Blob 存储定价

在本教程中,你将:

  • 创建存储帐户和容器
  • 上传示例文档
  • 授予去识别化服务访问权限
  • 配置网络隔离

先决条件

打开 Azure CLI

安装 Azure CLI 并打开所选终端。 在本教程中,我们将使用 PowerShell。

创建存储帐户和容器

  1. 设置上下文,将包含去识别化服务的订阅名称替换为 <subscription_name> 占位符:
    az account set --subscription "<subscription_name>"
    
  2. 保存资源组的变量,替换包含 <resource_group> 占位符的去识别化服务的资源组:
    $ResourceGroup = "<resource_group>"
    
  3. 创建存储帐户,并为 <storage_account_name> 占位符提供值:
    $StorageAccountName = "<storage_account_name>"
    $StorageAccountId = $(az storage account create --name $StorageAccountName --resource-group $ResourceGroup --sku Standard_LRS --kind StorageV2 --min-tls-version TLS1_2 --allow-blob-public-access false --query id --output tsv)
    
  4. 为自己分配一个角色,以对存储帐户执行数据操作:
    $UserId = $(az ad signed-in-user show --query id -o tsv)
    az role assignment create --role "Storage Blob Data Contributor" --assignee $UserId --scope $StorageAccountId
    
  5. 创建用于保存示例文档的容器:
    az storage container create --account-name $StorageAccountName --name deidtest --auth-mode login
    

上传示例文档

接下来,上传包含合成受保护健康信息 (PHI) 的文档:

$DocumentContent = "The patient came in for a visit on 10/12/2023 and was seen again November 4th at Contoso Hospital."
az storage blob upload --data $DocumentContent --account-name $StorageAccountName --container-name deidtest --name deidsample.txt --auth-mode login

授予去识别化服务对存储帐户的访问权限

在此步骤中,将授予去识别化服务系统分配的托管标识对容器的基于角色的访问权限。 授予存储 Blob 数据参与者角色权限,因为去识别化服务将同时读取原始文档和写入已去识别化的输出文档。 将去识别化服务的名称替换为 <deid_service_name> 占位符:

$DeidServicePrincipalId=$(az resource show -n <deid_service_name> -g $ResourceGroup --resource-type microsoft.healthdataaiservices/deidservices --query identity.principalId --output tsv)
az role assignment create --assignee $DeidServicePrincipalId --role "Storage Blob Data Contributor" --scope $StorageAccountId

若要验证取消标识服务是否有权访问存储帐户,可以在 存储帐户下的 Azure 门户中进行检查。 在“存储中心和资源”选项卡下,单击存储帐户名称。 选择 访问控制(IAM) 并在搜索栏中搜索取消标识服务的名称($ResourceGroup)。

在存储帐户上配置网络隔离

接下来,更新存储帐户以禁用公共网络访问,并且仅允许从受信任的 Azure 服务(例如去识别化服务)进行访问。 运行此命令后,你将无法查看存储容器的内容,除非设置网络例外。 若要了解详细信息,请参阅配置 Azure 存储防火墙和虚拟网络

az storage account update --name $StorageAccountName --public-network-access Disabled --bypass AzureServices

使用 python SDK

下面的代码包含 用于 Python 的 Azure Health Deidentification SDK 中的示例。


"""
FILE: deidentify_documents_async.py

DESCRIPTION:
    This sample demonstrates a basic scenario of de-identifying documents in Azure Storage. 
    Taking a container URI and an input prefix, the sample will create a job and wait for the job to complete.

USAGE:
    python deidentify_documents_async.py

    Set the environment variables with your own values before running the sample:
    1) endpoint - the service URL endpoint for a de-identification service.
    2) storage_location - an Azure Storage container endpoint, like "https://<storageaccount>.blob.core.windows.net/<container>".
    3) INPUT_PREFIX - the prefix of the input document name(s) in the container.
        For example, providing "folder1" would create a job that would process documents like "https://<storageaccount>.blob.core.windows.net/<container>/folder1/document1.txt".
"""


import asyncio
from azure.core.polling import AsyncLROPoller
from azure.health.deidentification.aio import DeidentificationClient
from azure.health.deidentification.models import (
    DeidentificationJob,
    SourceStorageLocation,
    TargetStorageLocation,
)
from azure.identity.aio import DefaultAzureCredential
import os
import uuid


async def deidentify_documents_async():
    endpoint = "<YOUR SERVICE URL HERE>" ### Replace 
    storage_location = "https://<CONTAINER NAME>.blob.core.windows.net/deidtest/" ### Replace <CONTAINER NAME>
    inputPrefix = "deidsample" 
    outputPrefix = "_output"

    credential = DefaultAzureCredential()
    client = DeidentificationClient(endpoint, credential)

    jobname = f"sample-job-{uuid.uuid4().hex[:8]}"

    job = DeidentificationJob(
        source_location=SourceStorageLocation(
            location=storage_location,
            prefix=inputPrefix,
        ),
        target_location=TargetStorageLocation(location=storage_location, prefix=outputPrefix, overwrite=True),
    )

    async with client:
        lro: AsyncLROPoller = await client.begin_deidentify_documents(jobname, job)
        finished_job: DeidentificationJob = await lro.result()

        await credential.close()

        print(f"Job Name:   {finished_job.job_name}")
        print(f"Job Status: {finished_job.status}")  # Succeeded
        print(f"File Count: {finished_job.summary.total_count if finished_job.summary is not None else 0}")


async def main():
    await deidentify_documents_async()


if __name__ == "__main__":
    asyncio.run(main())


清理资源

完成存储帐户的操作后,可以删除存储帐户和角色分配:

az role assignment delete --assignee $DeidServicePrincipalId --role "Storage Blob Data Contributor" --scope $StorageAccountId
az role assignment delete --assignee $UserId --role "Storage Blob Data Contributor" --scope $StorageAccountId
az storage account delete --ids $StorageAccountId --yes

后续步骤