Skip to content

Instantly share code, notes, and snippets.

@waliahimanshu
Created March 31, 2025 14:09
Show Gist options
  • Save waliahimanshu/60301b107801a63f1f5140db636a5d96 to your computer and use it in GitHub Desktop.
Save waliahimanshu/60301b107801a63f1f5140db636a5d96 to your computer and use it in GitHub Desktop.
KTOR upload file to AWS S3
import io.ktor.client.plugins.onUpload
import io.ktor.client.request.forms.MultiPartFormDataContent
import io.ktor.client.request.forms.formData
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.bodyAsText
import io.ktor.http.Headers
import io.ktor.http.HttpHeaders
import kotlinx.coroutines.runBlocking
import java.io.File
public fun main() {
runBlocking {
val client = getGetHttpClient()
val response: HttpResponse =
client.post("https://large-ingest-bucket-xxxxxx.s3.amazonaws.com/") {
setBody(
MultiPartFormDataContent(
// form data from
formData {
append(
"key",
"xxxx.dat"
)
append("AWSAccessKeyId", "...")
append(
"x-amz-security-token",
"..."
)
append(
"policy",
"...."
)
append("signature", "....")
append("file",
File("/path_to_your_zip_file").readBytes(),
Headers.build {
append(HttpHeaders.ContentType, "application/zip")
})
}
)
)
onUpload { bytesSentTotal, contentLength ->
println("Sent $bytesSentTotal bytes from $contentLength")
}
}
println(response.bodyAsText())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment