Skip to content

Commit

Permalink
Merge pull request #30 from CSID-DGU/develop
Browse files Browse the repository at this point in the history
โœจ [Feat] s3 ์„ค์ •
  • Loading branch information
saokiritoni authored Nov 27, 2024
2 parents 1290705 + fb7a65f commit 2e23f86
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ dependencies {

// Swagger
implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.2.0'

// S3
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
}


Expand Down
36 changes: 36 additions & 0 deletions src/main/java/dongguk/osori/config/S3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dongguk.osori.config;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
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;
import org.springframework.context.annotation.Primary;

@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
@Primary
public BasicAWSCredentials awsCredentialsProvider(){
BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey);
return basicAWSCredentials;
}

@Bean
public AmazonS3 amazonS3() {
AmazonS3 s3Builder = AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentialsProvider()))
.build();
return s3Builder;
}
}
32 changes: 32 additions & 0 deletions src/main/java/dongguk/osori/domain/s3/FileController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dongguk.osori.domain.s3;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileController {

private final FileService fileService;

@PostMapping("/{prefix}/presigned-url")
public ResponseEntity<String> getPresignedUrl(@PathVariable String prefix, @RequestBody Map<String, String> requestBody) {
String fileName = requestBody.get("imageName");
if (fileName == null || fileName.isEmpty()) {
return ResponseEntity.badRequest().body("The 'imageName' field is required.");
}

try {
String presignedUrl = fileService.getPreSignedUrl(prefix, fileName);
return ResponseEntity.ok(presignedUrl);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to generate presigned URL: " + e.getMessage());
}
}

}
87 changes: 87 additions & 0 deletions src/main/java/dongguk/osori/domain/s3/FileService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package dongguk.osori.domain.s3;

import com.amazonaws.HttpMethod;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.Headers;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.net.URL;
import java.util.Date;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class FileService {
@Value("${cloud.aws.s3.bucket}")
private String bucket;

private final AmazonS3 amazonS3;

/**
* presigned url ๋ฐœ๊ธ‰
* @param prefix ๋ฒ„ํ‚ท ๋””๋ ‰ํ† ๋ฆฌ ์ด๋ฆ„
* @param fileName ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ „๋‹ฌํ•œ ํŒŒ์ผ๋ช… ํŒŒ๋ผ๋ฏธํ„ฐ
* @return presigned url
*/
public String getPreSignedUrl(String prefix, String fileName) {
if(StringUtils.hasText(prefix)) {
fileName = createPath(prefix, fileName);
}

GeneratePresignedUrlRequest generatePresignedUrlRequest = getGeneratePreSignedUrlRequest(bucket, fileName);
URL url = amazonS3.generatePresignedUrl(generatePresignedUrlRequest);
return url.toString();
}

/**
* ํŒŒ์ผ ์—…๋กœ๋“œ์šฉ(PUT) presigned url ์ƒ์„ฑ
* @param bucket ๋ฒ„ํ‚ท ์ด๋ฆ„
* @param fileName S3 ์—…๋กœ๋“œ์šฉ ํŒŒ์ผ ์ด๋ฆ„
* @return presigned url
*/
private GeneratePresignedUrlRequest getGeneratePreSignedUrlRequest(String bucket, String fileName) {
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(bucket, fileName)
.withMethod(HttpMethod.PUT)
.withExpiration(getPreSignedUrlExpiration());
generatePresignedUrlRequest.addRequestParameter(
Headers.S3_CANNED_ACL,
CannedAccessControlList.PublicRead.toString());
return generatePresignedUrlRequest;
}

/**
* presigned url ์œ ํšจ ๊ธฐ๊ฐ„ ์„ค์ •
* @return ์œ ํšจ๊ธฐ๊ฐ„
*/
private Date getPreSignedUrlExpiration() {
Date expiration = new Date();
long expTimeMillis = expiration.getTime();
expTimeMillis += 1000 * 60 * 2;
expiration.setTime(expTimeMillis);
return expiration;
}

/**
* ํŒŒ์ผ ๊ณ ์œ  ID๋ฅผ ์ƒ์„ฑ
* @return 36์ž๋ฆฌ์˜ UUID
*/
private String createFileId() {
return UUID.randomUUID().toString();
}

/**
* ํŒŒ์ผ์˜ ์ „์ฒด ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑ
* @param prefix ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ
* @return ํŒŒ์ผ์˜ ์ „์ฒด ๊ฒฝ๋กœ
*/
private String createPath(String prefix, String fileName) {
String fileId = createFileId();
return String.format("%s/%s", prefix, fileId + fileName);
}
}

0 comments on commit 2e23f86

Please sign in to comment.