- 最新
- 最多得票
- 最多評論
It looks like there might be an issue with how you're providing the authentication token (authToken) to the fromCognitoIdentityPool function in the AWS SDK. The logins parameter expects an object where the key is the identity provider name, and the value is the authentication token. Here's a revised version of your code:
import { DynamoDBClient, PutCommand } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';
import { fromCognitoIdentityPool } from '@aws-sdk/credential-provider-cognito-identity';
const main = async (oN: string) => {
const { accessToken, idToken } = (await fetchAuthSession()).tokens ?? {};
const REGION = 'ap-south-1';
const COGNITO_IDENTITY_POOL_ID = 'your-identity-pool-id'; // Replace with your actual Identity Pool ID
const logins = {
[`cognito-idp.${REGION}.amazonaws.com/${COGNITO_IDENTITY_POOL_ID}`]: idToken,
};
const client = new DynamoDBClient({
region: REGION,
credentials: fromCognitoIdentityPool({
clientConfig: { region: REGION },
identityPoolId: COGNITO_IDENTITY_POOL_ID,
logins,
}),
});
const docClient = DynamoDBDocumentClient.from(client);
const command = new PutCommand({
TableName: 'your-dynamodb-table-name', // Replace with your actual table name
Item: {
pk: 'LK',
sk: 'test-sk',
oN: oN,
},
});
try {
const response = await docClient.send(command);
console.log(response);
return response;
} catch (error) {
console.error('Error executing DynamoDB PutCommand:', error);
throw error;
}
};
const fetchAuthSession = async () => {
// Implement your logic to fetch authentication tokens (idToken, accessToken) here
// This might involve using Amazon Cognito's authentication flow or any other authentication mechanism you're using.
};
main('example-data');
Ensure you replace placeholders like 'your-identity-pool-id' and 'your-dynamodb-table-name' with your actual Identity Pool ID and DynamoDB table name.
Additionally, make sure your fetchAuthSession function returns the correct authentication tokens. If you're using Cognito, it might involve calling CognitoUser.getSession() or a similar method.
If the issue persists, consider logging the actual values of idToken and authToken to the console to ensure they are being retrieved correctly before the DynamoDB operation.
its solved after passing the idToken
within backtick
const logins = {
[`cognito-idp.${REGION}.amazonaws.com/${COGNITO_IDENTITY_POOL_ID}`]: `${idToken}`,
};
相關內容
- 已提問 6 個月前
- AWS 官方已更新 2 年前
- AWS 官方已更新 1 年前
- AWS 官方已更新 1 年前
const logins = { [
cognito-idp.${REGION}.amazonaws.com/${COGNITO_IDENTITY_POOL_ID}
]:${idToken}
, };