https://docs.aws.amazon.com/AmazonS3/latest/userguide/example_s3_Scenario_PresignedUrl_section.html
Create a presigned URL for Amazon S3 using an AWS SDK - Amazon Simple Storage Service
This documentation is for an SDK in preview release. The SDK is subject to change and should not be used in production.
docs.aws.amazon.com
https://docs.aws.amazon.com/ko_kr/AmazonS3/latest/userguide/S3OutpostsShareObjectPresignedURL.html
미리 서명된 URL을 사용하여 객체 공유 - Amazon Simple Storage Service
미리 서명된 URL을 사용하여 객체 공유 버킷 정책을 업데이트하지 않고 Outpost에 로컬로 저장된 객체에 한시적 액세스 권한을 부여하려면 미리 서명된 URL을 사용할 수 있습니다. 버킷 소유자는 미
docs.aws.amazon.com
두 번째 페이지의 JAVA 코드 참고
1. 버킷 생성
@Configuration
class AmazonWebServiceConfiguration {
private val accessKey: String? = ""
private val secretKey: String? = ""
private val region: String? = ""
@Bean
fun amazonS3Client(): AmazonS3Client {
val awsCredentials = BasicAWSCredentials(accessKey, secretKey)
return AmazonS3ClientBuilder.standard().withRegion(region).withCredentials(AWSStaticCredentialsProvider(awsCredentials)).build() as AmazonS3Client
}
@Bean
fun amazonS3(): AmazonS3 {
val awsCredentials = BasicAWSCredentials(accessKey, secretKey)
return AmazonS3Client.builder().withRegion(region).withCredentials(AWSStaticCredentialsProvider(awsCredentials)).build()
}
}
2. Service 코드 짜기
@Service
class PresignedService {
@Autowired
lateinit var amazonS3: AmazonS3Client
private var bucket: String = ""
fun getFileDownloadUrl(fileName: String): String {
val expiration = Date()
var expTimeMillis = Instant.now().toEpochMilli()
expTimeMillis += 1000 * 60 * 30 //30분
expiration.time = expTimeMillis
val generatePresignedUrlRequest = GeneratePresignedUrlRequest(bucket, fileName).withMethod(HttpMethod.GET).withExpiration(
expiration
)
generatePresignedUrlRequest.addRequestParameter(Headers.S3_CANNED_ACL, CannedAccessControlList.PublicRead.toString())
val url: URL = amazonS3.generatePresignedUrl(generatePresignedUrlRequest)
return url.toString()
}
}
s3 버킷에 올라간 이미지를 일정 시간 동안 접근가능한 URL을 생성할 수 있었다
일정시간이 지난 후에는 접근이 불가능 하다
접근 가능한 시간이 지난 URL을 접속하면 위와 같이 뜬다!
'Kotlin' 카테고리의 다른 글
프로그래머스 - 옹알이(1) [Kotlin] (0) | 2025.02.06 |
---|---|
[QueryDSL] stringTemplate, dateTemplate로 날짜 포맷 하기, 날짜 더하기 (0) | 2023.07.17 |
[Querydsl] 멀티 DB(Multi DB) 설정 하기, DB 2개 이상 연결 (0) | 2023.07.17 |
[Spring Boot] Scheduled사용하여 특정 시간마다 동작하는 코드 구현 (0) | 2023.05.19 |
[Spring Boot] 스프링 부트 시작 - 프로젝트 만들기 (0) | 2023.02.19 |
댓글