forked from 9oormthon-univ/2024_DANPOONG_TEAM_5_BE
-
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.
Merge branch '9oormthon-univ:develop' into develop
- Loading branch information
Showing
8 changed files
with
170 additions
and
7 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
29 changes: 29 additions & 0 deletions
29
src/main/java/com/jangburich/global/config/s3/AmazonS3Config.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,29 @@ | ||
package com.jangburich.global.config.s3; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
|
||
@Configuration | ||
public class AmazonS3Config { | ||
@Value("${cloud.aws.credentials.access-key}") | ||
private String accessKey; | ||
@Value("${cloud.aws.credentials.secret-key}") | ||
private String secretKey; | ||
@Value("${cloud.aws.region.static}") | ||
private String region; | ||
|
||
@Bean | ||
public AmazonS3 amazonS3() { | ||
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); | ||
return AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) | ||
.build(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/jangburich/global/config/s3/MultipartConfig.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,36 @@ | ||
package com.jangburich.global.config.s3; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.web.servlet.MultipartConfigFactory; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.util.unit.DataSize; | ||
import org.springframework.web.multipart.MultipartResolver; | ||
import org.springframework.web.multipart.support.StandardServletMultipartResolver; | ||
|
||
import jakarta.servlet.MultipartConfigElement; | ||
|
||
@Configuration | ||
public class MultipartConfig { | ||
|
||
@Value("${file.multipart.maxUploadSize}") | ||
private long maxUploadSize; | ||
|
||
@Value("${file.multipart.maxUploadSizePerFile}") | ||
private long maxUploadSizePerFile; | ||
|
||
@Bean | ||
public MultipartResolver multipartResolver() { | ||
StandardServletMultipartResolver multipartResolver = new StandardServletMultipartResolver(); | ||
return multipartResolver; | ||
} | ||
|
||
@Bean | ||
public MultipartConfigElement multipartConfigElement() { | ||
MultipartConfigFactory factory = new MultipartConfigFactory(); | ||
factory.setMaxRequestSize(DataSize.ofBytes(maxUploadSize)); | ||
factory.setMaxFileSize(DataSize.ofBytes(maxUploadSizePerFile)); | ||
|
||
return factory.createMultipartConfig(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/jangburich/global/config/s3/MultipartJackson2HttpMessageConverter.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,35 @@ | ||
package com.jangburich.global.config.s3; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
import org.springframework.http.MediaType; | ||
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
@Component | ||
public class MultipartJackson2HttpMessageConverter extends AbstractJackson2HttpMessageConverter { | ||
|
||
/** | ||
* Converter for support http request with header Content-Type: multipart/form-data | ||
*/ | ||
public MultipartJackson2HttpMessageConverter(ObjectMapper objectMapper) { | ||
super(objectMapper, MediaType.APPLICATION_OCTET_STREAM); | ||
} | ||
|
||
@Override | ||
public boolean canWrite(Class<?> clazz, MediaType mediaType) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean canWrite(Type type, Class<?> clazz, MediaType mediaType) { | ||
return false; | ||
} | ||
|
||
@Override | ||
protected boolean canWrite(MediaType mediaType) { | ||
return false; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/jangburich/global/config/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,53 @@ | ||
package com.jangburich.global.config.s3; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.DeleteObjectRequest; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import com.amazonaws.services.s3.model.PutObjectResult; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class S3Service { | ||
private final AmazonS3 amazonS3; | ||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
private String changedImageName(String originName) { | ||
String random = UUID.randomUUID().toString(); | ||
return random +'-' + originName; | ||
} | ||
|
||
public String uploadImageToS3(MultipartFile image) { | ||
String originName = image.getOriginalFilename(); | ||
String ext = originName.substring(originName.lastIndexOf(".")); | ||
String changedName = changedImageName(originName); | ||
ObjectMetadata metadata = new ObjectMetadata(); | ||
metadata.setContentLength(image.getSize()); | ||
metadata.setContentType("image/" + ext); | ||
|
||
try { | ||
PutObjectResult putObjectResult = amazonS3.putObject(new PutObjectRequest( | ||
bucket, changedName, image.getInputStream(), metadata | ||
)); | ||
} catch (IOException e) { | ||
throw new RuntimeException("ImageUploadException"); | ||
} | ||
return amazonS3.getUrl(bucket, changedName).toString(); | ||
} | ||
|
||
public void deleteImageOnS3(String imgURL) { | ||
String fileName = imgURL.substring(imgURL.lastIndexOf("-"));; | ||
|
||
amazonS3.deleteObject(new DeleteObjectRequest(bucket, fileName)); | ||
} | ||
} |