Skip to content

Commit

Permalink
fix: AWS S3 signed URL 타입 변경에 따른, 테스트케이스 수정 및 코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
cokia committed Nov 16, 2023
1 parent b23f71d commit cbbc98a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 41 deletions.
55 changes: 32 additions & 23 deletions src/modules/stores/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,42 @@ module.exports.getList = (directoryPath, cb) => {
};

// function to generate signed-url for upload(PUT)
module.exports.getUploadPUrlPut = (filePath, contentType = "image/png") => {
const presignedUrl = getSignedUrl(s3, new PutObjectCommand({
Bucket: awsEnv.s3BucketName,
Key: filePath,
ContentType: contentType
}), {
expiresIn: 60
});
module.exports.getUploadPUrlPut = async (
filePath,
contentType = "image/png"
) => {
const presignedUrl = await getSignedUrl(
s3,
new PutObjectCommand({
Bucket: awsEnv.s3BucketName,
Key: filePath,
ContentType: contentType,
}),
{
expiresIn: 60,
}
);
return presignedUrl;
};

// function to generate signed-url for upload(POST)
module.exports.getUploadPUrlPost = (filePath, contentType, cb) => {
s3.createPresignedPost(
{
Bucket: awsEnv.s3BucketName,
Expires: 60, // 1 min
Conditions: [
{ key: filePath },
["eq", "$Content-Type", contentType],
["content-length-range", 1, 2 * 1024 * 1024], // Maximum file size is 2MB
],
},
(err, data) => {
cb(err, data);
}
);
module.exports.getUploadPUrlPost = async (filePath, contentType) => {
try {
const presignedUrl = await getSignedUrl(
s3,
new PutObjectCommand({
Bucket: awsEnv.s3BucketName,
Key: filePath,
contentType: contentType,
}),
{
expiresIn: 60,
}
);
return presignedUrl;
} catch (e) {
return e;
}
};

// function to delete object
Expand Down
16 changes: 3 additions & 13 deletions src/services/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,16 @@ const editAccountHandler = async (req, res) => {
const editProfileImgGetPUrlHandler = async (req, res) => {
try {
const type = req.body.type;
logger.info(type);
const user = await userModel.findOne({ id: req.userId }, "_id");
if (!user) {
return res
.status(500)
.send("User/editProfileImg/getPUrl : internal server error");
}
const key = `profile-img/${user._id}`;
aws.getUploadPUrlPost(key, type, (err, data) => {
if (err) {
return res
.status(500)
.send("User/editProfileImg/getPUrl : internal server error");
}
data.fields["Content-Type"] = type;
data.fields["key"] = key;
res.json({
url: data.url,
fields: data.fields,
});
});
const data = await aws.getUploadPUrlPost(key, type);
res.json({ url: data });
} catch (e) {
res.status(500).send("User/editProfileImg/getPUrl : internal server error");
}
Expand Down
5 changes: 0 additions & 5 deletions test/services/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,6 @@ describe("[users] 5.editProfileImgGetPUrlHandler", () => {
const resJson = res._getJSONData();
expect(res).to.has.property("statusCode", 200);
expect(resJson).to.has.property("url");
expect(resJson.fields).to.has.property(
"key",
`profile-img/${testUser1._id}`
);
expect(resJson.fields).to.has.property("Content-Type", testImgType);
});
});

Expand Down

0 comments on commit cbbc98a

Please sign in to comment.