I am using aws-sdk
to connect with dynamod and i tried the following ways to connect with dynamodb.
Method 1
const aws_remote_config = {
accessKeyId: process.env.ACCESS_KEY,
secretAccessKey: process.env.SECRET_KEY,
region: process.env.REGION,
}
express.Router().get("/test",(req,res)=>{
AWS.config.update(aws_remote_config);
const docClient = new AWS.DynamoDB.DocumentClient();
const params = {
TableName: "test"
};
docClient.scan(params, function (err, data) {
if (err) {
//console.log(err)
res.send({
success: false,
message: err
});
} else {
const { Items } = data;
res.send({
success: true,
movies: Items
});
}
});
})
I am getting this response
{"success":false,"message":{"message":"Requested resource not found","code":"ResourceNotFoundException","time":"2021-01-27T07:43:55.351Z","requestId":"L2MNQCL2EKUM13QED03FMK3ADJVV4KQNSO5AEMVJF66Q9ASUAAJG","statusCode":400,"retryable":false,"retryDelay":14.298055549242983}}
Method 2
const aws_remote_config = {
accessKeyId: process.env.ACCESS_KEY,
secretAccessKey: process.env.SECRET_KEY,
region: process.env.REGION,
}
express.Router().get("/test",(req,res)=>{
AWS.config.update(aws_remote_config);
const docClient = new AWS.DynamoDB.DocumentClient();
const params = {
TableName: "test"
};
var dynamodb = new AWS.DynamoDB();
var param = {}
dynamodb.listTables(param, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log("Sussess data",data);
});
})
I got this
Sussess data { TableNames: [] }
Method 3
// i tried to connect like this
AWS.config.loadFromPath('../../config/db.json');
// path is correct, i checked by console.log(require('../../config/db.json'));
I got this
Error: ENOENT: no such file or directory, open '../../config/db.json'
at Object.openSync (fs.js:462:3)
at Object.readFileSync (fs.js:364:35)
at Object.readFileSync (C:UsersRahul kumardesktopProjectsmerchent-backend
ode_modulesaws-sdklibutil.js:95:26)
at Config.loadFromPath (C:UsersRahul kumardesktopProjectsmerchent-backend
ode_modulesaws-sdklibconfig.js:473:39)
at C:UsersRahul kumardesktopProjectsmerchent-backend
outesuserest.js:9:16
at Layer.handle [as handle_request] (C:UsersRahul kumardesktopProjectsmerchent-backend
ode_modulesexpresslib
outerlayer.js:95:5)
at next (C:UsersRahul kumardesktopProjectsmerchent-backend
ode_modulesexpresslib
outer
oute.js:137:13)
at Route.dispatch (C:UsersRahul kumardesktopProjectsmerchent-backend
ode_modulesexpresslib
outer
oute.js:112:3)
at Layer.handle [as handle_request] (C:UsersRahul kumardesktopProjectsmerchent-backend
ode_modulesexpresslib
outerlayer.js:95:5)
at C:UsersRahul kumardesktopProjectsmerchent-backend
ode_modulesexpresslib
outerindex.js:281:22
question from:
https://stackoverflow.com/questions/65914981/how-to-connect-aws-dynamodb-from-nodejs 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…