Amazon Web Service - 从前端客户端与 RDS 数据库通信

Amazon Web Service - Communicating with RDS database from front-end client

为简单起见,我正在使用 Amazon S3 服务托管一个单页静态网站,但我想包含一个允许用户输入电子邮件地址并提交的字段。然后应该将此电子邮件添加到我的数据库中。我正在使用来自 Amazon RDS 服务的 MySQL 数据库。另外,由于这是一个静态网页,我没有后端 运行.

我想知道执行此写入数据库的架构最佳方法是什么?我应该在前端添加 javascript 以将电子邮件写入数据库吗?这会将数据库访问密钥暴露给 public 吗?我应该改用 Amazon Lambda 来写入数据库吗?

据我所知,javascript 无法处理您对服务器端的请求。

您将需要服务器端组件来处理请求。

TLDR; 这是可能的,Amazon 在客户端上为 运行 提供了一个 Javascript SDK 以连接到后端服务。 虽然有可能,但我仍然建议使用 lambda 来完成这项工作

亚马逊在浏览器中为 Javascript 提供 SDK(doc) it has some good examples 例如与 DynamoDB

一起工作

The following example puts an item in a DynamoDB table and then reads it back using the hash key.

var table = new AWS.DynamoDB({params: {TableName: 'MY_TABLE'}});
var key = 'UNIQUE_KEY_ID';

// Write the item to the table
var itemParams = {Item: {id: {S: key}, data: {S: 'data'}}};
table.putItem(itemParams, function() {
  // Read the item from the table
  table.getItem({Key: {id: {S: key}}}, function(err, data) {
    console.log(data.Item); // print the item data
  });
});

您可以从任何站点(甚至是 S3 上的静态站点 运行ning)使用那些 Javascript 方法,您只需要将库添加到您的页面

<script src="https://sdk.amazonaws.com/js/aws-sdk-2.2.28.min.js"></script>