Skip to content

Commit

Permalink
Merge pull request #81 from KUIT-Space/chore/#79/image_upload_fix
Browse files Browse the repository at this point in the history
[fix] ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ๋ฒ„๊ทธ ์ˆ˜์ •
  • Loading branch information
seongjunnoh authored Aug 12, 2024
2 parents 6d51fa9 + f4fb940 commit 4937379
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public enum BaseExceptionResponseStatus implements ResponseStatus {
*/

IS_NOT_IMAGE_FILE(9000, HttpStatus.BAD_REQUEST, "์ง€์›๋˜๋Š” ์ด๋ฏธ์ง€ ํŒŒ์ผ์˜ ํ˜•์‹์ด ์•„๋‹™๋‹ˆ๋‹ค."),

MULTIPARTFILE_CONVERT_FAILE_IN_MEMORY(9001,HttpStatus.INTERNAL_SERVER_ERROR,"multipartFile memory ๋ณ€ํ™˜ ๊ณผ์ •์—์„œ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค."),
/*
* 10000: voice room ์˜ค๋ฅ˜
*/
Expand Down
46 changes: 35 additions & 11 deletions src/main/java/space/space_spring/service/S3Uploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import space.space_spring.exception.CustomException;
import space.space_spring.validator.AllowedImageFileExtensions;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;

import static space.space_spring.response.status.BaseExceptionResponseStatus.MULTIPARTFILE_CONVERT_FAILE_IN_MEMORY;

@Slf4j
@RequiredArgsConstructor
@Service
Expand All @@ -27,20 +32,39 @@ public class S3Uploader {
private String bucket;

//MultipartFile์„ ์ „๋‹ฌ ๋ฐ›์•„ File๋กœ ์ „ํ™˜ ํ›„ S3 ์—…๋กœ๋“œ
public String upload(MultipartFile multipartFile, String dirName) throws IOException{// dirName์˜ ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ S3 Bucket ๋‚ด๋ถ€์— ์ƒ์„ฑ๋จ
// public String uploadFile(MultipartFile multipartFile, String dirName) throws IOException{// dirName์˜ ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ S3 Bucket ๋‚ด๋ถ€์— ์ƒ์„ฑ๋จ
//
// File uploadFile = convert(multipartFile).orElseThrow(()-> new IllegalArgumentException("MultipartFile -> File ์ „ํ™˜ ์‹คํŒจ"));
// //System.out.p
// // print("error: multipart file input. cant control");
// return upload(uploadFile,dirName);
// }

File uploadFile = convert(multipartFile).orElseThrow(()-> new IllegalArgumentException("MultipartFile -> File ์ „ํ™˜ ์‹คํŒจ"));
//System.out.p
// print("error: multipart file input. cant control");
return upload(uploadFile,dirName);
}
public String upload(File uploadFile, String dirName){
String fileName = dirName+"/"+uploadFile.getName();
String uploadImageUrl = putS3(uploadFile,fileName);
// File์— ์ €์žฅํ•˜์ง€ ์•Š๊ณ  Memory์—์„œ ๋ณ€ํ™˜ ์‹œํ–‰
public String upload(MultipartFile file, String dirName) throws IOException{
String fileName = dirName + "/" + file.getOriginalFilename();
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(file.getContentType());
metadata.setContentLength(file.getSize());

removeNewFile(uploadFile);// convert()ํ•จ์ˆ˜๋กœ ์ธํ•ด์„œ ๋กœ์ปฌ์— ์ƒ์„ฑ๋œ File ์‚ญ์ œ (MultipartFile -> File ์ „ํ™˜ ํ•˜๋ฉฐ ๋กœ์ปฌ์— ํŒŒ์ผ ์ƒ์„ฑ๋จ)
return uploadImageUrl;
try (InputStream inputStream = file.getInputStream()) {
amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, inputStream, metadata));
log.info("File uploaded successfully: {}", fileName);
return amazonS3Client.getUrl(bucket, fileName).toString();
} catch (IOException e) {
log.error("Error uploading file: {}", fileName, e);
throw new CustomException(MULTIPARTFILE_CONVERT_FAILE_IN_MEMORY,"Failed to upload file");
}
}


// public String upload(File uploadFile, String dirName){
// String fileName = dirName+"/"+uploadFile.getName();
// String uploadImageUrl = putS3(uploadFile,fileName);
//
// removeNewFile(uploadFile);// convert()ํ•จ์ˆ˜๋กœ ์ธํ•ด์„œ ๋กœ์ปฌ์— ์ƒ์„ฑ๋œ File ์‚ญ์ œ (MultipartFile -> File ์ „ํ™˜ ํ•˜๋ฉฐ ๋กœ์ปฌ์— ํŒŒ์ผ ์ƒ์„ฑ๋จ)
// return uploadImageUrl;
// }
// ์—…๋กœ๋“œํ•˜๊ธฐ
private String putS3(File uploadFile, String fileName){
amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, uploadFile)
Expand Down

0 comments on commit 4937379

Please sign in to comment.