简体   繁体   English

如何将 AWS-SDK DynamoDB 指向本地无服务器 DynamoDB

[英]How to point AWS-SDK DynamoDB to a serverless DynamoDB local

I am trying to write a script that will loop thru an array of items for a DynamoDB table and run a batch write command.我正在尝试编写一个脚本,该脚本将循环遍历 DynamoDB 表的项目数组并运行批量写入命令。 My functionality is good, but I am having trouble with DynamoDB.我的功能很好,但我在使用 DynamoDB 时遇到了问题。 Would be great if I could point my AWS.DynamoDB.DocumentClient() to my localhost running DynamoDB.如果我可以将我的 AWS.DynamoDB.DocumentClient() 指向运行 DynamoDB 的本地主机,那就太好了。 Any tips?有小费吗?

Would also consider a way to just run the commands via the aws cli but I am not sure how to do that.还会考虑一种通过 aws cli 运行命令的方法,但我不确定该怎么做。 I am running Node.js so it maybe possible?我正在运行 Node.js 所以可能吗?

Here is my code:这是我的代码:

var AWS = require('aws-sdk');
AWS.config.update({ region: 'eu-central-1' });
var DynamoDB = new AWS.DynamoDB.DocumentClient()
DynamoDB.endpoint = 'http://localhost:8000';
const allItems = require('./resource.json');
const tableName = 'some-table-name';
console.log({ tableName, allItems });
var batches = [];
var currentBatch = [];
var count = 0;

for (let i = 0; i < allItems.length; i++) {
  //push item to the current batch
  count++;
  currentBatch.push(allItems[i]);
  if (count % 25 === 0) {
    batches.push(currentBatch);
    currentBatch = [];
  }
}
//if there are still items left in the curr batch, add to the collection of batches
if (currentBatch.length > 0 && currentBatch.length !== 25) {
  batches.push(currentBatch);
}

var completedRequests = 0;
var errors = false;
//request handler for DynamoDB
function requestHandler(err, data) {
  console.log('In the request handler...');
  return function (err, data) {
    completedRequests++;
    errors = errors ? true : err;
    //log error
    if (errors) {
      console.error(JSON.stringify(err, null, 2));
      console.error('Request caused a DB error.');
      console.error('ERROR: ' + err);
      console.error(JSON.stringify(err, null, 2));
    } else {
      var res = {
        statusCode: 200,
        headers: {
          'Content-Type': 'application/json',
          'Access-Control-Allow-Methods': 'GET,POST,OPTIONS',
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Credentials': true,
        },
        body: JSON.stringify(data),
        isBase64Encoded: false,
      };
      console.log(`Success: returned ${data}`);
      return res;
    }
    if (completedRequests == batches.length) {
      return errors;
    }
  };
}

//Make request
var params;
for (let j = 0; j < batches.length; j++) {
  //items go in params.RequestedItems.id array
  //format for the items is {PutRequest : {Item: ITEM_OBJECT}}
  params = '{"RequestItems": {"' + tableName + '": []}}';
  params = JSON.parse(params);
  params.RequestItems[tableName] = batches[j];

  console.log('before db.batchWriteItem: ', params);

  //send to db
  DynamoDB.batchWrite(params, requestHandler(params));
}

I figured it out and will leave this here for anyone that may need it.我想通了,会把它留在这里给任何可能需要它的人。

var { DynamoDB } = require('aws-sdk');
var db = new DynamoDB.DocumentClient({
  region: 'localhost',
  endpoint: 'http://localhost:8000',
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DynamoDB 表种子适用于 cli 但不适用于 AWS-SDK - DynamoDB table seed works in cli but not AWS-SDK 在 typescript 中使用 aws-sdk/lib-dynamodb getall - getall with aws-sdk/lib-dynamodb in typescript `@aws-sdk/lib-dynamodb` DynamoDB delete() 函数返回无用的响应 - `@aws-sdk/lib-dynamodb` DynamoDB delete() function returning unhelpful response docker 上的 Serverless offline 和 DynamoDB local - Serverless offline and DynamoDB local on docker 无法读取 dynamodb 客户端的 aws-sdk js v3 未定义的属性“byteLength” - Cannot read property 'byteLength' of undefined for aws-sdk js v3 for dynamodb client 您可以在 Reactjs 中使用 aws-sdk/util-dynamodb unmarshall 吗? - Can you use aws-sdk/util-dynamodb unmarshall in Reactjs? AWS DynamoDB Java SDK 如何查看表是否启用了时间点恢复 (PITR)? - AWS DynamoDB Java SDK how to see if Point-in-time recovery (PITR) is enabled on table? serverless-s3-local 插件和@aws-sdk/client-s3 使用 PutObjectCommand 返回错误 - serverless-s3-local plugin and @aws-sdk/client-s3 returns an error with PutObjectCommand DynamoDB 上的 ValidationException 与 AWS sdk for Java - ValidationException on DynamoDB with AWS sdk for Java 如何使用 AWS Go SDK 仅更新 DynamoDB 中的单个字段 - How to only update a single field in DynamoDB using AWS Go SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM