简体   繁体   中英

Nextjs - Autodesk Forge upload to S3 bucket

I'm trying to upload a file to an s3 bucket as per this guide. https://forge.autodesk.com/en/docs/model-derivative/v2/tutorials/translate-to-obj/about-this-tutorial/ and I'm stuck on step 4 - finalize upload. I'm getting failed with a status code of 400 when calling await finalizeUpload() in the handler.put method.

Here is the api route I am using

const upload = multer({
  storage: multer.diskStorage({
    destination: './public/uploads',
    filename: (req, file, cb) => cb(null, file.originalname),
  }),
})

handler.use(upload.single('model'))

handler.put(async (req, res) => {
  const { _id: projectId } = req.query
  const token = await getInternalToken()
  const { access_token } = token
  const file = req.file
  const objectKey = file.originalname

  try {
    const projectsCollection = await getProject(projectId)
    const bucketKey = projectsCollection.project.bucket.bucketKey

    const signeds3upload = await obtainSignedUrl(bucketKey, objectKey, access_token)
    const { uploadKey, urls } = signeds3upload

    await uploadFile(urls, file.path)
    await finalizeUpload(bucketKey, objectKey, uploadKey, access_token)

    res.status(201).json({ bucketKey, objectKey, uploadKey })
  } catch (error) {
    console.log(error)
  }
})

Here are the functions for steps 2 - 4

const obtainSignedUrl = async (bucketKey, objectKey, token) => {
  const payload = {
    ossbucketKey: bucketKey,
    ossSourceFileObjectKey: objectKey,
    access: 'full',
    policyKey: 'transient',
  }
  const config = {
    method: 'get',
    url: `https://developer.api.autodesk.com/oss/v2/buckets/${bucketKey}/objects/${objectKey}/signeds3upload?minutesExpiration=30`,
    headers: {
      ContentType: 'application/json',
      Authorization: `Bearer ${token}`,
    },
  }
  try {
    let res = await axios(config, payload)
    return res.data
  } catch (error) {
    if (error.response) {
      console.log('response Error obtainSignedUrl')
    } else if (error.request) {
      console.log('Request Error')
    } else if (error.message) {
      console.log('Message Error')
    }
  }
}

async function uploadFile(signed_upload_url, path_to_file) {
  console.log(path_to_file)
  const config = {
    method: 'put',
    url: signed_upload_url,
    headers: {
      ContentType: 'application/octet-stream',
    },
  }
  try {
    let res = await axios(config, path_to_file)
    return res.data
  } catch (error) {
    if (error.response) {
      console.log('response Error Upload File', error.message)
    } else if (error.request) {
      console.log('Request Error')
    } else if (error.message) {
      console.log('Message Error')
    }
  }
}

const finalizeUpload = async (bucketKey, objectKey, uploadKey, token) => {
  const config = {
    method: 'post',
    url: `https://developer.api.autodesk.com/oss/v2/buckets/${bucketKey}/objects/${objectKey}/signeds3upload`,
    headers: {
      ContentType: 'application/json',
      Authorization: `Bearer ${token}`,
    },
  }
  try {
    let res = await axios(config, uploadKey)
    return res.data
  } catch (error) {
    if (error.response) {
      console.log('response Error Finalize upload', error.message)
    } else if (error.request) {
      console.log('Request Error')
    } else if (error.message) {
      console.log('Message Error')
    }
  }
}

Here same error. Solved with

let res = await axios(config, JSON.stringify({uploadKey}))

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM