AWS.Amplify不是构造函数

时间:2019-05-08 09:23:41

标签: amazon-web-services

我正在尝试做一个简单的lambda函数来进行基本的文本翻译,但是当我尝试将其发布到数据库中时,我得到了错误消息AWS.Amplify is not a constructor,有什么帮助吗?我正在使用aws网站对其进行编码,以便对其nodejs进行编码。

console.log('Loading function');
const AWS = require('aws-sdk');
const translate = new AWS.Translate({ apiVersion: '2017-07-01' });
const API = new AWS.Amplify();

exports.handler = async (event, context) => {
  try {
    const params = {
      SourceLanguageCode: 'en', /* required */
      TargetLanguageCode: 'es', /* required */
      Text: 'Hello World', /* required */
    };

    const data = await translate.translateText(params).promise();

    createSite(data.TranslatedText);

  } catch (err) {
    console.log(err, err.stack);
  }

  function createSite(site) {
  return API.post("sites", "/sites", {
    body: site
  });
}
};

1 个答案:

答案 0 :(得分:0)

尝试在本地创建node.js项目,然后通过webpack添加amplify.js。

package.json:

{
  "name": "amplify-js-app",
  "version": "1.0.0",
  "description": "Amplify JavaScript Example",
  "dependencies": {},
  "devDependencies": {
    "webpack": "^4.17.1",
    "webpack-cli": "^3.1.0",
    "copy-webpack-plugin": "^4.5.2",
    "webpack-dev-server": "^3.1.5"
  },
  "scripts": {
    "start": "webpack && webpack-dev-server --mode development",
    "build": "webpack"
  }
}

然后通过在节点项目的lambda的app.js /处理函数中导入amplify.js来完成现有代码,如下所示:

import Amplify, { API } from "aws-amplify";
// then write your existing code.

然后通过以下两种方式构建您的项目并将其部署到AWS lambda:

  1. 直接上传到功能
  2. 上传到S3存储桶,然后在lambda函数中提供该存储桶的引用。

参考:amplify.js docs