인프런 커뮤니티 질문&답변
lambda NoSuchKey 에러
작성
·
620
0
안녕하세요 바쁘실텐데 질문 드려 죄송합니다.
lambda함수에서 NoSuchKey 라는 에러가 아주아주 종종 한 번씩 발생이 됩니다.
어떤 특정 파일만 그런 것도 아니고 매번 그런 것도 아니라
무슨 상황에서 에러가 발생하는지 찾지를 못하겠습니다...
lambda/index.js
const AWS = require("aws-sdk");
const sharp = require("sharp");
const s3 = new AWS.S3();
exports.handler = async (event, context, callback) => {
const Bucket = event.Records[0].s3.bucket.name; // react-nodebird-s3
const Key = decodeURIComponent(event.Records[0].s3.object.key); // original/12312312_abc.png
console.log(Bucket, Key);
const filename = encodeURIComponent(
Key.split("/")[Key.split("/").length - 1]
);
const ext = Key.split(".")[Key.split(".").length - 1].toLowerCase();
const requiredFormat = ext === "jpg" ? "jpeg" : ext;
console.log("filename", filename, "ext", ext);
try {
const s3Object = await s3.getObject({ Bucket, Key }).promise();
console.log("original", s3Object.Body.length);
const resizedImage = await sharp(s3Object.Body)
.resize(400, 400, { fit: "inside" })
.toFormat(requiredFormat)
.toBuffer();
await s3
.putObject({
Bucket,
Key: `thumb/${filename}`,
Body: resizedImage,
})
.promise();
console.log("put", resizedImage.length);
return callback(null, `thumb/${filename}`);
} catch (error) {
console.error(error);
return callback(error);
}
};
cloudwatch log
어디가 문제일까요ㅠㅠ
답변 1
0
huhu
질문자
https://velog.io/@bigbrothershin/AWS-lambda-handler-%ED%95%A8%EC%88%98
파일명에 공백이 있어 그런 것 같습니다...!!
링크와 같이 정규표현식을 했는데도 띄어쓰기가 + 인 상태인데
lambda/index.js를 고치고
const Key = decodeURIComponent(
event.Records[0].s3.object.key.replace(/\+/g, " ")
);
깃 푸시한다음 백과 프론트 모두 각각 ec2 인스턴스를 연결하고 깃 풀 한 후
프론트는 빌드까지 하고 각각 npx pm2 reload all 을 했으나 여전히 cloudwatch에선 띄어쓰기가 +로 나옵니다...
console.log(Bucket, Key);
cloudwatch에서 확인한 로그
이미지 깨짐 현상
깨져도 누르면 이미지 줌은 잘됩니다... original엔 문제가 없고 thumb에만 그런 것 같습니다.
huhu
질문자
어느 댓글에서 encodeURIComponent 넣으란 걸 봤어서 넣어놨었는데...빼니까 잘 되네요...!
const Key = decodeURIComponent(
event.Records[0].s3.object.key.replace(/\+/g, " ")
);
파일명에 공백(띄어쓰기) 있으면 이미지 깨지는 분들은 그냥 요고만 하시면 될 것 같습니당





어떻게 하다보니 이젠 띄어쓰기가 +로 바뀌진 않는데 여전히
이미지가 깨집니다...ㅠㅠㅠ