[英]Nextjs - Autodesk Forge upload to S3 bucket
I'm trying to upload a file to an s3 bucket as per this guide.我正在尝试按照本指南将文件上传到 s3 存储桶。 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.
https://forge.autodesk.com/en/docs/model-derivative/v2/tutorials/translate-to-obj/about-this-tutorial/我卡在第 4 步 - 完成上传。 I'm getting failed with a status code of 400 when calling await finalizeUpload() in the handler.put method.
在 handler.put 方法中调用 await finalizeUpload() 时失败,状态代码为 400。
Here is the api route I am using这是我正在使用的 api 路由
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以下是步骤 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问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.