-
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 pull request #26 from jwpark1211/develop
Develop
- Loading branch information
Showing
6 changed files
with
141 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package capstone.bookitty.common; | ||
|
||
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 S3Config { | ||
@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
72 changes: 72 additions & 0 deletions
72
src/main/java/capstone/bookitty/domain/service/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,72 @@ | ||
package capstone.bookitty.domain.service; | ||
|
||
import com.amazonaws.AmazonServiceException; | ||
import com.amazonaws.SdkClientException; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.*; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class S3Service { | ||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
private final AmazonS3 amazonS3; | ||
|
||
public String uploadFile(MultipartFile multipartFile) throws IOException { | ||
String fileName = multipartFile.getOriginalFilename(); | ||
|
||
//파일 형식 구하기 | ||
String ext = fileName.split("\\.")[1]; | ||
String contentType = ""; | ||
|
||
//content type을 지정해서 올려주지 않으면 자동으로 "application/octet-stream"으로 고정이 | ||
// 돼서 링크 클릭시 웹에서 열리는게 아니라 자동 다운이 시작됨. | ||
switch (ext) { | ||
case "jpeg": | ||
contentType = "image/jpeg"; | ||
break; | ||
case "png": | ||
contentType = "image/png"; | ||
break; | ||
case "txt": | ||
contentType = "text/plain"; | ||
break; | ||
case "csv": | ||
contentType = "text/csv"; | ||
break; | ||
} | ||
|
||
try { | ||
ObjectMetadata metadata = new ObjectMetadata(); | ||
metadata.setContentType(contentType); | ||
|
||
amazonS3.putObject(new PutObjectRequest(bucket, fileName, multipartFile.getInputStream(), metadata) | ||
.withCannedAcl(CannedAccessControlList.PublicRead)); | ||
} catch (AmazonServiceException e) { | ||
e.printStackTrace(); | ||
} catch (SdkClientException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
//object 정보 가져오기 | ||
ListObjectsV2Result listObjectsV2Result = amazonS3.listObjectsV2(bucket); | ||
List<S3ObjectSummary> objectSummaries = listObjectsV2Result.getObjectSummaries(); | ||
|
||
for (S3ObjectSummary object: objectSummaries) { | ||
System.out.println("object = " + object.toString()); | ||
} | ||
return amazonS3.getUrl(bucket, fileName).toString(); | ||
} | ||
|
||
public void deleteImage(String filename){ | ||
amazonS3.deleteObject(new DeleteObjectRequest(bucket,filename)); | ||
} | ||
|
||
} |