[英]Getting "Unsupported Body Payload Object" error when trying to upload file to s3
I'm trying to upload a file to s3 that's being sent to my server via REST from my front end.我正在尝试将文件上传到 s3,该文件从我的前端通过 REST 发送到我的服务器。
I'm using React on the frontend and Koa on the backend.我在前端使用 React,在后端使用 Koa。 (
"koa": "^2.13.0", "koa-body": "^5.0.0"
). (
"koa": "^2.13.0", "koa-body": "^5.0.0"
)。
The code on the front end is using a FormData to send the file and an ID associated with which bucket the file should be uploaded to (using GraphQL that makes REST calls):前端的代码使用 FormData 发送文件和与文件应该上传到哪个存储桶关联的 ID(使用 GraphQL 进行 REST 调用):
async uploadFile (args) {
if (!args.file) return args;
const file = await args.file;
const configData = new FormData();
configData.append('file', file.createReadStream());
configData.append('locationId', args.locationId);
return await this.post('/model/config', configData);
}
Server side code:服务器端代码:
// Controller
const uploadConfig = async (ctx) => {
const file = ctx.request.files;
console.log('file', file)
const {locationId} = ctx.request.body;
ctx.body = await modelService.uploadConfig(file, locationId);
}
// Model Service
const uploadFile = async (file, path) => {
const {mimetype} = file;
console.log('file is: ', file);
return await s3.upload(file, `${path}/assets/config.rcconfig}`, 'application/octet-stream');
}
exports.uploadConfig = async (file, locationId) => {
const loc = await getDirLocation(locationId);
const {LOCATION: uri} = await uploadFile(file, dirname(loc.uri));
return uri;
}
When I log the file in the uploadFile function I get:当我在 uploadFile 函数中记录文件时,我得到:
file is: {
file: PersistentFile {
_events: [Object: null prototype] { error: [Function (anonymous)] },
_eventsCount: 1,
_maxListeners: undefined,
lastModifiedDate: 2022-05-31T17:40:02.850Z,
filepath: '/tmp/7064b3e99e9cb27282a393200',
newFilename: '7064b3e99e9cb27282a393200',
originalFilename: null,
mimetype: 'application/octet-stream',
hashAlgorithm: false,
size: 14372,
_writeStream: WriteStream {
fd: null,
path: '/tmp/7064b3e99e9cb27282a393200',
flags: 'w',
mode: 438,
start: undefined,
pos: undefined,
bytesWritten: 14372,
closed: true,
_writableState: [WritableState],
_events: [Object: null prototype],
_eventsCount: 1,
_maxListeners: undefined,
[Symbol(kFs)]: [Object],
[Symbol(kIsPerformingIO)]: false,
[Symbol(kCapture)]: false
},
hash: null,
[Symbol(kCapture)]: false
}
}
When I try to upload the file to s3 I get an error当我尝试将文件上传到 s3 时出现错误
App Error: Unsupported body payload object body: "", stack: Error: Unsupported body payload object
应用程序错误:不支持的正文有效负载对象正文:“”,堆栈:错误:不支持的正文有效负载对象
I can log the file (as post above) so I'm not sure why I'm getting this error and how to resolve it so I can upload my file.我可以记录文件(如上面的帖子),所以我不确定为什么会收到此错误以及如何解决它,以便我可以上传我的文件。
I'd appreciate any help that would get me closer to getting my file uploaded to s3!我将不胜感激任何可以让我更接近将文件上传到 s3 的帮助! Thank you!
谢谢!
Create a new file.创建一个新文件。 Write the data from the file passed to your server to the new file.
将传递给服务器的文件中的数据写入新文件。 Upload that new file to s3.
将该新文件上传到 s3。
Try that.试试看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.