【问题标题】:Why I get 416 error when uploading +100mb files to Objects Api为什么将 +100mb 文件上传到 Objects Api 时出现 416 错误
【发布时间】:2021-03-16 13:05:25
【问题描述】:

这是我第一次尝试使用 Autodesk 上传大于 100mb 的 3DModel,我收到 416 错误 Chunk-Length mismatch (found: 52566915, required:52428801)

我正在使用来自 forge-apis 的 node.js 和 ObjectsApi

我很确定我的代码有问题,因为我已经阅读了docs 和其他类似问题,所以我不太明白该怎么做。

我的代码:

/routes/upload/uploadLarge.js

import { UploadLarge } from '../../TS/routes/upload'
import fs from 'fs'
import { twoLeggedOauth } from '../../auth/forgeAuth'
import { ObjectsApi } from 'forge-apis'
const objectApi = new ObjectsApi()

const uploadLarge: UploadLarge = (req, res) => {
  console.log('bucket key', req.params.bucket)

    const totalBytes = fs.statSync(req.file.path).size
    const bucketKey = req.params.bucket
    const fileName = req.file.originalname
    const sessionId = 'test-session-id'

  fs.readFile(req.file.path, async (err, filecontent) => {
   try {
     const contentRange = `bytes 0-${totalBytes / 2}/${totalBytes}`

     const { fToken, oauth } = await twoLeggedOauth()

     console.log('chunks', contentRange)
     const upload = await objectApi.uploadChunk(
       bucketKey,
       fileName,
       totalBytes,
       contentRange,
       sessionId,
       filecontent,
       {},
       oauth,
       fToken
     )
     res.send(upload.body)
   } catch (error) {
     res.send('Failed to create a new object in the bucket')
   }
  })
}

路由/上传/index.js

import multer from 'multer' // https://github.com/axios/axios/issues/1221
import { Router } from 'express'
import { uploadLarge } from './uploadLarge'
import { uploadSmall } from './uploadSmall'

const uploadRouter = Router()
const upload = multer({ dest: 'tmp/' })


uploadRouter.post(
  '/upload-large/:token/:bucket',
  upload.single('fileToUpload'),
  uploadLarge
)

【问题讨论】:

    标签: node.js autodesk-forge autodesk-viewer autodesk-data-management


    【解决方案1】:

    您只需阅读文件的不同部分...

    const readStream = fs.createReadStream(
      file.path, { start, end }
    )
    

    ...并使用 uploadChunk() 函数上传每个部分,使用相同的 sessionIdrange 遵循规则“ bytes [start]-[end]/[full file size]”,例如“字节 0-9/20”和“字节 10-19/20

    这是一篇关于此主题的文章,还讨论了您遇到的错误: https://forge.autodesk.com/blog/nailing-large-files-uploads-forge-resumable-api

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-05
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      相关资源
      最近更新 更多