Sails.js File upload with Skipper to AWS S3

眉间皱痕 提交于 2019-12-06 01:59:22

问题


I have a form where you can upload a file. I upload the file directly with skipper and it works perfectly.

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

But I want to resize first 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 can upload the outputBuffer instead of req.file('file') ?


回答1:


Instead of using skipper-s3 you can use the aws-sdk module. Upload the image to disk (original), process it, upload it, 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 (like gm) can take a remote URL. You could use skipper-s3 to upload, then carry out the process above (where original is the s3 URL) and it would work, too—really not performant, at all, though.



来源:https://stackoverflow.com/questions/26867554/sails-js-file-upload-with-skipper-to-aws-s3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!