

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 将 Amazon S3 访问权限授予与 Studio JupyterLab 笔记本连接起来
<a name="s3-access-grants-setup"></a>

使用以下信息在 Studio JupyterLab 笔记本中授予 Amazon S3 访问权限。

设置 Amazon S3 访问权限管控后，向域或用户[执行角色](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-roles.html#sagemaker-roles-get-execution-role)[添加以下权限](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html)。
+ `us-east-1` 是您的 AWS 区域
+ `111122223333` 是您的 AWS 账户 ID
+ `S3-ACCESS-GRANT-ROLE` 是您的 Amazon S3 访问权限管控角色

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowDataAccessAPI",
            "Effect": "Allow",
            "Action": [
                "s3:GetDataAccess"
            ],
            "Resource": [
                "arn:aws:s3:us-east-1:111122223333:access-grants/default"
            ]
        },
        {
            "Sid": "RequiredForTIP",
            "Effect": "Allow",
            "Action": "sts:SetContext",
            "Resource": "arn:aws:iam::111122223333:role/S3-ACCESS-GRANT-ROLE"
        }
    ]
}
```

------

确保您的 Amazon S3 访问权限管控角色的信任策略允许 `sts:SetContext` 和 `sts:AssumeRole` 操作。以下是在[更新角色信任策略](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_update-role-trust-policy.html)时使用的策略的示例。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": [
                    "access-grants.s3.amazonaws.com"
                ]
            },
            "Action": [
                "sts:AssumeRole",
                "sts:SetContext"
            ],
            "Condition": {
                "StringEquals": {
                    "aws:SourceAccount": "111122223333",
                    "aws:SourceArn": "arn:aws:s3:us-east-1:111122223333:access-grants/default"
                }
            }
        }
    ]
}
```

------

## 使用 Amazon S3 访问权限管控调用 Amazon S3
<a name="s3-access-grants-python-example"></a>

以下是 Python 脚本示例，说明了如何使用 Amazon S3 访问权限管控调用 Amazon S3。这假设您已经成功使用 SageMaker AI 设置了可信身份传播。

```
import boto3
from botocore.config import Config

def get_access_grant_credentials(account_id: str, target: str, 
                                 permission: str = 'READ'):
    s3control = boto3.client('s3control')
    response = s3control.get_data_access(
        AccountId=account_id,
        Target=target,
        Permission=permission
    )
    return response['Credentials']

def create_s3_client_from_credentials(credentials) -> boto3.client:
    return boto3.client(
        's3',
        aws_access_key_id=credentials['AccessKeyId'],
        aws_secret_access_key=credentials['SecretAccessKey'],
        aws_session_token=credentials['SessionToken']
    )

# Create client
credentials = get_access_grant_credentials('111122223333',
                                        "s3://tip-enabled-bucket/tip-enabled-path/")
s3 = create_s3_client_from_credentials(credentials)

s3.list_objects(Bucket="tip-enabled-bucket", Prefix="tip-enabled-path/")
```

如果使用未启用 Amazon S3 访问权限管控的 Amazon S3 存储桶的路径，则调用将失败。

有关其他编程语言，请参阅[使用 Amazon S3 访问权限管控管理访问权限](https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-grants.html)以了解更多信息。