We’re Still Here to Help (Even Over the Holidays!) - find out more here.
Forum Discussion
Arthur C.9
11 years agoNew member | Level 1
Chunked upload with HTTP requests
Hello,
I am trying to implement chunked upload utilizing HTTP requests and find myself in trouble with chunked upload.
The upload itself seems to work fine, i am able to go through the flow and ...
Arthur C.9
11 years agoNew member | Level 1
Written in scala, so far for testing purposes no other handling is done. Due to the need to have 100% e2e test coverage it was decided against using java SDK (mainly due to having trouble supplying local trusted-certs.jks, seemed easier overall to just do everything with plain HTTP requests)
for debugging purposes there are certain variables made just to see more data, but not used, please disregard that
def chunkedUpload(): Any = {
val fileToUpload = new File(getClass.getClassLoader.getResource("files/app.psd").toURI)
val defaultChunkSize: Int = 4 * 1024 * 1024
val fileInByteArray = Files.readAllBytes(fileToUpload.toPath)
val httpClient = HttpClients.createDefault()
val length = fileInByteArray.length
val chunkingCounter = fileInByteArray.length / defaultChunkSize + 1
def getChunk(byteArray: Array[Byte], counter: Int, chunkSize: Int = defaultChunkSize): HttpEntity = {
val byteArrayLength = byteArray.length
if (counter == 0) {
val multipartEntityBuilder = MultipartEntityBuilder.create()
if (byteArrayLength <= chunkSize) {
val chunk = byteArray
val chunklength = chunk.length
multipartEntityBuilder.addBinaryBody("chunk", chunk)
val requestBody = multipartEntityBuilder.build()
requestBody
}
else {
val chunk = byteArray.slice(0, chunkSize)
val chunklength = chunk.length
multipartEntityBuilder.addBinaryBody("chunk", chunk)
val requestBody = multipartEntityBuilder.build()
requestBody
}
}
else {
val leftoverArray = byteArray.drop(chunkSize)
val leftoverLength = leftoverArray.length
getChunk(leftoverArray, counter - 1, chunkSize)
}
}
def uploadChunk(requestBody: HttpEntity, uploadUrl: String): DBObject = {
val uploadRequest = new HttpPut(uploadUrl)
uploadRequest.setHeader("Authorization", s"Bearer $token")
uploadRequest.setEntity(requestBody)
val response = httpClient.execute(uploadRequest)
val parsedResponse = JSON.parse(EntityUtils.toString(response.getEntity)).asInstanceOf[DBObject]
parsedResponse
}
def commitChunkedUpload(uploadUrl: String, uploadId: String): Any = {
val commitUploadRequest = new HttpPost(uploadUrl)
commitUploadRequest.setHeader("Authorization", s"Bearer $token")
val postParameters = new util.ArrayList[NameValuePair]()
postParameters.add(new BasicNameValuePair("upload_id", uploadId))
postParameters.add(new BasicNameValuePair("overwrite", "true"))
commitUploadRequest.setEntity(new UrlEncodedFormEntity(postParameters))
val commitResponse = httpClient.execute(commitUploadRequest)
val parsedResponse = JSON.parse(EntityUtils.toString(commitResponse.getEntity)).asInstanceOf[DBObject]
parsedResponse
}
def executeChunkedUpload(counter: Int, uploadId: String = ", offset: Long = 0): Any = {
if (counter == chunkingCounter) commitChunkedUpload(
s"https://content.dropboxapi.com/1/commit_chunked_upload/auto/Stuff/add%20pdf.psd", uploadId) //try setting parameters as post params instead of URL
else {
val requestBody = getChunk(fileInByteArray, counter)
val uploadUrl = s"https://content.dropboxapi.com/1/chunked_upload?upload_id=$uploadId&offset=$offset"
val intermediaryResponse = uploadChunk(requestBody, uploadUrl)
val newOffset = intermediaryResponse.get("offset").asInstanceOf[Int]
val rewriteId = intermediaryResponse.get("upload_id").asInstanceOf[String]
executeChunkedUpload(counter + 1, rewriteId, newOffset)
}
}
executeChunkedUpload(0)
}
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!