【问题标题】:Amazon DynamoDB and AngularJS亚马逊 DynamoDB 和 AngularJS
【发布时间】:2015-01-22 19:32:24
【问题描述】:

所以,我创建了一个 AWS dynamoDB 表(数据库),并准备好使用 AngularJS 获取该数据。我如何用 AngularJS 做到这一点?我需要在亚马逊上设置其他服务吗?或者我可以直接访问我的数据库吗?

我无法直接找到与 DynamoDB 和 AngularJS 相关的任何内容。任何帮助将不胜感激!

【问题讨论】:

  • 有一个 JS SDK (aws.amazon.com/sdk-for-browser)。在此处查看示例:ng-newsletter.com/posts/aws-js-sdk.html
  • 您是否打算从 Web 应用程序直接与 DynamoDB 对话?如果您这样做,您将向用户公开您的 AWS 凭证。
  • 谢谢塔维。我认为 AWS JS SDK 正是我想要的。
  • @MikeKobit cognito 是否允许我们安全地从网络应用程序中使用 DynamoDb ?
  • @Thomas 是的,Cognito 允许您安全地控制对 DynamoDB 的访问。我的旧评论应该更多地表述为“如果您直接提供凭据以将 DynamoDB 访问到 web 应用程序,您将公开这些凭据”。

标签: javascript database angularjs angularjs-directive amazon-dynamodb


【解决方案1】:

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

https://github.com/mayosmith/HelloDynamoDB

/*
-----------------------------------------------------------------
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
});
}

【讨论】:

  • 非常好的总结,虽然需要额外说明的是,要使用它,您必须将回调函数传递给 read() 和 update() 方法,而不是在 getItem 和 updateItem 调用中声明它。
  • 如果您链接到您自己的内容,您需要披露。 stackoverflow.com/help/promotion
【解决方案2】:

是的,您可以使用适用于浏览器的 AWS JavaScript 开发工具包直接从您的 AngularJS 应用程序访问 Amazon DynamoDB。同样的代码 sn-p 也应该适用于 NodeJS。

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

源代码: https://github.com/awslabs/aws-dynamodb-mars-json-demo

现场演示: http://dynamodb-msl-image-explorer.s3-website-us-east-1.amazonaws.com/

使用 AWS 库的示例: https://github.com/awslabs/aws-dynamodb-mars-json-demo/blob/master/viewer/app/scripts/services/mars-photos.js

使用使用 AWS 库的服务的示例: https://github.com/awslabs/aws-dynamodb-mars-json-demo/blob/master/viewer/app/scripts/controllers/favorites.js

https://github.com/awslabs/aws-dynamodb-mars-json-demo/blob/master/viewer/app/scripts/controllers/top-voted.js

文档 SDK: https://github.com/awslabs/dynamodb-document-js-sdk

【讨论】:

  • 你的例子很棒,但如果我还没有登录 facebook,facebook 投票就不起作用。它是设计使然还是只是一个错误?通常,如果您未登录该站点,则会打开登录窗口,您可以在其中输入凭据,但在您的情况下则不行 - 您只需抛出异常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-26
  • 2012-12-08
  • 1970-01-01
  • 2022-12-03
相关资源
最近更新 更多