简体   繁体   中英

Download file from Url and upload to AWS S3 bucket

I'm creating a system to upload files sent to a WhatsApp bot using a node express server.

When my server gets the data from whatsapp there is a link to the media file in the req.body and I'd like to be able to get the file from that link and upload it to an AWS S3 bucket. I'd also like to get the file type ie usually jpg or pdf.

I've played around with this code by Johann Philipp Strathausen

var fs = require('fs');
var zlib = require('zlib');

var body = fs.createReadStream('bigfile').pipe(zlib.createGzip());
var s3obj = new AWS.S3({params: {Bucket: 'myBucket', Key: 'myKey'}});
s3obj.upload({Body: body})
  .on('httpUploadProgress', function(evt) { console.log(evt); })
  .send(function(err, data) { console.log(err, data) });

using this I get the following error when executing the last line

Error: ENOENT: no such file or directory, open 'url/from/whatsapp/api' Emitted 'error' event at:

at lazyFs.open (internal/fs/streams.js:115:12)

at FSReqWrap.args [as oncomplete] (fs.js:140:20)

In the end I used axios to get the file and pass it to the s3upload function

const whatsAppUpload = async (file) => {
    try{
        let {data, headers} = await axios.get(file, {responseType: 'stream'});
        let id = crypto.randomBytes(8).toString('hex');
        let link = headers['content-type'];
        let format = link.split("/").pop();
        let key = Date.now() + id + "." + format;
        const objectParams = {
            Bucket: 'bucket-name-here',
            ACL: 'public-read',
            ServerSideEncryption: 'AES256',
            ContentLength: headers['content-length'],
            Body: data,
            ContentType: headers['content-type'],
            Key: key
        };
        data = await s3.upload(objectParams).promise();
        console.log(`File uploaded successfully. ${data.Location}`);
    } catch(err){
        console.log("ERROR --->" + err)
    }
}

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