如何在JavaScript模块中使用AWS?

时间:2019-05-10 04:18:51

标签: javascript amazon-web-services webpack

我对AWS了解很多,但对JavaScript知之甚少,对webpack / npm甚至了解甚少。我正在尝试在浏览器中运行的JavaScript Vue模块中调用DynamoDB,但我不知道如何仅引用DynamoDB库。这是我的代码:

var AWS = require('aws-sdk/clients/dynamodb'); //This doesn't work, I get an undefined error below
//var AWS = require('aws-sdk'); //This works, but I get the whole huge awsk-sdk JavaScript library

var dynamodb = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10', region: 'us-east-1', accessKeyId: "XXXXX", secretAccessKey: "YYYYYYYYYYYYYYY" });

这是错误: 无法读取未定义的属性“ DocumentClient”

如何仅对DynamoDB JavaScript库创建需求,以便我的代码具有合理的下载大小?

1 个答案:

答案 0 :(得分:2)

我发现了-我需要更改它:

var AWS = require('aws-sdk/clients/dynamodb'); //This doesn't work, I get an undefined error below
var dynamodb = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10', region: 'us-east-1', accessKeyId: "XXXXX", secretAccessKey: "YYYYYYYYYYYYYYY" });

对此:

var DynamoDB = require('aws-sdk/clients/dynamodb'); //This doesn't work, I get an undefined error below
var dynamodb = new DynamoDB.DocumentClient({apiVersion: '2012-08-10', region: 'us-east-1', accessKeyId: "XXXXX", secretAccessKey: "YYYYYYYYYYYYYYY" });

请注意,我已将名称从AWS更改为DynamoDB。