简体   繁体   English

将文件 stream 上传到没有文件的 S3 和从 memory

[英]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.我正在尝试从 string 创建 csv 并将其上传到我的 S3 存储桶。 I don't want to write a file.我不想写文件。 I want it all to be in memory.我希望这一切都在 memory 中。

I don't want to read from a file to get my stream.我不想从文件中读取我的 stream。 I would like to make a stream with out a file.我想制作一个没有文件的 stream 。 I would like this method createReadStream , but instead of a file, I would like to pass a string with my stream's contents.我想要这个方法createReadStream ,但不是文件,我想传递一个 string 和我的流内容。

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

You can create a ReadableStream and push your string directly to it which, can then be consumed by your s3Stream instance.您可以创建一个 ReadableStream 并将您的 string 直接推送到它,然后可以由您的 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM