【问题标题】:Upload a file stream to S3 without a file and from memory将文件流上传到 S3 没有文件和从内存
【发布时间】:2017-06-03 03:05:57
【问题描述】:

我正在尝试从字符串创建 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);

【问题讨论】:

    标签: node.js amazon-s3


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-18
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 2016-03-14
      • 2012-03-07
      • 1970-01-01
      • 2011-07-21
      相关资源
      最近更新 更多