-
-
Save waliahimanshu/60301b107801a63f1f5140db636a5d96 to your computer and use it in GitHub Desktop.
KTOR upload file to AWS S3
This file contains hidden or 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
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