Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate S3 Presigned-url #86

Merged
merged 4 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dependencies {
// aws - s3
implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-aws', version: '2.2.6.RELEASE'
testImplementation group: 'io.findify', name: 's3mock_2.12', version: '0.2.6'
testImplementation group: 'software.amazon.awssdk', name: 's3', version: '2.20.151'

// sentry
implementation 'io.sentry:sentry-spring-boot-starter-jakarta:6.27.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.swmaestro.repl.gifthub.util.HttpJsonHeaders;
import org.swmaestro.repl.gifthub.util.JwtProvider;
import org.swmaestro.repl.gifthub.util.Message;
import org.swmaestro.repl.gifthub.util.StatusEnum;
import org.swmaestro.repl.gifthub.vouchers.dto.PresignedUrlResponseDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherSaveRequestDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUpdateRequestDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUseRequestDto;
Expand All @@ -44,21 +43,21 @@ public class VoucherController {
private final StorageService storageService;
private final JwtProvider jwtProvider;

@PostMapping("/images")
@Operation(summary = "Voucher 이미지 등록 메서드", description = "클라이언트에서 요청한 기프티콘 이미지를 Amazon S3에 저장하기 위한 메서드입니다.")
@GetMapping("/images")
@Operation(summary = "Voucher 이미지 등록 메서드", description = "클라이언트에서 요청한 기프티콘 이미지를 Amazon S3에 저장하기 위한 메서드입니다. 요청 시 S3 PreSigned URL이 반환됩니다.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "기프티콘 이미지 등록 성공"),
@ApiResponse(responseCode = "400(404)", description = "존재하지 않는 브랜드 조회 시도")
@ApiResponse(responseCode = "200", description = "성공적으로 S3 Presigned URL 반환"),
})
public ResponseEntity<Message> saveVoucherImage(@RequestPart("image_file") MultipartFile imageFile) throws IOException {
return new ResponseEntity<>(
Message.builder()
.status(StatusEnum.OK)
.message("기프티콘 이미지가 성공적으로 등록되었습니다!")
.data(storageService.save(voucherDirName, imageFile))
.build(),
new HttpJsonHeaders(),
HttpStatus.OK
public ResponseEntity<Message> saveVoucherImage() throws IOException {
PresignedUrlResponseDto presignedUrlResponseDto = PresignedUrlResponseDto.builder()
.presignedUrl(storageService.getPresignedUrlForSaveVoucher("voucher", "PNG"))
.build();

return ResponseEntity.ok(Message.builder()
.status(StatusEnum.OK)
.message("성공적으로 S3 Presigned URL 반환되었습니다!")
.data(presignedUrlResponseDto)
.build()
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.swmaestro.repl.gifthub.vouchers.dto;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class PresignedUrlResponseDto {
private String presignedUrl;

@Builder
public PresignedUrlResponseDto(String presignedUrl) {
this.presignedUrl = presignedUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.swmaestro.repl.gifthub.vouchers.dto.S3FileDto;

import com.amazonaws.HttpMethod;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;

Expand Down Expand Up @@ -57,4 +60,12 @@ private String getUUidFileName(String fileName) {
public String getDefaultImagePath(String dirName) {
return "http://" + bucketName + "/" + dirName + "/" + defaultImageFile;
}

public String getPresignedUrlForSaveVoucher(String dirName, String extension) {
String key = dirName + "/" + UUID.randomUUID().toString() + "." + extension;
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.PUT)
.withExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 5));
return amazonS3Client.generatePresignedUrl(generatePresignedUrlRequest).toString();
}
}