简体   繁体   中英

How to get Cognito Identity ID given the Cognito User Pool ID?

In AWS Amplify's Storage Documentation , there's a section on how to get other users' objects...

Storage.get('test.txt', { 
    level: 'protected', 
    identityId: 'xxxxxxx' // the identityId of that user
})

Where can I get the identityId of another user?

I can only query for the Cognito User Pool Id of users. Is there a way to map the User Pool Id to the Identity Id?

You can get identity id using Auth.currentUserCredential Method

import { Auth } from 'aws-amplify';

await Auth.signIn(username, password);
const credentials = await Auth.currentUserCredentials();
console.log("identityId", credentials.identityId);

After much research and looking into some similar/related questions/answers, it seems that this is still missing from AWS.

The closest topic I found is from AWS Forums: https://forums.aws.amazon.com/thread.jspa?messageID=924345 which is still unanswered :-)

There is a work-around in this GitHub comment where you can use Cognito User Pool ID instead of Identity ID for the S3 folder names. This way you won't really need to deal with Identity ID.

After doing some more research we found that you can use user attributes for access control so instead of using the federated id as the users folder name, you can specify a custom attribute mapping (we mapped cognitoId to sub) using principal tags, and in your policy you can dynamically reference resources using these tags:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM