2017-06-02 71 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)