简体   繁体   中英

Read file from Amazon S3 with nodeJS and post it to another server with axios

Im trying to read a file (image) from amazon S3 and post it to another server with multipart/form .

  let imageParams = { Bucket: 'my-bucket', Key: 'imageName.jpg' };

  let formData = new FormData();

  formData.append('file', s3.getObject(imageParams).createReadStream());

  let apiResponse = await api.post("/fileUpload", formData,
    { params: { token: process.env.API_TOKEN } }, 
    { headers: {'Content-Type': 'multipart/form-data' } } );

But im not managing it to work, it returns me:

Error: Request failed with status code 415

maybe im misunderstanding how the createReadStream() works?

Use concat for pipe the stream. Otherwise form data send only the first chunk of stream, and the server don't know how to handle it.

For example

const axios = require('axios');
const {S3} = require('aws-sdk');
const FormData = require('form-data');
const s3 = new S3();
var concat = require('concat-stream')

const api = axios.default.create({
    baseURL: 'http://example.com',

})

const readStream = s3.getObject({Bucket: 'bucket', Key: 'file'}).createReadStream();
readStream.pipe(concat(filebuffer => {
    const formData = new FormData();
    formData.append('file', filebuffer);
    formData.getLength((err, length) => {
        console.log(length)
        const headers = formData.getHeaders({'content-length': length})
        console.log({headers})
        return api({
            method: 'post',
            url: "/upload",
            headers: headers,
            data: formData
        })
    })

}))

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