-
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 #13 from Kusitms-28th-Meetup-D/feature/10-S3
S3 연동 및 이미지 업로드
- Loading branch information
Showing
11 changed files
with
186 additions
and
5 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
34 changes: 34 additions & 0 deletions
34
src/main/java/com/kusithm/meetupd/common/config/S3Config.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,34 @@ | ||
package com.kusithm.meetupd.common.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 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 credentials = new BasicAWSCredentials(accessKey, secretKey); | ||
|
||
return (AmazonS3Client) AmazonS3ClientBuilder | ||
.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(credentials)) | ||
.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
8 changes: 8 additions & 0 deletions
8
src/main/java/com/kusithm/meetupd/common/error/FileUploadFailedException.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,8 @@ | ||
package com.kusithm.meetupd.common.error; | ||
|
||
public class FileUploadFailedException extends ApplicationException { | ||
|
||
public FileUploadFailedException(ErrorCode error) { | ||
super(error); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/kusithm/meetupd/domain/fileUpload/controller/S3FileUploadController.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,24 @@ | ||
package com.kusithm.meetupd.domain.fileUpload.controller; | ||
|
||
import com.kusithm.meetupd.domain.fileUpload.dto.response.FileUploadResponse; | ||
import com.kusithm.meetupd.domain.fileUpload.service.AwsS3Service; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/api/s3") | ||
public class S3FileUploadController { | ||
|
||
private final AwsS3Service awsS3Service; | ||
|
||
@PostMapping("/upload") | ||
public FileUploadResponse uploadFile( | ||
@RequestPart(value = "file") List<MultipartFile> images) { | ||
return awsS3Service.uploadFile(images); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/kusithm/meetupd/domain/fileUpload/dto/response/FileUploadResponse.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,20 @@ | ||
package com.kusithm.meetupd.domain.fileUpload.dto.response; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Builder | ||
@Getter | ||
public class FileUploadResponse { | ||
List<String> fileUrls = new ArrayList<>(); | ||
|
||
public FileUploadResponse (List<String> fileUrls){ | ||
this.fileUrls=fileUrls; | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/kusithm/meetupd/domain/fileUpload/entity/Image.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,24 @@ | ||
package com.kusithm.meetupd.domain.fileUpload.entity; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
@Builder | ||
@Entity(name = "Image") | ||
public class Image { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "image_id") | ||
private Long id; | ||
|
||
@Column(name = "image_url", nullable = false) | ||
private String imageUrl; | ||
|
||
public Image(String url){ | ||
this.imageUrl=url; | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/kusithm/meetupd/domain/fileUpload/mysql/ImageRepository.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,9 @@ | ||
package com.kusithm.meetupd.domain.fileUpload.mysql; | ||
|
||
|
||
import com.kusithm.meetupd.domain.fileUpload.entity.Image; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ImageRepository extends JpaRepository<Image, Long> { | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/kusithm/meetupd/domain/fileUpload/service/AwsS3Service.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,60 @@ | ||
package com.kusithm.meetupd.domain.fileUpload.service; | ||
|
||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import com.amazonaws.services.s3.model.PutObjectResult; | ||
import com.kusithm.meetupd.common.error.ErrorCode; | ||
import com.kusithm.meetupd.common.error.FileUploadFailedException; | ||
import com.kusithm.meetupd.domain.fileUpload.dto.response.FileUploadResponse; | ||
import com.kusithm.meetupd.domain.fileUpload.entity.Image; | ||
import com.kusithm.meetupd.domain.fileUpload.mysql.ImageRepository; | ||
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.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.kusithm.meetupd.common.error.ErrorCode.FILE_UPLOAD_FAILED; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Service | ||
public class AwsS3Service { | ||
private final AmazonS3Client amazonS3Client; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucketName; | ||
private final ImageRepository imageRepository; | ||
|
||
public FileUploadResponse uploadFile(List<MultipartFile> images) { | ||
|
||
List<String> fileUrls = new ArrayList<>(); | ||
for (MultipartFile image : images) { | ||
String fileName = image.getOriginalFilename(); | ||
|
||
ObjectMetadata objectMetadata = new ObjectMetadata(); | ||
objectMetadata.setContentType(image.getContentType()); | ||
|
||
try (InputStream inputStream = image.getInputStream()) { | ||
amazonS3Client.putObject(new PutObjectRequest(bucketName, fileName, inputStream, objectMetadata) | ||
.withCannedAcl(CannedAccessControlList.PublicRead)); | ||
|
||
String imagePath = amazonS3Client.getUrl(bucketName, fileName).toString(); // 접근가능한 URL 가져오기 | ||
fileUrls.add(imagePath); | ||
imageRepository.save(new Image(imagePath)); | ||
|
||
} catch (IOException e) { | ||
throw new FileUploadFailedException(FILE_UPLOAD_FAILED); | ||
} | ||
} | ||
|
||
return new FileUploadResponse(fileUrls); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -34,4 +34,4 @@ private Sample saveText(String text) { | |
sampleRepository.save(sample); | ||
return sample; | ||
} | ||
} | ||
} |