-
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 구현 S3Service 추가 스티커 이미지 업로드 API 구현 Related to: #146 * [Feat]: 스티커 이미지 업로드 API 구현 Related to: #146
- Loading branch information
Showing
8 changed files
with
170 additions
and
36 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
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
91 changes: 91 additions & 0 deletions
91
src/main/java/io/sobok/SobokSobok/external/aws/s3/S3Service.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,91 @@ | ||
package io.sobok.SobokSobok.external.aws.s3; | ||
|
||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import io.sobok.SobokSobok.exception.ErrorCode; | ||
import io.sobok.SobokSobok.exception.model.BadRequestException; | ||
import io.sobok.SobokSobok.exception.model.NotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.UUID; | ||
import javax.annotation.PostConstruct; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class S3Service { | ||
|
||
private AmazonS3 amazonS3; | ||
|
||
@Value("${spring.cloud.aws.credentials.access-key}") | ||
private String accessKey; | ||
|
||
@Value("${spring.cloud.aws.credentials.secret-key}") | ||
private String secretKey; | ||
|
||
@Value("${spring.cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
@Value("${spring.cloud.aws.region.static}") | ||
private String region; | ||
|
||
@PostConstruct | ||
public void amazonS3Client() { | ||
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); | ||
amazonS3 = AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) | ||
.build(); | ||
} | ||
|
||
public String uploadImage(MultipartFile multipartFile, String folder) { | ||
String fileName = createFileName(multipartFile.getOriginalFilename()); | ||
ObjectMetadata objectMetadata = new ObjectMetadata(); | ||
objectMetadata.setContentLength(multipartFile.getSize()); | ||
objectMetadata.setContentType(multipartFile.getContentType()); | ||
try (InputStream inputStream = multipartFile.getInputStream()) { | ||
amazonS3.putObject( | ||
new PutObjectRequest(bucket + "/" + folder + "/image", fileName, inputStream, | ||
objectMetadata) | ||
.withCannedAcl(CannedAccessControlList.PublicRead)); | ||
return amazonS3.getUrl(bucket + "/" + folder + "/image", fileName).toString(); | ||
} catch (IOException e) { | ||
throw new NotFoundException(ErrorCode.NOT_FOUND_SAVE_IMAGE_EXCEPTION); | ||
} | ||
} | ||
|
||
// 파일명 (중복 방지) | ||
private String createFileName(String fileName) { | ||
return UUID.randomUUID().toString().concat(getFileExtension(fileName)); | ||
} | ||
|
||
// 파일 유효성 검사 | ||
private String getFileExtension(String fileName) { | ||
if (fileName.length() == 0) { | ||
throw new NotFoundException(ErrorCode.NOT_FOUND_IMAGE_EXCEPTION); | ||
} | ||
ArrayList<String> fileValidate = new ArrayList<>(); | ||
fileValidate.add(".jpg"); | ||
fileValidate.add(".jpeg"); | ||
fileValidate.add(".png"); | ||
fileValidate.add(".JPG"); | ||
fileValidate.add(".JPEG"); | ||
fileValidate.add(".PNG"); | ||
String idxFileName = fileName.substring(fileName.lastIndexOf(".")); | ||
if (!fileValidate.contains(idxFileName)) { | ||
throw new BadRequestException(ErrorCode.INVALID_MULTIPART_EXTENSION_EXCEPTION); | ||
} | ||
return fileName.substring(fileName.lastIndexOf(".")); | ||
} | ||
|
||
} |
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