Skip to content
IntelliProve

Multipart/form-data Upload

Information
Description Code snippet for fetching a pre-signed upload URL and uploading ZIP file using multipart/form-data.
Purpose The function:
Builds the pre-signed upload POST request
Uploads the ZIP file as multipart/form-data
Language Kotlin
  • zipFile: File
    The ZIP file containing the JPG images.
  • uploadUrl: String
    The base S3 endpoint URL provided by the /request-url API endpoint. It is typically in the format of https://<bucket-name>.s3.amazonaws.com/.
  • fields: Map<String, String>
    This is a map of key-value pairs that must be included in the multipart request’s form data.
  • success: Boolean
    Value is true when the upload succeeded.
import okhttp3.*
import java.io.File
fun uploadZipToPresignedUrl(zipFile: File, uploadUrl: String, fields: Map<String, String>): Boolean {
val client = OkHttpClient()
val multipartBodyBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
// Add all required AWS fields
for ((key, value) in fields) {
multipartBodyBuilder.addFormDataPart(key, value)
}
// Add file as "file" field
multipartBodyBuilder.addFormDataPart(
"file", zipFile.name,
RequestBody.create(MediaType.parse("application/zip"), zipFile)
)
val request = Request.Builder()
.url(uploadUrl)
.post(multipartBodyBuilder.build())
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
println("Upload failed with code: ${response.code}")
return false
}
return true
}
}
  • The above code snippet was AI-generated and not yet tested on syntax or runtime errors. It merely serves as an example implementation. Be cautious when using this snippet.
  • It’s important to add the AWS fields first and add the “file” key as latest.