在我的自定义Ask-SDK Webhook中使用Dynamo DB客户端

时间:2019-06-09 16:00:44

标签: amazon-dynamodb alexa alexa-skills-kit

我已经使用我自己的自定义webhook和ask-sdk部署在我的ec2实例中。现在我想使用DynamoDB作为DynamoDbPersistenceAdapter 但是我没有任何参考。

DynamoDbPersistenceAdapter将需要AWS Key和表名以及dynamo数据库的一些详细信息,但在哪里初始化呢?我找到了一些代码,但这没有任何东西:

persistenceAdapter = new DynamoDbPersistenceAdapter({ 
        tableName: 'global_attr_table',
        createTable: true,
        partitionKeyGenerator: keyGenerator
    });

1 个答案:

答案 0 :(得分:1)

这可以通过添加环境变量和设置AWS CLI配置文件来解决:

此处介绍如何设置AWS CLI配置文件: https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html

一旦使用AWS访问信息进行了配置文件设置,便可以在命令行或Shell脚本中导出环境变量

$> export AWS_PROFILE=YourNewAWSCLIProfileName
$> export AWS_REGION=us-east-1
$> export AWS_DEFAULT_REGION=us-east-1

,您可以通过输入来检查这些变量是否已设置

$> echo $AWS_PROFILE
$> echo $AWS_REGION
$> echo $AWS_DEFAULT_REGION

这就是我用的。如果由于某种原因在这里不起作用,请研究如何添加DynamoDB客户端:

尝试解决其他问题,所以让我在我的研究中解决您的问题: 在:node_modules / ask-sdk / dist / skill / factory / StandardSkillFactory.js 引用了与您上面类似的内容

new ask_sdk_dynamodb_persistence_adapter_1.DynamoDbPersistenceAdapter({
     tableName: thisTableName,
     createTable: thisAutoCreateTable,
     partitionKeyGenerator: thisPartitionKeyGenerator,
     dynamoDBClient: thisDynamoDbClient,
})

我相信您需要创建一个DynamoDbClient实例,该实例在AWS开发工具包文档中找到了。

https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/dynamodb-example-document-client.html

您必须设置自己的服务: 在:node_modules / aws-sdk / lib / dynamodb / document_client.js

  /**
   * Creates a DynamoDB document client with a set of configuration options.
   *
   * @option options params [map] An optional map of parameters to bind to every
   *   request sent by this service object.
   * @option options service [AWS.DynamoDB] An optional pre-configured instance
   *  of the AWS.DynamoDB service object to use for requests. The object may
   *  bound parameters used by the document client.
   * @option options convertEmptyValues [Boolean] set to true if you would like
   *  the document client to convert empty values (0-length strings, binary
   *  buffers, and sets) to be converted to NULL types when persisting to
   *  DynamoDB.
   * @see AWS.DynamoDB.constructor
   *
   */
  constructor: function DocumentClient(options) {
    var self = this;
    self.options = options || {};
    self.configure(self.options);
  },

  /**
   * @api private
   */
  configure: function configure(options) {
    var self = this;
    self.service = options.service;
    self.bindServiceObject(options);
    self.attrValue = options.attrValue =
      self.service.api.operations.putItem.input.members.Item.value.shape;
  },

  /**
   * @api private
   */
  bindServiceObject: function bindServiceObject(options) {
    var self = this;
    options = options || {};

    if (!self.service) {
      self.service = new AWS.DynamoDB(options);
    } else {
      var config = AWS.util.copy(self.service.config);
      self.service = new self.service.constructor.__super__(config);
      self.service.config.params =
        AWS.util.merge(self.service.config.params || {}, options.params);
    }
  },

我不确定这些选项的外观。