【发布时间】:2020-12-09 10:55:23
【问题描述】:
我正在使用 lambda、API 网关和 dynamodb
我正在创建一个 batchWrite 函数,以便在一个函数中上传超过 25 个项目。
但我发现当我上传 35 个项目时,
前 25 个项目成功上传到 dynamodb,但后 10 个项目失败但没有记录错误。
函数是用node.js写的
'use strict'
const AWS = require('aws-sdk');
const sha1 = require('sha1');
const documentClient = new AWS.DynamoDB.DocumentClient();
exports.handler = async function (event, context, callback) {
let responseBody = "";
let statusCode = 0;
const { excelObject } = JSON.parse(event.body);
if(excelObject){
await uploadFileByBatch(excelObject)
}
const response = {
statusCode: statusCode,
headers:{
"Content-Type": "application/json",
"access-control-allow-origin": "*"
},
body: responseBody
}
console.log(response)
return response
}
let uploadFileByBatch = async function (payload) {
var items = [];
var params={};
if(payload.length > 25){
console.log("> 25")
let newObj = payload.slice(0,25)
payload.splice(0,25)
console.log("new Payload length")
console.log(payload.length)
newObj.forEach(obj =>{
let hash = sha1(Buffer.from(new Date().toString()+ Math.random()));
items.push(
{
PutRequest:{
Item: {
id: obj.id?obj.id:hash,
organization_EN: obj.organization_EN,
email: obj.email,
isActive: obj.isActive
}
}
}
)
})
params = {
RequestItems:{
"Community": items
}
}
console.log(params)
try{
const data = await documentClient.batchWrite(params).promise();
}catch(err){
console.log(err)
}
uploadFileByBatch(payload)
}else{
console.log("less than 25")
payload.forEach(obj =>{
let hash = sha1(Buffer.from(new Date().toString()+ Math.random()));
items.push(
{
PutRequest:{
Item: {
id: obj.id?obj.id:hash,
organization_EN: obj.organization_EN,
email: obj.email,
isActive: obj.isActive
}
}
}
)
})
params = {
RequestItems:{
"Community": items
}
}
console.log(params)
try{
console.log("first BatchWrite")
const data = await documentClient.batchWrite(params).promise();
console.log("first Data")
}catch(err){
console.log(err)
}
}
}
日志如下。
>25
New Payload length
10
{
RequestItems: {
'Community': [
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object]
]
}
}
first BatchWrite
Less than 25
{
RequestItems: {
'Community': [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object]
]
}
}
{
statusCode: 0,
headers: {
'Content-Type': 'application/json',
'access-control-allow-origin': '*'
},
body: ''
}
【问题讨论】:
标签: node.js amazon-web-services aws-lambda amazon-dynamodb