Autodesk Forge: File upload resumable returns always 202 even for final chunk

231 Views Asked by At

I am trying to upload file using end point buckets/:bucketKey/objects/:objectName/resumable I am always getting the response code 202 even for the final chunk. As per documentation i should receive response 200 with some urn details for the final upload. How to solve this? For testing i was using 17 MB file. But my main agenda is to upload larger files. Below are my code:

byte[] bytes = uploadObjectRequest.getInputStream().readAllBytes();
    int fileSize = bytes.length;    
            
    System.out.println("File size in bytes: "+ fileSize);
    int chunkSize = 5 * 1024 * 1024 ;
    int nbChunks = (fileSize / chunkSize) + 1;
    try(ByteArrayInputStream isReader = new ByteArrayInputStream(bytes)){
        for(int i = 0; i < nbChunks; i++){
            int start = i * chunkSize;
            int end = Math.min(fileSize, (i + 1) * chunkSize) - 1;
            String range = "bytes " + start + "-" + end + "/" + fileSize;

            // length of this piece
            int contentLength = end - start + 1; 
            byte[] buffer = new byte[contentLength];
            
            int count = isReader.read(buffer, 0, contentLength);
            ByteArrayInputStream is = new ByteArrayInputStream(buffer);

            uploadObjectRequest.setContentLength(contentLength);
            uploadObjectRequest.setContentRange(range);
            String sessionId = UUID.randomUUID().toString();
            uploadObjectRequest.setSessionId(sessionId);
            uploadObjectRequest.setInputStream(is);
            System.out.println(String.format("For Chunk %s contentLength %s, contentRange %s, sessionId %s", i, contentLength, range, sessionId));
            HttpResponse res = datamanagementAPI.uploadObjsInChunk(uploadObjectRequest, authenticator);
            int status = res.getStatusLine().getStatusCode();
        }
    }
1

There are 1 best solutions below

3
On BEST ANSWER

I got an example in Node.js posted here. Back to your code, you need to modify it like this:

    string sessionId = UUID.randomUUID().toString()
    byte[] bytes = uploadObjectRequest.getInputStream().readAllBytes();
    int fileSize = bytes.length;    
        
    System.out.println("File size in bytes: "+ fileSize);
    int chunkSize = 5 * 1024 * 1024 ;

    if ( fileSize < chunkSize ) {
        // Do a standard upload since OSS will reject any chunk less than 2Mb
        // At the same time, using the chunk approach for small files has a cost.
        // So let's say 5Mb is our limit.
        ...
        return;
    }

    int nbChunks = (int) Math.floor(fileSize / chunkSize);
    if ((fileSize % chunkSize) != 0)
        nbChunks++;
    try(ByteArrayInputStream isReader = new ByteArrayInputStream(bytes)){
        for(int i = 0; i < nbChunks; i++){
            int start = i * chunkSize;
            int end = start + chunkSize - 1;
            if (end > fileSize - 1)
                end = fileSize - 1;
            String range = "bytes " + start + "-" + end + "/" + fileSize;

            // length of this piece
            int contentLength = end - start + 1; 
            byte[] buffer = new byte[contentLength];
            
            int count = isReader.read(buffer, 0, contentLength);
            ByteArrayInputStream is = new ByteArrayInputStream(buffer);

            uploadObjectRequest.setContentLength(contentLength);
            uploadObjectRequest.setContentRange(range);
            String sessionId = sessionId;
            uploadObjectRequest.setSessionId(sessionId);
            uploadObjectRequest.setInputStream(is);
            System.out.println(String.format("For Chunk %s contentLength %s, contentRange %s, sessionId %s", i, contentLength, range, sessionId));
            HttpResponse res = datamanagementAPI.uploadObjsInChunk(uploadObjectRequest, authenticator);
            int status = res.getStatusLine().getStatusCode();
        }
    }

After talking, note that sessionId should be the same for all chunk upload. Otherwise, OSS cannot combine all chunks together.