从经过身份验证的用户获取 CognitoId

Get CognitoId from Authenticated User

我正在使用 Amplify 框架将我的想法变为现实,到目前为止一切顺利,除了我坚持的一件事。

我让经过身份验证的用户将文件上传到 S3,并且我正在使用 Protected 访问级别,根据 this,Protected 对所有用户都是可读的,我已经设法上传文件并在 Dynamo 中保存一些信息 table。

存储文档说明如下:

Protected: Readable by all users, but writable only by the creating user. Files are stored under protected/{user_identity_id}/ where the user_identity_id corresponds to the unique Amazon Cognito Identity ID for that user.

但是我无法将在受保护的 S3 中创建的 {user_identity_id} 与 Cognito 用户池中可用信息的 none 相匹配,所以,我真的找不到一种了解哪些文件属于特定用户的方法,此外,我需要一种方法让我的 Admin 用户能够看到所有用户上传的文件。

另一方面,here,文档说可以通过将 identityId 作为参数传递来检索文件,但是:

我会很感激一些指导。

嗯,我想明白你去哪儿了。我从未使用过 AWS Amplify,但基本概念就像我上面提到的生成 identityId。在图块上,它会像这样:

IdentityId 是每个联合身份角色的唯一 ID,可以验证或未验证角色。我们将调用 CognitoIdentityCredentials 将令牌与 aws temp 凭证交换为经过身份验证的角色。用户获得凭据后,他们将拥有 AWS.config.credentials 并包含 identityId。

回到你的问题,我找到了“Auth.currentUserCredentials()”方法来获取带有放大库的用户身份ID。示例:

let creds = await Auth.currentUserCredentials()
console.log(creds.identityId)

然后,根据放大文档,您可以通过示例检索您的受保护的 s3 :

Storage.list('photos/', { 
   level: 'protected', 
   identityId: creds.identityId
})
.then(result => console.log(result))
.catch(err => console.log(err));