Friday 2 June 2017

Upload a file stream to S3 without a file and from memory

I'm trying to create a csv from a string and upload it to my S3 bucket. I don't want to write a file. I want it all to be in memory.

I don't want to read from a file to get my stream. I would like to make a stream with out a file. I would like this method createReadStream, but instead of a file, I would like to pass a string with my stream's contents.

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);



via bigbusterswife

No comments:

Post a Comment