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進行測試。