在SageMaker Notebook实例中的代码中,使用AWS SDK为SSO提供的API来获取用户关联的标识符(identifier),并将其传递到SageMaker的CreatePresignedNotebookInstanceURL方法中。以下是示例代码:
import boto3
from botocore.exceptions import ClientError
ssoclient = boto3.client('sso')
sagemakerclient = boto3.client('sagemaker')
try:
# Get user's SSO identifier
response = ssoclient.list_account_roles()
sso_identifier = response['RoleList'][0]['SSOUserIdentifier']
# Generate pre-signed URL with SSO identifier
response = sagemakerclient.create_presigned_notebook_instance_url(
NotebookInstanceName='my-instance',
SessionExpirationDurationInSeconds=1800,
SingleSignOnIdentifierTypeString='USER_ATTRIBUTE',
SingleSignOnIdentifierValue=sso_identifier
)
url = response['AuthorizedUrl']
print(f'Presigned URL: {url}')
except ClientError as e:
print(f'Error: {e}')
上述示例代码中,我们首先创建了SSO和SageMaker的客户端。然后,我们使用SSO API中的list_account_roles方法获取用户关联的AWS SSO角色列表,从中提取出用户的SSO标识符。接下来,我们将标识符传递给SageMaker的create_presigned_notebook_instance_url方法,以生成带有用户标识符的预签名URL。最后,我们打印出预签名URL以供用户使用。