javascript – Amazon DynamoDB和AngularJS

前端之家收集整理的这篇文章主要介绍了javascript – Amazon DynamoDB和AngularJS前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以,我创建了一个AWS dynamoDB表(数据库),我准备用AngularJS获取该数据.我如何使用AngularJS执行此操作?我是否需要在亚马逊上设置其他服务?或者我可以直接访问我的数据库吗?

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

解决方法

虽然Mars JSON演示非常出色,但这是一个非常简单的入门示例,它使用AWS SDK for JavaScript v2.1.33.为自己切换钥匙.这只是一个演示,不要硬编码密钥,可以使用AWS Cognito代替.查看几个AWS陷阱的屏幕截图.

https://github.com/mayosmith/HelloDynamoDB

  1. /*
  2. -----------------------------------------------------------------
  3. AWS configure
  4. Note: this is a simple experiement for demonstration
  5. purposes only. Replace the keys below with your own.
  6. Do not include the secret key in an actual production
  7. environment,because,then,it wont be secret anymore...
  8. -----------------------------------------------------------------
  9. */
  10. AWS.config.update({accessKeyId: 'AKIAJUPWRIYYQGDB6AFA',secretAccessKey: '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'});
  11. // Configure the region
  12. AWS.config.region = 'us-west-2'; //us-west-2 is Oregon
  13. //create the ddb object
  14. var ddb = new AWS.DynamoDB();
  15. /*
  16. -----------------------------------------------------------------
  17. Update the Table
  18. -----------------------------------------------------------------
  19. */
  20. //update the table with this data
  21. var params = {
  22. Key: {
  23. name: {S: 'John Mayo-Smith'},city: {S: 'New York'}
  24. },AttributeUpdates: {
  25. food: {
  26. Action: 'PUT',Value: {S: 'chocolate'}
  27. }
  28. },TableName: 'sampletable',ReturnValues: 'ALL_NEW'
  29. };
  30. //update the table
  31. update();
  32. /*
  33. -----------------------------------------------------------------
  34. Get Item from the Table
  35. -----------------------------------------------------------------
  36. */
  37. //attribute to read
  38. var readparams = {
  39.  
  40. Key: {
  41. name: {S: 'John Mayo-Smith'},AttributesToGet: ['food'],TableName: 'sampletable'
  42. };
  43. //get the item
  44. read();
  45. /*
  46. -----------------------------------------------------------------
  47. function update()
  48. Description: Calls updateItem which is part of the AWS Javascript
  49. SDK.
  50. Returns: JSON object (the object is stringifyed so we can see
  51. what's going on in the javascript console)
  52. -----------------------------------------------------------------
  53. */
  54. function update(){
  55. ddb.updateItem(params,function(err,data) {
  56. if (err) { return console.log(err); }
  57. console.log("We updated the table with this: " + JSON.stringify(data));
  58. });
  59. }
  60. /*
  61. -----------------------------------------------------------------
  62. function read()
  63. Description: Calls getItem which is part of the AWS Javascript
  64. SDK.
  65. Returns: JSON object (the object is stringifyed so we can see
  66. what's going on in the javascript console)
  67. -----------------------------------------------------------------
  68. */
  69. function read(){
  70. ddb.getItem(readparams,data) {
  71. if (err) { return console.log(err); }
  72. console.log(": " + data);
  73.  
  74. console.log("John's favorite food is: "+ JSON.stringify(data.Item.food.S)); // print the item data
  75. });
  76. }

猜你在找的JavaScript相关文章