H
H
hardwellZero2017-06-23 17:44:06
Amazon Web Services
hardwellZero, 2017-06-23 17:44:06

Async upload AWS S3?

Hello.
There is a need to download a picture, upload it to AWS S3, then delete it.
If you don't have to download it, it would be great to keep it in memory or something like that...
This is an example of a controller for koajs.
In the response back, of course, I want to give a link to the file on S3.

exports.updatePhoto = async function (ctx) {
    let url = 'http://cs8.pikabu.ru/post_img/2017/06/23/8/149822058719807864.jpg';
    let timestamp = + new Date();
    let filename = config.aws.userFolder + '/' + timestamp + '.png';

    await request(url).pipe(fs.createWriteStream('test.png'));

    let read = await fs.createReadStream('test.png');
    let upload = s3Stream.upload({
        'Bucket': config.aws.bucketName,
        'Key': filename
    });

    upload.on('error', function (error) {
        console.log('ERROR');
        console.log(error);
        ctx.body = {error: error};
    });

    upload.on('uploaded', function (details) {
        console.log('DONE');
        console.log(details);
        ctx.body = {url: details.Location};
    });

    await read.pipe(upload);

    // await fs.unlink('test.png', function(err) {
    //     console.log(err);
    // });

    ctx.body = 'Hello';
};

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question