I am using AWS Amplify for a ReactJS application. The only two functional areas I am using from Amplify are Authentication and Hosting. These two are working fine, and a Cognito user pool associated with the project is working as expected, providing the logged in user to the React component after successful authentication.
My next step is to query other AWS resources outside of Amplify configuration, starting with an S3 bucket and a DynamoDB table. I am attempting to use AWS Client SDK for these, but am unable to figure out how to use the credentials of the currently logged in user.
I tried to use fromWebToken
from @aws-sdk/credentials-providers
but I do not have an Identity pool; I am using a Cognito user pool as only authenticated users have access to the web app. So I am stumped on how to proceed. My thought was that the current user credentials would automatically be used for any client request, but apparently it's not the case.
Here is the code I have; please note that this is only an attempt to show what I have tried, my question is quite simple: How do I get the currently authenticated user's credentials for use with S3 and other AWS Client SDK components?
// user is the currently logged in user. I can see the accessToken etc. here.
console.log(user.signInUserSession.accessToken);
const client = new S3Client({
region: "us-west-2", credentials: fromWebToken({
clientConfig: {region: "us-west-2"},
roleArn: "arn:aws:iam::...", // Got these from IAM for authenticated users
webIdentityToken: user.signInUserSession.accessToken.jwtToken
})
});
I try to use the client for downloading a file, but get the following error:
const command = new GetObjectCommand({
Bucket: bucket,
Key: key,
});
const response = await client.send(command);
// Throws the error: No Cognito Identity pool provided for unauthenticated access
Any help is appreciated.