-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[feat] νλ‘ν μ¬μ§ μ λ‘λ ꡬν(#105)
- Loading branch information
Showing
11 changed files
with
170 additions
and
8 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
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,81 @@ | ||
package hyangyu.server.aws; | ||
|
||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.AmazonS3Exception; | ||
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.stereotype.Service; | ||
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 | ||
@RequiredArgsConstructor | ||
@Service | ||
public class S3Uploader { | ||
|
||
private final AmazonS3Client amazonS3Client; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
public String bucket; // S3 λ²ν· μ΄λ¦ | ||
|
||
public String upload(MultipartFile multipartFile, String dirName) throws IOException { | ||
File uploadFile = convert(multipartFile) // νμΌ λ³νν μ μμΌλ©΄ μλ¬ | ||
.orElseThrow(() -> new IllegalArgumentException("error: MultipartFile -> File convert fail")); | ||
|
||
return upload(uploadFile, dirName); | ||
} | ||
|
||
public void delete(String name) { | ||
if(!amazonS3Client.doesObjectExist(bucket, name)) | ||
throw new AmazonS3Exception("Object " +name+ " does not exist!"); | ||
amazonS3Client.deleteObject(bucket,name); | ||
} | ||
|
||
// S3λ‘ νμΌ μ λ‘λνκΈ° | ||
private String upload(File uploadFile, String dirName) { | ||
String fileName = dirName + "/" + UUID.randomUUID() + uploadFile.getName(); // S3μ μ μ₯λ νμΌ μ΄λ¦ | ||
String uploadImageUrl = putS3(uploadFile, fileName); // s3λ‘ μ λ‘λ | ||
removeNewFile(uploadFile); | ||
return fileName; | ||
} | ||
|
||
// S3λ‘ μ λ‘λ | ||
private String putS3(File uploadFile, String fileName) { | ||
amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, uploadFile).withCannedAcl(CannedAccessControlList.PublicRead)); | ||
return amazonS3Client.getUrl(bucket, fileName).toString(); | ||
} | ||
|
||
public String getThumbnailPath(String path) { | ||
return amazonS3Client.getUrl(bucket, path).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 file) throws IOException { | ||
File convertFile = new File(System.getProperty("user.dir") + "/" + file.getOriginalFilename()); | ||
if (convertFile.createNewFile()) { // λ°λ‘ μμμ μ§μ ν κ²½λ‘μ Fileμ΄ μμ±λ¨ (κ²½λ‘κ° μλͺ»λμλ€λ©΄ μμ± λΆκ°λ₯) | ||
try (FileOutputStream fos = new FileOutputStream(convertFile)) { // FileOutputStream λ°μ΄ν°λ₯Ό νμΌμ λ°μ΄νΈ μ€νΈλ¦ΌμΌλ‘ μ μ₯νκΈ° μν¨ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package hyangyu.server.config; | ||
|
||
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.access-key}") | ||
private String accessKey; | ||
|
||
@Value("${cloud.aws.credentials.secret-key}") | ||
private String secretKey; | ||
|
||
@Value("${cloud.aws.region.static}") | ||
private String region; | ||
|
||
@Bean | ||
public AmazonS3Client amazonS3Client() { | ||
BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey); | ||
return (AmazonS3Client) AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCreds)) | ||
.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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.