问题描述: 当使用AWS DynamoDB DocumentClient的get方法时,始终返回null。
解决方法:
以下是一个示例,演示如何使用get方法查询DynamoDB表:
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB.DocumentClient();
const params = {
TableName: 'yourTableName',
Key: {
primaryKey: 'yourPrimaryKey'
}
};
dynamodb.get(params, (err, data) => {
if (err) {
console.error(err);
} else {
console.log(data);
}
});
请确保您将yourTableName
替换为实际的DynamoDB表名,将yourPrimaryKey
替换为要查询的主键值。
您可以通过设置AWS_ACCESS_KEY_ID
、AWS_SECRET_ACCESS_KEY
和AWS_REGION
环境变量,或使用AWS SDK的config
方法来配置凭证和区域。以下是一个示例:
AWS.config.update({
accessKeyId: 'yourAccessKeyId',
secretAccessKey: 'yourSecretAccessKey',
region: 'yourRegion'
});
请确保将yourAccessKeyId
、yourSecretAccessKey
和yourRegion
替换为实际的凭证和区域值。
例如,以下是一个示例策略,允许角色执行DynamoDB的读操作:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"dynamodb:BatchGetItem",
"dynamodb:GetItem",
"dynamodb:Query",
"dynamodb:Scan"
],
"Resource": "arn:aws:dynamodb:yourRegion:yourAccountId:table/yourTableName"
}
]
}
请确保将yourRegion
、yourAccountId
和yourTableName
替换为实际的值。
通过检查上述几个方面,您应该能够解决AWS DynamoDB DocumentClient的get方法始终返回null的问题。