autodesk-forge - 以node.js 斷點續傳將模型上傳到Autodesk Forge,但api無回應?

浏览:19日期:2022-09-16

问题描述

以下code測試,api沒有回應說目前上傳到那段,就直接timeout。再麻煩檢視程序是否有誤。謝謝!

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 * chunkSizelet end = Math.min(fileSize, (index + 1) * chunkSize) - 1let contentRange = ’bytes ’ + start + ’-’ + end + ’/’ + fileSizelet 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) }) }})

}

问题解答

回答1:

首先,你的代码uploadChunk用到fileSize,这是整个文件的大小,而不是某一次分块的大小。另外,需要有一个object.statusCode == 200的状态,这是最后一个分块成功后的状态。

我不太熟悉ES6,所以按照常规JS写写了一段函数,基于Forge Node.js SDK中的测试脚本, 可以替换掉line 155的uploadFile进行测试。

//随机字串,形成session idfunction randomString(length, chars) { var result = ’’; for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)]; return result;}//分块上载var resumableUploadFile = function(bucketKey, filePath, fileName){ console.log('**** Resumable Uploading file. bucket:'+ bucketKey + ' filePath:'+filePath); return new Promise(function(resolve, reject) { fs.stat(fileName, function (err, stats) {if (err) { reject(err); return;}var fileSize = stats.size ; var chunkSize = CHUNK_SIZE * 1024 * 1024 ; var nbChunks = Math.round (0.5 + fileSize / chunkSize) ; var rString = randomString(32, ’0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’); var sessionID = ’forge-xiaodong-test-’ + rString; function unitPromise(n, chunkSize) { return new Promise(function(resolve, reject){ var start = index * chunkSize;var end = Math.min(fileSize, (index + 1) * chunkSize) - 1;var contentRange = ’bytes ’ + start + ’-’ + end + ’/’ + fileSize;var length = end - start + 1; console.log('**** 分块上载:'+ start + ' end:'+end ); var readStream = fs.createReadStream(filePath+fileName, { start: start, end: end });objectsApi.uploadChunk(bucketKey, fileName, length, contentRange, sessionID, readStream, {}, oAuth2TwoLegged, oAuth2TwoLegged.getCredentials()).then( function(res){if(res.statusCode == 202){ console.log('*********分块上载成功!'); //continue the next chunk resolve(res);}else if(res.statusCode == 200){ //last chunk console.log('********最后一块上载成功!'); resolve(res);} },function(err){console.log('*********此分块上载失败!'+err.statusCode);reject(err); }); }); }//unit promise functionvar uploadpromises = [];for( var index=0;index<nbChunks;index++) { uploadpromises.push(unitPromise(index,chunkSize)); } Promise.all(uploadpromises) .then(function(data){resolve(data); }) .catch(function(err){ reject(err);}); }); }); };

相关文章: