简体   繁体   English

尝试将文件上传到 s3 时出现“不支持的正文有效负载对象”错误

[英]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.

相关问题 尝试上传到 Amazon S3 时出现“不受支持的正文有效负载对象” - "Unsupported body payload object" when trying to upload to Amazon S3 AWS S3 图像上传错误:不支持的正文有效负载 Object - AWS S3 Image Upload Error: Unsupported Body Payload Object 使用 Node.js 上传 AWS S3 文件:不支持的正文有效负载错误 - AWS S3 file upload with Node.js: Unsupported body payload error 尝试使用 Node 将图像上传到 S3 时,为什么会出现此连接错误? - Why am I getting this connection error when trying to upload an image to S3 with Node? 尝试通过 JS SDK 将对象上传到 S3 时 AuthorizationHeaderMalformed - AuthorizationHeaderMalformed when trying to upload object to S3 via JS SDK WebKitFormBoundary 包含在直接上传到 s3 的文件有效负载中 - WebKitFormBoundary included in file payload on direct upload to s3 尝试上传到AWS S3存储桶时收到400错误请求 - getting 400 Bad Request when trying to upload to aws s3 bucket AWS S3文件上传,没有出现“访问控制允许来源”错误 - AWS S3 File Upload, getting No 'Access-Control-Allow-Origin' error 使用节点js的S3文件上传文件对象 - S3 file upload file object using node js 尝试上载到存储时出现firebase错误 - Getting firebase error when trying to upload to storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM