Go SDK调用AWS SageMaker端点Body标记敏感,如何获取实际结果?
解决Go SDK调用AWS SageMaker无服务器端点时Body显示的问题
用Go SDK调用AWS SageMaker无服务器端点时,返回结果中的Body被标记为<sensitive>,查看SDK代码文档得知Body不会直接在结构体打印中返回,但未说明如何获取实际内容。而使用AWS CLI调用该端点时,能正常将结果JSON写入指定文件。
问题代码
svc := sagemakerruntime.New(sess) out, err := svc.InvokeEndpoint(&sagemakerruntime.InvokeEndpointInput{ Body: []byte(`xxx`), ContentType: aws.String("application/json"), EndpointName: aws.String("xxx"), })
返回结果(打印结构体时)
{ Body: <sensitive>, ContentType: "application/json", InvokedProductionVariant: "single-variant" }
可用的AWS CLI命令
aws sagemaker-runtime invoke-endpoint --content-type 'application/json' --endpoint-name xxx --body xxx /tmp/sagemaker-output
解决方案
<sensitive>只是SDK在打印结构体时的掩码显示,实际Body内容是存在的——它的类型是io.ReadCloser,需要手动读取:
import ( "io" "log" "fmt" ) // 初始化svc的代码不变 out, err := svc.InvokeEndpoint(&sagemakerruntime.InvokeEndpointInput{ Body: []byte(`xxx`), ContentType: aws.String("application/json"), EndpointName: aws.String("xxx"), }) if err != nil { log.Fatal(err) } defer out.Body.Close() // 读取Body中的实际内容 bodyBytes, err := io.ReadAll(out.Body) if err != nil { log.Fatal(err) } // 转换为字符串或解析为JSON使用 bodyContent := string(bodyBytes) fmt.Println(bodyContent)
解释:SDK为了避免在日志或打印中泄露敏感数据,会在打印InvokeEndpointOutput结构体时将Body标记为<sensitive>,但底层的数据流是完整的,通过io.ReadAll读取后就能获取到端点返回的实际内容。
内容的提问来源于stack exchange,提问作者Stefanos Chrs
相关产品推荐
相关产品推荐

