Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Amazon DynamoDB和AngularJS_Javascript_Database_Angularjs_Angularjs Directive_Amazon Dynamodb - Fatal编程技术网

Javascript Amazon DynamoDB和AngularJS

Javascript Amazon DynamoDB和AngularJS,javascript,database,angularjs,angularjs-directive,amazon-dynamodb,Javascript,Database,Angularjs,Angularjs Directive,Amazon Dynamodb,因此,我创建了一个AWS dynamoDB表(数据库),并准备使用AngularJS获取该数据。我该如何处理AngularJS?我是否需要在Amazon上安装其他服务?或者我可以直接访问我的数据库吗 我找不到任何与DynamoDB和AngularJS直接相关的东西。任何帮助都将不胜感激 是的,您可以使用浏览器的AWS JavaScript SDK直接从AngularJS应用程序访问Amazon DynamoDB。同样的代码片段也适用于NodeJ 需要注意的一点是,您的应用程序需要安全地向AWS进

因此,我创建了一个AWS dynamoDB表(数据库),并准备使用AngularJS获取该数据。我该如何处理AngularJS?我是否需要在Amazon上安装其他服务?或者我可以直接访问我的数据库吗


我找不到任何与DynamoDB和AngularJS直接相关的东西。任何帮助都将不胜感激

是的,您可以使用浏览器的AWS JavaScript SDK直接从AngularJS应用程序访问Amazon DynamoDB。同样的代码片段也适用于NodeJ

需要注意的一点是,您的应用程序需要安全地向AWS进行身份验证,而无需在代码中嵌入安全凭据。如果您已经创建了一个身份池并相应地配置了您的应用程序,那么AmazonCognito可以使身份验证变得简单和自动。 这里有一些链接可以帮助您入门。我们在Amazon DynamoDB上用AngularJS构建了一个演示应用程序。它使用amazoncognito进行身份验证,使用documentsdk直接在DynamoDB中存储和检索JSON对象,而不需要在浏览器的客户端代码中序列化和反序列化它们

源代码:

现场演示:

使用AWS库的示例:

使用AWS库的服务的示例:

文档SDK:

虽然Mars JSON演示非常出色,但这里有一个非常简单的示例,可以开始使用AWS SDK for JavaScript v2.1.33。把钥匙换成你自己的。这只是一个演示,不要硬编码密钥,可以使用AWS Cognito代替。查看几个AWS gotcha的屏幕截图


有一个jssdk()。看看这里的一个例子:您是否计划通过web应用程序直接与DynamoDB对话?如果您这样做,您将向用户公开您的AWS凭据。谢谢Tavi。我认为AWS JS SDK正是我想要的。@MikeKobit cognito允许我们安全地从web应用程序使用DynamoDb吗?@Thomas是的,cognito允许您安全地访问DynamoDb。我以前的评论应该更多地表述为“如果你直接提供凭据来访问Web应用程序的DynamoDB,你将公开这些凭据”。你的例子很好,但如果我还没有登录facebook,facebook投票就不起作用。它是设计的还是仅仅是一个bug?通常,如果您未登录该站点,则会打开“登录”窗口,在该窗口中可以输入凭据,但在您的情况下,您只需抛出一个异常。非常好的摘要,尽管需要另外说明的是,要想发挥作用,您必须将回调函数传递给read()和update()方法,而不是在getItem和updateItem调用中声明。如果您链接到自己的内容,则需要披露。
/*
-----------------------------------------------------------------
AWS configure
Note: this is a simple experiement for demonstration
purposes only. Replace the keys below with your own.
Do not include the secret key in an actual production
environment, because, then, it wont be secret anymore...
-----------------------------------------------------------------
*/
AWS.config.update({accessKeyId: 'AKIAJUPWRIYYQGDB6AFA', secretAccessKey: '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'});
// Configure the region
AWS.config.region = 'us-west-2';  //us-west-2 is Oregon
//create the ddb object
var ddb = new AWS.DynamoDB();
/*
-----------------------------------------------------------------
Update the Table
-----------------------------------------------------------------
*/
//update the table with this data
var params = {
  Key: {
    name: {S: 'John Mayo-Smith'},
    city: {S: 'New York'}
  },
  AttributeUpdates: {
    food: {
      Action: 'PUT',
      Value: {S: 'chocolate'}
    }
  },
  TableName: 'sampletable',
  ReturnValues: 'ALL_NEW'
};
//update the table
update();
/*
-----------------------------------------------------------------
Get Item from the Table
-----------------------------------------------------------------
*/
//attribute to read
var readparams = {

  Key: {
    name: {S: 'John Mayo-Smith'},
    city: {S: 'New York'}
  },
  AttributesToGet: ['food'],
  TableName: 'sampletable'
};
//get the item
read();
/*
-----------------------------------------------------------------
function update()
Description: Calls updateItem which is part of the AWS Javascript
SDK.
Returns: JSON object (the object is stringifyed so we can see 
what's going on in the javascript console)
-----------------------------------------------------------------
*/
function update(){
    ddb.updateItem(params, function(err, data) {
        if (err) { return console.log(err); }
        console.log("We updated the table with this: " + JSON.stringify(data));
    });
}
/*
-----------------------------------------------------------------
function read()
Description: Calls getItem which is part of the AWS Javascript
SDK.
Returns: JSON object (the object is stringifyed so we can see 
what's going on in the javascript console)
-----------------------------------------------------------------
*/
function read(){
    ddb.getItem(readparams, function(err, data) {
        if (err) { return console.log(err); }
        console.log(": " + data);       

    console.log("John's favorite food is: "+ JSON.stringify(data.Item.food.S)); // print the item data
});
}