寻求Python实现Google Cloud DLP TransformationErrorHandling的示例
Python dlp_v2 中 TransformationErrorHandling 的正确实现
你的代码问题在于未正确给DeidentifyConfig的transformation_error_handling字段赋值,而是直接将TransformationErrorHandling实例作为参数传入。以下是两种正确的实现方式:
方式一:使用 Protobuf 类型构造
from google.cloud import dlp_v2 # 初始化DLP客户端 dlp_client = dlp_v2.DlpServiceClient() # 构造错误处理配置 error_handling = dlp_v2.types.TransformationErrorHandling( mode=dlp_v2.types.TransformationErrorHandling.Mode.LEAVE_UNTRANSFORMED ) # 构造去标识化配置 deidentify_config = dlp_v2.types.DeidentifyConfig( transformation_error_handling=error_handling ) # 构造重新标识请求 reidentify_request = dlp_v2.ReidentifyContentRequest( parent=f"projects/{project_id}/locations/northamerica-northeast1", reidentify_template_name=template_id, item=item, reidentify_config=deidentify_config ) # 发送请求 response = dlp_client.reidentify_content(request=reidentify_request)
方式二:使用字典构造(更简洁)
from google.cloud import dlp_v2 dlp_client = dlp_v2.DlpServiceClient() deidentify_config = { "transformationErrorHandling": { "mode": "LEAVE_UNTRANSFORMED" } } reidentify_request = dlp_v2.ReidentifyContentRequest( parent=f"projects/{project_id}/locations/northamerica-northeast1", reidentify_template_name=template_id, item=item, reidentify_config=deidentify_config ) response = dlp_client.reidentify_content(request=reidentify_request)
关键注意点
TransformationErrorHandling的模式通过mode字段指定,可选枚举值包括LEAVE_UNTRANSFORMED、THROW_ERROR等,可根据需求选择。- 无论用Protobuf类型还是字典,都需将错误处理配置赋值给
DeidentifyConfig的transformation_error_handling(Protobuf)或transformationErrorHandling(字典)字段。 - 确保
reidentify_config字段正确关联到请求中,与reidentify_template_name配合使用时,该配置会覆盖模板中的对应设置。
内容的提问来源于stack exchange,提问作者naveed1224
相关产品推荐
相关产品推荐

