Sails.js Uploading a file using Skipper in AWS S3

I have a form in which you can upload a file. I download the file directly from the skipper and it works great.

req.file('file').upload({ adapter: require('skipper-s3'), key: 'key', secret: 'secret', bucket: 'bucketname' }, function (err, uploadedFiles) { if (err){ // ko } else{ // ok } }); 

But first I want to resize and then upload the file, so:

 sharp(original).resize(800).quality(90).toBuffer(function(err, outputBuffer) { if (err) { // ko } // ok outputBuffer; }); 

So my question is: how to load outputBuffer instead of req.file('file') ?

+6
source share
1 answer

Instead of using skipper-s3 you can use the aws-sdk module. Download the image to disk (the original), process it, load and delete the original.

 var AWS = require('aws-sdk'), fs = require('fs'); sharp(original).resize(800).quality(90).toBuffer(function(err, outputBuffer) { if (err) { ... } else { new AWS.S3({ accessKeyId: 'your access key', secretAccessKey: 'your secret', params : { Bucket : 'your bucket', Key: 'desired filename' } }); s3client.upload({ACL:'public-read', Body: outputBuffer}, function(err, result) { if(err) { //handle error } else { // continue, handle returned data fs.unlinkSync(original); // delete original } }); } }); 

Alternatively, some libraries (e.g. gm ) may accept a remote URL. You can use skipper-s3 to download and then execute the above process (where the original is the s3 url) and it will work too, even not quite executable.

+3
source

Source: https://habr.com/ru/post/978032/


All Articles