Skip to content

Instantly share code, notes, and snippets.

@wittawasw
Last active February 1, 2024 09:40
Show Gist options
  • Save wittawasw/1970981c2e46dd326623ab4a67f28a9d to your computer and use it in GitHub Desktop.
Save wittawasw/1970981c2e46dd326623ab4a67f28a9d to your computer and use it in GitHub Desktop.
plugins {
kotlin("jvm") version "1.9.21"
id("application")
}
repositories {
mavenCentral()
}
val kotlinSdkVersion = "1.0.41"
val smithyKotlinVersion = "1.0.10"
dependencies {
implementation("aws.sdk.kotlin:s3:$kotlinSdkVersion")
implementation("aws.sdk.kotlin:s3control:$kotlinSdkVersion")
implementation("aws.sdk.kotlin:sts:$kotlinSdkVersion")
implementation("aws.sdk.kotlin:secretsmanager:$kotlinSdkVersion")
implementation("aws.smithy.kotlin:http-client-engine-okhttp:$smithyKotlinVersion")
implementation("aws.smithy.kotlin:http-client-engine-crt:$smithyKotlinVersion")
implementation("aws.smithy.kotlin:aws-signing-crt:$smithyKotlinVersion")
implementation("aws.smithy.kotlin:http-auth-aws:$smithyKotlinVersion")
implementation("com.squareup.okhttp3:okhttp:4.12.0")
implementation("org.apache.logging.log4j:log4j-slf4j2-impl:2.20.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1")
implementation("com.google.code.gson:gson:2.10")
implementation("com.fasterxml.jackson.core:jackson-databind:2.14.2")
}
application {
mainClass.set("com.kotlin.s3.MainKt")
}
package com.kotlin.s3
import aws.sdk.kotlin.services.s3.S3Client
import aws.sdk.kotlin.services.s3.model.CreateBucketRequest
import aws.sdk.kotlin.services.s3.model.DeleteBucketRequest
import aws.sdk.kotlin.services.s3.model.DeleteObjectRequest
import aws.sdk.kotlin.services.s3.model.GetObjectRequest
import aws.sdk.kotlin.services.s3.model.PutObjectRequest
import aws.sdk.kotlin.services.s3.presigners.presignGetObject
import aws.sdk.kotlin.services.s3.presigners.presignPutObject
import aws.sdk.kotlin.services.s3.waiters.waitUntilBucketExists
import aws.sdk.kotlin.services.s3.waiters.waitUntilBucketNotExists
import aws.sdk.kotlin.services.s3.waiters.waitUntilObjectExists
import aws.sdk.kotlin.services.s3.waiters.waitUntilObjectNotExists
import aws.smithy.kotlin.runtime.auth.awssigning.AwsSignatureType
import aws.smithy.kotlin.runtime.auth.awssigning.AwsSigningAlgorithm
import aws.smithy.kotlin.runtime.auth.awssigning.crt.CrtAwsSigner
import aws.smithy.kotlin.runtime.content.ByteStream
import aws.smithy.kotlin.runtime.http.request.HttpRequest
import aws.smithy.kotlin.runtime.time.Instant
import kotlinx.coroutines.runBlocking
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import java.net.URL
import java.util.UUID
import kotlin.system.exitProcess
import kotlin.time.Duration.Companion.hours
fun main(): Unit = runBlocking {
// Setup these ENVs AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN
val s3 = S3Client.fromEnvironment() { region = "ap-southeast-1" }
val bucketName = "bucket"
val keyName = "key"
println(getObjectPresigned(s3, bucketName, keyName))
exitProcess(1)
}
suspend fun getObjectPresigned(s3: S3Client, bucketName: String, keyName: String): String {
// Create a GetObjectRequest.
val unsignedRequest = GetObjectRequest {
bucket = bucketName
key = keyName
}
// Presign the GetObject request.
val presignedRequest = s3.presignGetObject(unsignedRequest, 24.hours)
return presignedRequest.url.toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment