将文件流上传到S3而没有文件和内存

时间:2017-06-02 18:15:43

标签: node.js amazon-s3

我正在尝试从字符串创建一个csv并将其上传到我的S3存储桶。我不想写文件。我希望这一切都在记忆中。

我不想从文件中读取来获取我的流。我想制作一个没有文件的流。我想要这个方法createReadStream,但我想传递一个包含我的流内容的字符串而不是文件。

var AWS      = require('aws-sdk'),
    zlib     = require('zlib'),
    fs       = require('fs');
    s3Stream = require('s3-upload-stream')(new AWS.S3()),

// Set the client to be used for the upload. 
AWS.config.loadFromPath('./config.json');

// Create the streams 
var read = fs.createReadStream('/path/to/a/file');
var upload = s3Stream.upload({
  "Bucket": "bucket-name",
  "Key": "key-name"
});

// Handle errors. 
upload.on('error', function (error) {
  console.log(error);
});

upload.on('part', function (details) {
  console.log(details);
});

upload.on('uploaded', function (details) {
  console.log(details);
});

read.pipe(upload);

1 个答案:

答案 0 :(得分:3)

你可以创建一个ReadableStream并将你的字符串直接推送到它,然后你的s3Stream实例可以使用它。

const Readable = require('stream').Readable

let data = 'this is your data'
let read = new Readable()
read.push(data) // Push your data string
read.push(null) // Signal that you're done writing

// Create upload s3Stream instance and attach listeners go here

read.pipe(upload)