【问题标题】:Autodesk Forge: File upload resumable returns always 202 even for final chunkAutodesk Forge:文件上传恢复总是返回 202,即使是最终块
【发布时间】:2021-03-21 14:48:37
【问题描述】:

我正在尝试使用端点 buckets/:bucketKey/objects/:objectName/resumable 上传文件 即使是最后一块,我也总是得到响应代码 202。根据文档,我应该收到响应 200,其中包含一些最终上传的骨灰盒详细信息。如何解决这个问题?为了测试,我使用了 17 MB 文件。但我的主要议程是上传更大的文件。 以下是我的代码:

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();
        }
    }

【问题讨论】:

    标签: autodesk-forge autodesk-data-management


    【解决方案1】:

    我在 Node.js 中发布了一个示例 here。回到你的代码,你需要像这样修改它:

        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();
            }
        }
    

    说完,注意sessionId对于所有的chunk上传都应该是一样的。否则,OSS 无法将所有的 chunk 组合在一起。

    【讨论】:

    • 我看不出这段代码有什么不同,因为这也会计算相同的块大小、块数等数字。但我仍然使用了你共享的代码并得到了相同的结果。我收到了所有块的响应代码 202(对于最终块也是如此),但它应该为最终块返回 200。
    • 我得到一个 200 的代码,如果最后一个卡盘是 202,这意味着没有收到所有字节。我确实修改了您的代码以反映我工作正常的代码。你能和我分享你的代码/项目吗,我在这里测试和调试它。我的电子邮件地址是 Autodesk dot com 的 cyrille
    • 问题已解决:所有块的会话 id 应该相同
    猜你喜欢
    • 2017-01-12
    • 2013-01-21
    • 2021-10-25
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 2021-09-01
    相关资源
    最近更新 更多