function _uploadChunk(fileName, filePath, oAuth2TwoLegged, credentials, progressCallback) {
return new Promise((resolve, reject) => {
let objectApi = new ForgeSDK.ObjectsApi()
let fileState = fs.statSync(filePath)
let fileSize = fileState.size
let chunkSize = 2 * 1024 * 1024
let nbChunks = Math.round(0.5 + fileSize / chunkSize)
let sessionID = 'testviotv4054irt'// + Math.floor(Date.now() / 1000)
for (let index = 0; index < nbChunks; index++) {
let start = index * chunkSize
let end = Math.min(fileSize, (index + 1) * chunkSize) - 1
let contentRange = 'bytes ' + start + '-' + end + '/' + fileSize
let readStream = fs.createReadStream(filePath, { start: start, end: end })
objectApi.uploadChunk(config.defaultBucketKey, fileName, fileSize, contentRange, sessionID, readStream, {}, oAuth2TwoLegged, credentials)
.then(object => {
if (object.statusCode == 202) {
progressCallback(object.body)
} else if (object.statusCode == 202) {
resolve(object.body)
}
}).catch(err => {
resolve(err)
})
}
})
}
首先,你的代码uploadChunk用到fileSize,这是整个文件的大小,而不是某一次分块的大小。另外,需要有一个object.statusCode == 200的状态,这是最后一个分块成功后的状态。
我不太熟悉ES6,所以按照常规JS写写了一段函数,基于Forge Node.js SDK中的测试脚本, 可以替换掉line 155的uploadFile进行测试。