2017-06-02 72 views
1

我试图从字符串创建一个csv并将其上传到我的S3存储桶。我不想写一个文件。我希望这一切都在记忆中。上传文件流到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

您可以创建一个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)