Sails.js使用Skipper将文件上载到AWS S3

时间:2014-11-11 14:47:09

标签: sails.js skipper

我有一个表单,您可以上传文件。我直接用船长上传文件,效果很好。

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

但我想首先调整大小然后上传文件,所以:

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

所以,我的问题是:如何上传outputBuffer代替req.file('file')

1 个答案:

答案 0 :(得分:1)

使用Skipper-s3并进行帆帆version_1并进行帆帆钩上传

npm我帆钩上传

fn: async function (inputs) {
    var coverImage = await sails
      .uploadOne(inputs.coverImage, {
        // maxBytes: 3000000
        adapter: require('skipper-s3'),
        key: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        secret: 'xxxxxxxxxxxxxxxxxxxx',
        bucket: 'xxxxxxx',
        saveAs:'your custom file name ?',
        dirname: '/cover_images ?',
      })
      // Note: E_EXCEEDS_UPLOAD_LIMIT is the error code for exceeding
      // `maxBytes` for both skipper-disk and skipper-s3.
      .intercept('E_EXCEEDS_UPLOAD_LIMIT', 'tooBig')
      .intercept(
        (err) =>
          new Error('The coverImage upload failed: ' + util.inspect(err)),
      )

    if (!coverImage) {
      throw 'noFileAttached'
    }
}