这是一个问题的解释,我在使用tus-node-server或Tusd将文件上传到S3时遇到了问题。
问题是,我有一个运行tus服务器的服务器,它接收来自react的请求,并对本地客户端做出反应。该文件成功地从客户端上传到S3,但是当我在S3桶中检查时,内容类型总是转换为S3。
尝试设置内容类型的标题,但tus服务器不喜欢这样。
在下面的回答中,我解释了如何解决这个问题:
发布于 2022-10-21 02:35:28
事实证明,它必须作为元数据从tus-js或uppy客户端作为元数据传递。
代码如下:
uppy
uppy.addFiles({
name: asset.fileName,
type: asset.type,
data: asset,
meta: {
name: asset.fileName,
type: asset.type,
contentType: asset.type, //This field is how you set content-type, it will be parsed on the tus server automatically and will set the correct content-type
},
})
tus-js-client
var upload = new tus.Upload({
name: asset.fileName,
type: asset.type,
data: asset,
meta: {
name: asset.fileName,
type: asset.type,
contentType: asset.type, //This field is how you set content-type, it will be parsed on the tus server automatically and will set the correct content-type
}
}, {
endpoint: "http://localhost:1080/files/",
retryDelays: [0, 3000, 5000, 10000, 20000],
metadata: {
filename: file.name,
filetype: file.type
},
onError: function(error) {
console.log("Failed because: " + error)
},
onProgress: function(bytesUploaded, bytesTotal) {
var percentage = (bytesUploaded / bytesTotal * 100).toFixed(2)
console.log(bytesUploaded, bytesTotal, percentage + "%")
},
onSuccess: function() {
console.log("Download %s from %s", upload.file.name, upload.url)
}
})
// Check if there are any previous uploads to continue.
upload.findPreviousUploads().then(function (previousUploads) {
// Found previous uploads so we select the first one.
if (previousUploads.length) {
upload.resumeFromPreviousUpload(previousUploads[0])
}
// Start the upload
upload.start()
})
请注意,contentType是在tus服务器头上设置内容类型的关键.元数据将被解码,并将正确设置为S3内容类型。
https://stackoverflow.com/questions/74148196
复制相似问题