autodesk-forge - Autodesk Forge:文件上传恢复总是返回 202,即使是最终块
问题描述
我正在尝试使用端点 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();
}
}
解决方案
我在这里发布了一个 Node.js 示例。回到你的代码,你需要像这样修改它:
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 组合在一起。
推荐阅读
- php - 两个不同的关联数组的PHP二维关联数组如何foreach只是两个中的一个
- javascript - Spark AR studio,在 v100+ 中获取对象的位置
- c++ - 在 gcc/linux 中运行 C++ 上的函数?
- javascript - 使美丽的微调器的中心透明
- javascript - 如何避免从事件监听器重新渲染无限循环(React)
- swift - 如何持久化计算属性 SwiftUI 2.0 Xcode 12.3
- svg - 具有巨大尺寸的 SVG 字形
- c# - 我创建了一个无限循环,但我不知道为什么
- java - 如何修复项目构建路径错误?我无法为 Java 8/9 计算器应用程序导入 java.lang.math
- c++ - 可变模板类型