콘텐츠 API 추가
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package kr.co.vividnext.sodalive.aws.cloudfront
|
||||
|
||||
import com.amazonaws.services.cloudfront.CloudFrontUrlSigner
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.stereotype.Component
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Paths
|
||||
import java.security.KeyFactory
|
||||
import java.security.PrivateKey
|
||||
import java.security.spec.PKCS8EncodedKeySpec
|
||||
import java.util.Date
|
||||
|
||||
@Component
|
||||
class AudioContentCloudFront(
|
||||
@Value("\${cloud.aws.content-cloud-front.host}")
|
||||
private val cloudfrontDomain: String,
|
||||
|
||||
@Value("\${cloud.aws.content-cloud-front.private-key-file-path}")
|
||||
private val privateKeyFilePath: String,
|
||||
|
||||
@Value("\${cloud.aws.content-cloud-front.key-pair-id}")
|
||||
private val keyPairId: String
|
||||
) {
|
||||
fun generateSignedURL(
|
||||
resourcePath: String,
|
||||
expirationTime: Long
|
||||
): String {
|
||||
// Load private key from file
|
||||
val privateKey = loadPrivateKey(privateKeyFilePath)
|
||||
|
||||
// Generate signed URL for resource with custom policy and expiration time
|
||||
|
||||
return CloudFrontUrlSigner.getSignedURLWithCannedPolicy(
|
||||
"https://$cloudfrontDomain/$resourcePath", // Resource URL
|
||||
keyPairId, // CloudFront key pair ID
|
||||
privateKey, // CloudFront private key
|
||||
Date(System.currentTimeMillis() + expirationTime) // Expiration date
|
||||
)
|
||||
}
|
||||
|
||||
private fun loadPrivateKey(resourceName: String): PrivateKey {
|
||||
val path = Paths.get(resourceName)
|
||||
val bytes = Files.readAllBytes(path)
|
||||
val keySpec = PKCS8EncodedKeySpec(bytes)
|
||||
val keyFactory = KeyFactory.getInstance("RSA")
|
||||
return keyFactory.generatePrivate(keySpec)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user