Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

node.js - How to connect aws dynamodb from nodejs

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I agree with another answer: Method #2 appears to be working. One point is that listTables does not require any arguments to succeed. Of the two arguments ...

  • Limit is optional; it defaults to 100
  • ExclusiveStartTableName is also optional. It should only be provided if you are "paging" through a long list of table names and need to get the next page.

Long story short, this ...

Sussess data { TableNames: [] }

... is showing that the account you are authenticated in does not have any DynamoDB tables. So the array of table names is empty.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...