-
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.
Merge pull request #46 from 7th-UMC/feature/45
Feat : s3설정, 이미지 업로드 api 추가
- Loading branch information
Showing
14 changed files
with
269 additions
and
1 deletion.
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,31 @@ | ||
package hsu.umc.server.aws.s3; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import hsu.umc.server.config.AmazonConfig; | ||
import hsu.umc.server.entity.Uuid; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class AmazonS3Manager { | ||
private final AmazonS3 amazonS3; | ||
private final AmazonConfig amazonConfig; | ||
public String uploadFile(String keyName, MultipartFile file)throws IOException { | ||
System.out.println(keyName); | ||
ObjectMetadata metadata = new ObjectMetadata(); | ||
metadata.setContentLength(file.getSize()); | ||
amazonS3.putObject(new PutObjectRequest(amazonConfig.getBucket(), keyName, file.getInputStream(), metadata)); | ||
return amazonS3.getUrl(amazonConfig.getBucket(),keyName).toString(); | ||
} | ||
public String generatePhotoKeyName(Uuid uuid) { | ||
return amazonConfig.getPhotoPath() + '/' + uuid.getUuid(); | ||
} | ||
} |
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,51 @@ | ||
package hsu.umc.server.config; | ||
|
||
import com.amazonaws.auth.AWSCredentials; | ||
import com.amazonaws.auth.AWSCredentialsProvider; | ||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import jakarta.annotation.PostConstruct; | ||
import lombok.Getter; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@Getter | ||
public class AmazonConfig { | ||
|
||
private AWSCredentials awsCredentials; | ||
|
||
@Value("${cloud.aws.credentials.accessKey}") | ||
private String accesskey; | ||
@Value("${cloud.aws.credentials.secretKey}") | ||
private String secretKey; | ||
@Value("${cloud.aws.region.static}") | ||
private String region; | ||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
|
||
@Value("${cloud.aws.s3.path.photo}") | ||
private String photoPath; | ||
|
||
@PostConstruct | ||
public void init(){ | ||
this.awsCredentials = new BasicAWSCredentials(accesskey, secretKey); | ||
} | ||
@Bean | ||
public AmazonS3 amazonS3() { | ||
AWSCredentials awsCredentials = new BasicAWSCredentials(accesskey, secretKey); | ||
return AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public AWSCredentialsProvider awsCredentialsProvider(){ | ||
return new AWSStaticCredentialsProvider(awsCredentials); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/hsu/umc/server/converter/PhotoConverter.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,12 @@ | ||
package hsu.umc.server.converter; | ||
|
||
import hsu.umc.server.entity.Photo; | ||
import hsu.umc.server.web.dto.PhotoResponseDto; | ||
|
||
public class PhotoConverter { | ||
public static PhotoResponseDto.CreatePhotoResultDto toCreatePhotoResultDto(Photo photo){ | ||
return PhotoResponseDto.CreatePhotoResultDto.builder() | ||
.photoUrl(photo.getPhotoUrl()) | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package hsu.umc.server.entity; | ||
|
||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import lombok.*; | ||
|
||
@Entity | ||
@Builder | ||
@Getter | ||
@AllArgsConstructor(access = AccessLevel.PROTECTED) | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Photo extends BaseEntity { | ||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String photoUrl; | ||
|
||
} |
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,16 @@ | ||
package hsu.umc.server.entity; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
@Entity | ||
@Builder | ||
@Getter | ||
@AllArgsConstructor(access = AccessLevel.PROTECTED) | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Uuid extends BaseEntity{ | ||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY ) | ||
private Long id; | ||
@Column(unique = true) | ||
private String uuid; | ||
} |
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,7 @@ | ||
package hsu.umc.server.repository; | ||
|
||
import hsu.umc.server.entity.Photo; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface PhotoRepository extends JpaRepository<Photo, Long> { | ||
} |
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,7 @@ | ||
package hsu.umc.server.repository; | ||
|
||
import hsu.umc.server.entity.Uuid; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface UuidRepository extends JpaRepository<Uuid, Long> { | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/hsu/umc/server/service/PhotoCommandService.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 hsu.umc.server.service; | ||
|
||
import hsu.umc.server.entity.Photo; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
|
||
public interface PhotoCommandService { | ||
Photo createPhoto(MultipartFile request); | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/hsu/umc/server/service/PhotoCommandServiceImpl.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,41 @@ | ||
package hsu.umc.server.service; | ||
|
||
import hsu.umc.server.aws.s3.AmazonS3Manager; | ||
import hsu.umc.server.entity.Photo; | ||
import hsu.umc.server.entity.Uuid; | ||
import hsu.umc.server.repository.PhotoRepository; | ||
import hsu.umc.server.repository.UuidRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class PhotoCommandServiceImpl implements PhotoCommandService{ | ||
private final AmazonS3Manager s3Manager; | ||
private final UuidRepository uuidRepository; | ||
private final PhotoRepository photoRepository; | ||
@Override | ||
@Transactional | ||
public Photo createPhoto(MultipartFile photoPicture) { | ||
String uuid = UUID.randomUUID().toString(); | ||
Uuid savedUuid = uuidRepository.save(Uuid.builder().uuid(uuid).build()); | ||
String pictureUrl = null; | ||
try { | ||
pictureUrl = s3Manager.uploadFile(s3Manager.generatePhotoKeyName(savedUuid), photoPicture); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
Photo photo = Photo.builder() | ||
.photoUrl(pictureUrl) | ||
.build(); | ||
|
||
return photoRepository.save(photo); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/hsu/umc/server/web/controller/PhotoController.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 hsu.umc.server.web.controller; | ||
|
||
import hsu.umc.server.apipayload.ApiResponse; | ||
import hsu.umc.server.converter.PhotoConverter; | ||
import hsu.umc.server.entity.Photo; | ||
import hsu.umc.server.service.PhotoCommandService; | ||
import hsu.umc.server.web.dto.PhotoResponseDto; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
|
||
@RestController | ||
@RequestMapping("/api/photo") | ||
@RequiredArgsConstructor | ||
@Tag(name="photo",description = "네컷사진 관련 API") | ||
public class PhotoController { | ||
private final PhotoCommandService photoCommandService; | ||
@PostMapping(value = "/",consumes = "multipart/form-data") | ||
@Operation(summary = "네컷 사진 저장", description = "제공된 사진을 저장하고 url을 반환합니다.") | ||
public ApiResponse<PhotoResponseDto.CreatePhotoResultDto> createPhoto( | ||
@RequestPart("photoPicture") MultipartFile photoPicture) { | ||
Photo photo = photoCommandService.createPhoto(photoPicture); | ||
return ApiResponse.onSuccess(PhotoConverter.toCreatePhotoResultDto(photo)); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/hsu/umc/server/web/dto/PhotoResponseDto.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,16 @@ | ||
package hsu.umc.server.web.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
public class PhotoResponseDto { | ||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class CreatePhotoResultDto{ | ||
private String photoUrl; | ||
} | ||
} |
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