• 1
  • 新人请关照

为什么Autodesk Forge nodejs API分块上传最后一块上传之后返回值依然是202?

我调用Forge的Nodejs API,使用uploadChunk接口上传大模型,每个块分成2m大小上传,最后一个上传之后返回值依然是202,按文档说明不应该是200吗?而且得不到返回的ObjectKey等值。下面是我的代码:

async function uploadModel(fileInfos, options) {
    const chunkSize = (options.chunkSize || 2) * 1024 * 1024; //碎片大小,默认2m
    const chunkCount = Math.round(0.5 + fileInfos.size / chunkSize);
    let details = null;
    for (let i = 0; i < chunkCount; i++) {
        const start = i * chunkSize;
        const end = Math.min(fileInfos.size, (i + 1) * chunkSize);
        details = null;
        while (!details) {
            details = await uploadChunk(fileInfos, start, end);
        }
        options.callback && options.callback(i + 1, chunkCount);
    }
    console.log(details);
    //await translateObject(details.objectId, options);
}
async function uploadChunk(fileInfos, start, end) {
    const credentials = await getInternalToken();
    const oauth2client = getClient();
    const contentLength = end - start; //后面用的slice方法不包括end索引指向的字节
    const contentRange = `bytes ${start}-${end-1}/${fileInfos.size}`;
    console.log(contentRange);
    const buffer = fileInfos.data.slice(start, end); //slice方法不包括end索引指向的字节
    const sessionId = md5(buffer);
    try {
        //https://github.com/Autodesk-Forge/forge-api-nodejs-client/blob/master/docs/ObjectsApi.md#uploadchunk
        let details = await new ObjectsApi().uploadChunk(bucketKey, fileInfos.name, contentLength, contentRange, sessionId, buffer, {}, oauth2client, credentials);
        return details;
    } catch (err) {
        console.log(err);
        return null;
    }
}

调用之后最后得到的结果是:
`{
statusCode: 202,
headers: {

'accept-ranges': 'bytes',
'access-control-allow-headers': 'Authorization, Accept-Encoding, Range, Content-Type',
'access-control-allow-methods': 'GET',
'access-control-allow-origin': '*',
'content-range': 'bytes 427819008-429560714/429560715',
date: 'Tue, 30 Jun 2020 04:42:00 GMT',
'x-ads-chunk-sha1': '84d5fee9e9ec849cb1a9a5931b3da6750be8af08',
'content-length': '0',
connection: 'Close'

},
body: ''
}`

是还有别的参数需要设置吗?

阅读 78
评论
    1 个回答
    • 1
    • 新人请关照

    找到问题所在了,是一个文件的所有碎片都用同一个sessionId,而不是一个碎片就用一个sessionId。但是接下来的问题是:怎么删除forge服务端之前上传的无用碎片?

      撰写回答

      登录后参与交流、获取后续更新提醒