-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3 feat : 프로필 이미지 추가, AWS S3 연동, User 출력 수정 (Socket Exception)
- Loading branch information
Showing
11 changed files
with
198 additions
and
5 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.zatch.zatchserver.config; | ||
|
||
import com.amazonaws.auth.AWSCredentials; | ||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
|
||
@Configuration | ||
public class S3Config { | ||
@Value("${cloud.aws.credentials.accessKey}") | ||
private String accessKey; | ||
|
||
@Value("${cloud.aws.credentials.secretKey}") | ||
private String secretKey; | ||
|
||
@Value("${cloud.aws.region.static}") | ||
private String region; | ||
|
||
// @Bean | ||
// @Primary | ||
// public BasicAWSCredentials awsCredentialsProvider(){ | ||
// BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey); | ||
// return basicAWSCredentials; | ||
// } | ||
|
||
@Bean | ||
public AmazonS3 amazonS3Client() { | ||
System.out.println("KEY"+secretKey); | ||
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); | ||
// AmazonS3 s3Builder = AmazonS3ClientBuilder | ||
// .standard() | ||
// .withRegion(region) | ||
// .withCredentials(new AWSStaticCredentialsProvider(awsCredentialsProvider())) | ||
// .build(); | ||
// return s3Builder; | ||
|
||
return AmazonS3ClientBuilder | ||
.standard() | ||
.withCredentials(new AWSStaticCredentialsProvider(credentials)) | ||
.withRegion(region) | ||
.build(); | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
src/main/java/com/zatch/zatchserver/dto/PostUserProfileReqDto.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,11 @@ | ||
package com.zatch.zatchserver.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class PostUserProfileReqDto { | ||
private MultipartFile image; | ||
} |
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
72 changes: 72 additions & 0 deletions
72
src/main/java/com/zatch/zatchserver/service/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,72 @@ | ||
package com.zatch.zatchserver.service; | ||
|
||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
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.context.annotation.Bean; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.util.Optional; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor // final 멤버변수가 있으면 생성자 항목에 포함시킴 | ||
@Component | ||
@Service | ||
public class S3Uploader { | ||
|
||
private final AmazonS3Client amazonS3Client; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
// MultipartFile을 전달받아 File로 전환한 후 S3에 업로드 | ||
public String upload(MultipartFile multipartFile, String dirName) throws IOException { | ||
File uploadFile = convert(multipartFile) | ||
.orElseThrow(() -> new IllegalArgumentException("MultipartFile -> File 전환 실패")); | ||
return upload(uploadFile, dirName); | ||
} | ||
|
||
private String upload(File uploadFile, String dirName) { | ||
String fileName = dirName + "/" + uploadFile.getName(); | ||
String uploadImageUrl = putS3(uploadFile, fileName); | ||
|
||
removeNewFile(uploadFile); // 로컬에 생성된 File 삭제 (MultipartFile -> File 전환 하며 로컬에 파일 생성됨) | ||
|
||
return uploadImageUrl; // 업로드된 파일의 S3 URL 주소 반환 | ||
} | ||
|
||
private String putS3(File uploadFile, String fileName) { | ||
amazonS3Client.putObject( | ||
new PutObjectRequest(bucket, fileName, uploadFile) | ||
.withCannedAcl(CannedAccessControlList.PublicRead) // PublicRead 권한으로 업로드 됨 | ||
); | ||
return amazonS3Client.getUrl(bucket, fileName).toString(); | ||
} | ||
|
||
private void removeNewFile(File targetFile) { | ||
if(targetFile.delete()) { | ||
log.info("파일이 삭제되었습니다."); | ||
}else { | ||
log.info("파일이 삭제되지 못했습니다."); | ||
} | ||
} | ||
|
||
private Optional<File> convert(MultipartFile file) throws IOException { | ||
File convertFile = new File(file.getOriginalFilename()); | ||
if(convertFile.createNewFile()) { | ||
try (FileOutputStream fos = new FileOutputStream(convertFile)) { | ||
fos.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