Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
795 views
in Technique[技术] by (71.8m points)

amazon s3 - AWS S3 lambda upload return 413 Request too long

Im using a lambda to handle images upload. When i upload 2 images it is working fine. But once i upload more than 4 images, api return 413 request too long.

i noted that there is a limit of Invocation payload. Is there any ways to handle this upload action? or any suggestion?

Please help, thanks and regards!

module.exports = {
 create: async (info, files) => {
   if (!info.shop_id || !info.username || !info.comment) {
     response = getResponse(400, {
       code: "FAIL",
       message: "FAIL",
     });
     return response;
   }
   const member = await knex("members").where("username", "=", info.username);
   const Comment_id = await knex(tableName).returning("id").insert({
     shop_id: info.shop_id,
     user_id: member[0].id,
     comment: info.comment,
     attachment: info.attachment,
   });
   if (info.attachment) {
     //upload files to s3
     for (let index = 0; index < files.length; index++) {
       const element = files[index];
       let d = new Date();
       const file_name = `c${Comment_id}-${index}-${d.getDate()}-${d.getMonth()}-${d.getFullYear()}.${
         element.mimetype.split("/")[1]
       }`;
       const destparams = {
         Bucket: s3_config.Bucket,
         Key: `shop/comments/${"s" + info.shop_id}/${file_name}`,
         Body: element.buffer,
         ContentType: "image",
         ACL: "public-read",
       };
       try {
         const putResult = await s3.putObject(destparams).promise();
       } catch (error) {
         console.log(error);
       }
       const addFile = await knex("files").insert({
         mapping_id: Comment_id[0],
         type: "COMMENT_FILE",
         path: `${s3_config.host}shop/comments/${
           "s" + info.shop_id
         }/${file_name}`,
       });
     }
   }

   response = getResponse(200, {
     code: "SUCCESSFUL",
     message: "SUCCESSFUL",
     comment_id: Comment_id,
   });
   return response;
 },
 update: async (id, info) => {
   const isExist = await knex(tableName).where("id", "=", id);
   if (!isExist || !info) {
     response = getResponse(400, {
       code: "FAIL",
       message: "FAIL",
     });
     return response;
   }
   const result = await knex(tableName).where("id", "=", id).update({
     comment: info,
   });
   if (!result) {
     response = getResponse(400, {
       code: "FAIL",
       message: "FAIL",
     });
   } else {
     response = getResponse(200, {
       code: "SUCCESSFUL",
       message: "SUCCESSFUL",
     });
   }

   return response;
 },
 delete: async (id) => {
   const isExist = await knex(tableName).where("id", "=", id);
   if (!isExist) {
     response = getResponse(400, {
       code: "FAIL",
       message: "FAIL",
     });
     return response;
   }
   const result = await knex(tableName).where("id", id).del();
   if (result) {
     response = getResponse(200, {
       code: "SUCCESSFUL",
       message: "SUCCESSFUL",
     });
   } else {
     response = getResponse(400, {
       code: "FAIL",
       message: "FAIL",
     });
   }
   return response;
 },
}
question from:https://stackoverflow.com/questions/66049186/aws-s3-lambda-upload-return-413-request-too-long

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...