-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] 이미지 업로드 API 및 로그인 early 변수 위치 수정
- Loading branch information
Showing
9 changed files
with
107 additions
and
156 deletions.
There are no files selected for viewing
34 changes: 0 additions & 34 deletions
34
src/main/java/modernfarmer/server/farmususer/global/config/s3/AmazonS3ResourceStorage.java
This file was deleted.
Oops, something went wrong.
34 changes: 0 additions & 34 deletions
34
src/main/java/modernfarmer/server/farmususer/global/config/s3/FileDetail.java
This file was deleted.
Oops, something went wrong.
45 changes: 0 additions & 45 deletions
45
src/main/java/modernfarmer/server/farmususer/global/config/s3/MultipartUtil.java
This file was deleted.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
src/main/java/modernfarmer/server/farmususer/global/config/s3/S3Uploader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package modernfarmer.server.farmususer.global.config.s3; | ||
|
||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
@Slf4j | ||
@Component | ||
public class S3Uploader { | ||
private final AmazonS3Client amazonS3Client; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
public S3Uploader(AmazonS3Client amazonS3Client) { | ||
this.amazonS3Client = amazonS3Client; | ||
} | ||
|
||
public String uploadFiles( | ||
MultipartFile multipartFile, String dirName) throws IOException { | ||
File uploadFile = convert(multipartFile).orElseThrow(() -> | ||
new IllegalArgumentException("error: MultipartFile -> File convert fail")); | ||
return upload(uploadFile, dirName); | ||
} | ||
|
||
public String upload(File uploadFile, String filePath) { | ||
String fileName = filePath + "/" + UUID.randomUUID() + uploadFile.getName(); | ||
String uploadImageUrl = putS3(uploadFile, fileName); | ||
removeNewFile(uploadFile); | ||
return uploadImageUrl; | ||
} | ||
|
||
private String putS3(File uploadFile, String fileName) { | ||
amazonS3Client.putObject( | ||
new PutObjectRequest(bucket, fileName, uploadFile) | ||
.withCannedAcl(CannedAccessControlList.PublicRead)); | ||
return amazonS3Client.getUrl(bucket, fileName).toString(); | ||
} | ||
|
||
private void removeNewFile(File targetFile) { | ||
if (targetFile.delete()) { | ||
System.out.println("File delete success"); | ||
return; | ||
} | ||
System.out.println("File delete fail"); | ||
} | ||
|
||
private Optional<File> convert(MultipartFile file) throws IOException { | ||
File convertFile = new File(System.getProperty("user.dir") + "/" + file.getOriginalFilename()); | ||
if (convertFile.createNewFile()) { | ||
try (FileOutputStream fileOutputStream = new FileOutputStream(convertFile)) { | ||
fileOutputStream.write(file.getBytes()); | ||
} | ||
return Optional.of(convertFile); | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.