Loading File directly from Node js req body to S3

南笙酒味 提交于 2019-12-25 03:37:08

问题


I am trying to upload files to s3 using Node.js Amazon Web Service sdk and I try to pick file directly from the request body to upload on Amazon. But, I keep getting a TypeError: buf.copy is not a function. Below is my code:

 create: function(req, res) {
    var imageFile = req.file('imageFile');
    var fileName = Math.floor(Date.now() / 1000);
    var key = settings.aws.Key;
    var secret = settings.aws.Secret;
    var bucket = settings.aws.Bucket;
    AWS.config.update({accessKeyId: key, secretAccessKey: secret});
    var parames = {Bucket: bucket, Key: fileName, Body: imageFile};
    var s3Obj = new AWS.S3();
    s3Obj.upload(parames).
    on('httpUploadProgress', function(evt) {console.log("In "+evt.loaded); }).
    send(function(err, data){
        if (err) {
            return ValidationService.jsonResolveError(err, Inventory, res);
        }
        console.log(data);
        res.json({status:200, file: data});
    })
}

And a more detailed stack trace of the error I keep getting:

buffer.js:237 buf.copy(buffer, pos); ^

TypeError: buf.copy is not a function at Function.Buffer.concat (buffer.js:237:9) at ManagedUpload.fillStream (/node_modules/aws-sdk/lib/s3/managed_upload.js:389:21) at Upstream. (/node_modules/aws-sdk/lib/s3/managed_upload.js:172:28) at emitNone (events.js:67:13) at Upstream.emit (events.js:166:7) at endReadableNT (_stream_readable.js:905:12) at nextTickCallbackWith2Args (node.js:455:9) at process._tickDomainCallback (node.js:410:17)


回答1:


I'm not sure what req.file('imageFile') returns, but you should set a stream to AWS.S3 params' Body field. Something like:

var fileStream = fs.createReadStream(filePath);
fileStream.on('open', () => {
    //your existing S3 initialisation code...
     var parames = {Bucket: bucket, Key: fileName, Body: fileStream};



回答2:


Starting in Node 8 you can make use of async/await and stream a file from local directory to S3 as follows:

async function uploadFile(filePath, folderPath)
{
  const readStream = fs.createReadStream(filePath);

  const writeStream = new stream.PassThrough();
  readStream.pipe(writeStream);

  var fname = path.basename(filePath);

  var params = {
        Bucket : 's3-bucket-name',
        Key : folderPath+'/'+fname,
        Body : writeStream
    }

  let uploadPromise = new Promise((resolve, reject) => {
    s3.upload(params, (err, data) => {
      if (err) {
        //logger.error('upload error..', err);
        reject(err);
      } else {
        //logger.debug('upload done..');
        resolve(data);
      }
    });
  });

  var res = await uploadPromise;
  return res;
}


来源:https://stackoverflow.com/questions/35171385/loading-file-directly-from-node-js-req-body-to-s3

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