-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
451 additions
and
48 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
32 changes: 32 additions & 0 deletions
32
src/main/java/com/cloneweek/hanghaebnb/common/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,32 @@ | ||
package com.cloneweek.hanghaebnb.common.s3; | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
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; | ||
|
||
@Configuration | ||
public class AmazonS3Config { | ||
|
||
@Value("${cloud.aws.credentials.accessKey}") //S3 accessKey | ||
private String accessKey; | ||
|
||
@Value("${cloud.aws.credentials.secretKey}") //S3 secretKey | ||
private String secretKey; | ||
|
||
@Value("${cloud.aws.region.static}") //S3 region | ||
private String region; | ||
|
||
@Bean | ||
public AmazonS3Client amazonS3Client() { | ||
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); | ||
|
||
return (AmazonS3Client) AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) | ||
.build(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/cloneweek/hanghaebnb/common/s3/AmazonS3Controller.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,30 @@ | ||
package com.cloneweek.hanghaebnb.common.s3; | ||
|
||
import com.cloneweek.hanghaebnb.common.security.UserDetailsImpl; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestPart; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/api") | ||
public class AmazonS3Controller { | ||
|
||
private final AmazonS3Service s3Service; | ||
|
||
// @PostMapping(value = "/upload", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) | ||
// public String uploadImage(@RequestPart List<MultipartFile> imagelist, | ||
// @AuthenticationPrincipal UserDetailsImpl userDetails) throws IOException{ | ||
// s3Service.upload(imagelist,"static",userDetails.getUser()); | ||
// return "success"; | ||
// } | ||
} |
99 changes: 99 additions & 0 deletions
99
src/main/java/com/cloneweek/hanghaebnb/common/s3/AmazonS3Service.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,99 @@ | ||
package com.cloneweek.hanghaebnb.common.s3; | ||
|
||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.DeleteObjectRequest; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import com.cloneweek.hanghaebnb.entity.ImageFile; | ||
import com.cloneweek.hanghaebnb.entity.Room; | ||
import com.cloneweek.hanghaebnb.entity.User; | ||
import com.cloneweek.hanghaebnb.repository.ImageFileRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
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.List; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
@Service | ||
public class AmazonS3Service { | ||
|
||
|
||
private final AmazonS3Client amazonS3Client; | ||
private final ImageFileRepository imageFileRepository; | ||
|
||
@Value("${cloud.aws.s3.bucket}") //bucket ์ด๋ฆ | ||
public String bucket; | ||
|
||
|
||
public void upload(List<MultipartFile> multipartFilelist, String dirName, Room room, User user) throws IOException { | ||
|
||
for (MultipartFile multipartFile : multipartFilelist){ | ||
if (multipartFile != null){ | ||
File uploadFile = convert(multipartFile).orElseThrow(() -> new IllegalArgumentException("ํ์ผ ์ ํ ์คํจ")); | ||
ImageFile imageFile = new ImageFile(upload(uploadFile, dirName), user,room); //url, user, room ์ ๋ณด ์ ์ฅ | ||
imageFileRepository.save(imageFile); | ||
} | ||
} | ||
} | ||
|
||
// S3๋ก ํ์ผ ์ ๋ก๋ํ๊ธฐ | ||
private String upload(File uploadFile, String dirName) { | ||
String fileName = dirName + "/" + UUID.randomUUID(); // S3์ ์ ์ฅ๋ ํ์ผ ์ด๋ฆ | ||
String uploadImageUrl = putS3(uploadFile, fileName); // s3๋ก ์ ๋ก๋ | ||
removeNewFile(uploadFile); | ||
return uploadImageUrl; | ||
} | ||
|
||
// S3๋ก ์ ๋ก๋ | ||
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()) { | ||
log.info("File delete success"); | ||
return; | ||
} | ||
log.info("File delete fail"); | ||
} | ||
|
||
private Optional<File> convert(MultipartFile multipartFile) throws IOException { | ||
// File convertFile = new File(System.getProperty("user.dir") + "/" + multipartFile.getOriginalFilename()); //์ด์ ์ฝ๋ 12.23 11:37 | ||
File convertFile = new File(multipartFile.getOriginalFilename()); | ||
// ๋ฐ๋ก ์์์ ์ง์ ํ ๊ฒฝ๋ก์ File์ด ์์ฑ๋จ (๊ฒฝ๋ก๊ฐ ์๋ชป๋์๋ค๋ฉด ์์ฑ ๋ถ๊ฐ๋ฅ) | ||
if (convertFile.createNewFile()) { | ||
try (FileOutputStream fos = new FileOutputStream(convertFile)) { // FileOutputStream ๋ฐ์ดํฐ๋ฅผ ํ์ผ์ ๋ฐ์ดํธ ์คํธ๋ฆผ์ผ๋ก ์ ์ฅํ๊ธฐ ์ํจ | ||
fos.write(multipartFile.getBytes()); | ||
} | ||
return Optional.of(convertFile); | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
// find image from s3 | ||
public String getThumbnailPath(String path) { | ||
return amazonS3Client.getUrl(bucket, path).toString(); | ||
} | ||
|
||
//remove s3 object | ||
public void deleteFile(String fileName){ | ||
DeleteObjectRequest request = new DeleteObjectRequest(bucket, fileName); | ||
amazonS3Client.deleteObject(request); | ||
} | ||
|
||
} |
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.