-
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 #98 from wafflestudio/dev
s3 배포 테스트 위한 main merge
- Loading branch information
Showing
6 changed files
with
200 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
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
92 changes: 92 additions & 0 deletions
92
src/main/kotlin/com/example/toyTeam6Airbnb/Image/ImageController.kt
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,92 @@ | ||
package com.example.toyTeam6Airbnb.Image | ||
|
||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RequestParam | ||
import org.springframework.web.bind.annotation.RestController | ||
import org.springframework.web.multipart.MultipartFile | ||
import java.nio.file.Paths | ||
import java.util.Date | ||
|
||
@RestController | ||
@RequestMapping("/api/images") | ||
class ImageController( | ||
private val imageService: ImageService | ||
) { | ||
|
||
// 이미지 업로드 엔드포인트 | ||
@PostMapping("/upload") | ||
fun uploadImage( | ||
@RequestParam("file") file: MultipartFile, | ||
@RequestParam("key") key: String | ||
): ResponseEntity<String> { | ||
return try { | ||
val tempFile = Paths.get(System.getProperty("java.io.tmpdir"), file.originalFilename).toFile() | ||
file.transferTo(tempFile) // MultipartFile을 임시 파일로 저장 | ||
|
||
imageService.uploadFile(tempFile.absolutePath, key) // S3에 업로드 | ||
tempFile.delete() // 임시 파일 삭제 | ||
|
||
ResponseEntity("Image uploaded successfully with key: $key", HttpStatus.OK) | ||
} catch (e: Exception) { | ||
ResponseEntity("Failed to upload image: ${e.message}", HttpStatus.INTERNAL_SERVER_ERROR) | ||
} | ||
} | ||
|
||
@GetMapping("/{key}") | ||
fun getImageSignedUrl( | ||
@PathVariable("key") key: String | ||
): ResponseEntity<String> { | ||
return try { | ||
val expirationDate = Date(System.currentTimeMillis() + 3600_000) // 1시간 유효 | ||
|
||
val signedUrl = imageService.generateSignedUrl( | ||
"https://d3m9s5wmwvsq01.cloudfront.net", | ||
key, | ||
expirationDate | ||
) | ||
|
||
ResponseEntity.ok(signedUrl) | ||
} catch (e: Exception) { | ||
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.body("Failed to generate signed URL: ${e.message}") | ||
} | ||
} | ||
} | ||
|
||
// // 이미지 다운로드 엔드포인트 | ||
// @GetMapping("/download") | ||
// fun downloadImage( | ||
// @RequestParam("key") key: String, | ||
// @RequestParam("destination") destination: String | ||
// ): ResponseEntity<String> { | ||
// return try { | ||
// imageService.downloadFile(key, destination) // S3에서 다운로드 | ||
// ResponseEntity("Image downloaded successfully to: $destination", HttpStatus.OK) | ||
// } catch (e: Exception) { | ||
// ResponseEntity("Failed to download image: ${e.message}", HttpStatus.INTERNAL_SERVER_ERROR) | ||
// } | ||
// } | ||
|
||
// // 이미지를 화면에 표시, 보안 취약 | ||
// @GetMapping("/{key}") | ||
// fun getImage( | ||
// @PathVariable("key") key: String | ||
// ): ResponseEntity<Void> { | ||
// return try { | ||
// // CloudFront 배포 URL | ||
// val cloudFrontUrl = "https://d3m9s5wmwvsq01.cloudfront.net/$key" | ||
// | ||
// // 302 Redirect | ||
// ResponseEntity.status(HttpStatus.FOUND) | ||
// .header("Location", cloudFrontUrl) | ||
// .build() | ||
// } catch (e: Exception) { | ||
// ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
// .body(null) | ||
// } | ||
// } |
92 changes: 92 additions & 0 deletions
92
src/main/kotlin/com/example/toyTeam6Airbnb/Image/ImageService.kt
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,92 @@ | ||
package com.example.toyTeam6Airbnb.Image | ||
|
||
import com.amazonaws.services.cloudfront.CloudFrontUrlSigner | ||
import com.amazonaws.services.cloudfront.util.SignerUtils | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Service | ||
import software.amazon.awssdk.core.sync.RequestBody | ||
import software.amazon.awssdk.regions.Region | ||
import software.amazon.awssdk.services.s3.S3Client | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest | ||
import java.io.File | ||
import java.nio.file.Paths | ||
import java.security.PrivateKey | ||
import java.util.Date | ||
|
||
@Service | ||
class ImageService() { | ||
|
||
@Value("\${cloudfront.private-key}") | ||
private lateinit var privateKey: String | ||
|
||
@Value("\${cloudfront.key-pair-id}") | ||
private lateinit var keyPairId: String | ||
private val s3Client: S3Client = S3Client.builder() | ||
.region(Region.AP_NORTHEAST_2) // 원하는 리전 설정 | ||
.build() | ||
private val bucketName: String = "waffle-team6-storage" | ||
private val cloudFrontUrl: String = "https://d3m9s5wmwvsq01.cloudfront.net" | ||
|
||
// 파일 업로드 메서드 | ||
fun uploadFile(filePath: String, key: String) { | ||
s3Client.putObject( | ||
PutObjectRequest.builder() | ||
.bucket(bucketName) | ||
.key(key) | ||
.build(), | ||
RequestBody.fromFile(Paths.get(filePath)) | ||
) | ||
} | ||
|
||
// // 파일 다운로드 메서드 | ||
// @Transactional | ||
// fun downloadFile(key: String, destination: String) { | ||
// s3Client.getObject( | ||
// GetObjectRequest.builder() | ||
// .bucket(bucketName) | ||
// .key(key) | ||
// .build(), | ||
// Paths.get(destination) | ||
// ) | ||
// } | ||
|
||
// CloudFront signed URL 생성 메서드 | ||
fun generateSignedUrl( | ||
domain: String, | ||
key: String, | ||
expiration: Date | ||
): String { | ||
// privateKey와 keyPairId는 GitHub Secrets에서 가져온 값이 자동으로 주입됨 | ||
return createSignedUrl(domain, key, privateKey, keyPairId, expiration) | ||
} | ||
|
||
// CloudFront 서명 URL 생성 메서드 | ||
// createSignedUrl 메서드 구현 | ||
private fun createSignedUrl( | ||
domain: String, | ||
key: String, | ||
privateKey: String, | ||
keyPairId: String, | ||
expiration: Date | ||
): String { | ||
try { | ||
// 1. 임시 파일 생성 및 비공개 키 쓰기 | ||
val tempFile = File.createTempFile("privateKey", ".pem") | ||
tempFile.deleteOnExit() // 애플리케이션 종료 시 자동 삭제 | ||
tempFile.writeText(privateKey) | ||
|
||
// 2. Private Key 객체 로드 | ||
val privateKeyObj: PrivateKey = SignerUtils.loadPrivateKey(tempFile) | ||
|
||
// 3. CloudFront 서명된 URL 생성 | ||
return CloudFrontUrlSigner.getSignedURLWithCannedPolicy( | ||
"$domain/$key", // 리소스 URL | ||
keyPairId, // Key Pair ID | ||
privateKeyObj, // PrivateKey 객체 | ||
expiration // 만료 시간 | ||
) | ||
} catch (e: Exception) { | ||
throw RuntimeException("Failed to create signed URL: ${e.message}", e) | ||
} | ||
} | ||
} |
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