Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix 포스트수정 오류 수정 #19

Merged
merged 9 commits into from
Aug 21, 2023
Prev Previous commit
Next Next commit
Fix: S3 이미지 API 수정 및 에러 정리
sonji406 committed Aug 21, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 52b884a74114c5cef5fd7e0f78879828c16bc07d
54 changes: 36 additions & 18 deletions src/app/api/v1/image/uploadFile/route.js
Original file line number Diff line number Diff line change
@@ -12,10 +12,10 @@ const s3Data = new S3({

const bucketName = process.env.AWS_S3_BUCKET_NAME;

function GET(request) {
const { file: fileName, fileType } = request.query;
async function GET(request) {
try {
const { file: fileName, fileType } = request.query;

return new Promise((resolve) => {
const s3Params = {
Bucket: bucketName,
Key: fileName,
@@ -24,22 +24,28 @@ function GET(request) {
ACL: 'public-read',
};

s3Data.getSignedUrl('putObject', s3Params, (err, data) => {
if (err) {
throw createError(
ERRORS.SIGNED_URL_CREATION_ERROR.STATUS_CODE,
ERRORS.SIGNED_URL_CREATION_ERROR.MESSAGE,
);
}
const signedUrl = await new Promise((resolve, reject) => {
s3Data.getSignedUrl('putObject', s3Params, (err, data) => {
if (err) {
reject(
createError(
ERRORS.SIGNED_URL_CREATION_ERROR.STATUS_CODE,
ERRORS.SIGNED_URL_CREATION_ERROR.MESSAGE,
),
);
} else {
resolve(data);
}
});
});

resolve(
NextResponse.json({
signedRequest: data,
url: `https://${bucketName}.s3.amazonaws.com/${fileName}`,
}),
);
return NextResponse.json({
signedRequest: signedUrl,
url: `https://${bucketName}.s3.amazonaws.com/${fileName}`,
});
});
} catch (error) {
return sendErrorResponse(error);
}
}

async function POST(request) {
@@ -48,17 +54,29 @@ async function POST(request) {
const file = formData.get('image');

if (!file) {
throw new Error('요청에서 파일을 찾을 수 없습니다.');
throw createError(
ERRORS.FILE_NOT_FOUND.STATUS_CODE,
ERRORS.FILE_NOT_FOUND.MESSAGE,
);
}

const fileName = file.name;
const fileType = file.type;
const fileStream = file.stream();
let fileBuffer = Buffer.alloc(0);

for await (const chunk of fileStream) {
fileBuffer = Buffer.concat([fileBuffer, chunk]);
}

const s3Params = {
Bucket: bucketName,
Key: fileName,
Body: fileBuffer,
ContentType: fileType,
ACL: 'public-read',
};

await s3Data.putObject(s3Params);

const fileUrl = `https://${bucketName}.s3.amazonaws.com/${fileName}`;
6 changes: 6 additions & 0 deletions utils/errors.js
Original file line number Diff line number Diff line change
@@ -38,6 +38,11 @@ const SIGNED_URL_CREATION_ERROR = {
MESSAGE: 'signed URL 생성 오류입니다.',
};

const FILE_NOT_FOUND = {
STATUS_CODE: 400, // 예: 400은 잘못된 요청을 나타냅니다.
MESSAGE: '요청에서 파일을 찾을 수 없습니다.',
};

export const ERRORS = {
INVALID_USER_ID,
USER_NOT_FOUND,
@@ -47,4 +52,5 @@ export const ERRORS = {
MISSING_PARAMETERS,
POST_NOT_FOUND,
SIGNED_URL_CREATION_ERROR,
FILE_NOT_FOUND,
};