DynamoDB table seed 在 cli 中有效,但在 AWS-SDK 中无效

DynamoDB table seed works in cli but not AWS-SDK

我有一个 table,它有超过 25 个项目,并编写了一个基本脚本将它们分成 25 个项目的子数组,然后循环遍历该子数组集合到 运行 一个批次在 AWS DynamoDB 客户端中写入项目命令。我遇到的问题是返回的验证错误。当我通过 aws-cli 运行 相同的种子文件时,它完美地为 table 播种。这让我觉得这与我的脚本有关。看到我遗漏了什么吗?提前致谢!

var { DynamoDB } = require('aws-sdk');
var db = new DynamoDB.DocumentClient({
  region: 'localhost',
  endpoint: 'http://localhost:8000',
});
const allItems = require('./allItems.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) {
    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('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);

    try {
        //send to db
        db.batchWrite(params, requestHandler(params));
    } catch{
        console.error(err)
    }
}

这是格式化的请求对象和错误:

before db.batchWriteItem:  
{ RequestItems:
   { 'some-table-name': [ [Object], [Object], [Object], [Object] ] } 
}
In the request handler...
Request caused a DB error.
ERROR: ValidationException: Invalid attribute value type
{
  "message": "Invalid attribute value type",
  "code": "ValidationException",
  "time": "2020-08-04T10:51:13.751Z",
  "requestId": "dd49628c-6ee9-4275-9349-6edca29636fd",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 47.94198279972915
}

您在nodejs代码中使用了DocumentClient。这会自动将 DynamoDB 使用的数据格式转换为更易于使用的格式。

例如

  {
     "id": {
       "S": "A string value"
     }
  }

会变成

  {
     "id": "A string value"
  }

CLI 不执行此数据转换。 您可以使用常规 DynamoDB 客户端不在 Nodejs 中执行此转换。例如const db = new Dynamodb()