-
Notifications
You must be signed in to change notification settings - Fork 2
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 #30 from CSID-DGU/develop
โจ [Feat] s3 ์ค์
- Loading branch information
Showing
4 changed files
with
158 additions
and
0 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,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; | ||
} | ||
} |
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,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()); | ||
} | ||
} | ||
|
||
} |
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,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); | ||
} | ||
} |