如何在node.js中创建流?

时间:2018-01-09 16:14:18

标签: javascript node.js stream

我有一个以stream为参数的函数:

  startUpload(stream, filename) {
    const upload = this.s3.upload({
      Bucket: this.bucket,
      ServerSideEncryption: 'aws:kms',
      SSEKMSKeyId: this.s3SseKmsKeyId,
      Key: `${this.key}/${filename}`,
      Body: stream
    }, { partSize: 50 * 1024 * 1024, queueSize: 1 });
  }

如何创建流并向其写入数据?

1 个答案:

答案 0 :(得分:1)

下面的代码适用于我

const AWS = require('aws-sdk');
AWS.config.update({
        accessKeyId: process.env.AWS_KeyId,
        secretAccessKey: process.env.AWS_secret,
        // region:process.env.AWS_region  
});

const s3 = new AWS.S3({   signatureVersion: 'v4',  })
var multer = require('multer')
var multerS3 = require('multer-s3')
var photoBucket = new AWS.S3({
    params: {
        Bucket: 'carwhapps'
    }
})
// let source is like req.files.filename or test with static file like ./public/test.jpg
photoBucket.upload({
        ACL: 'public-read', 
        Body: fs.createReadStream(source), 
        Key: filename,
        ContentType: 'application/octet-stream' // force download if it's accessed as a top location
},(err, response)=>{
    console.log(err, response)

})