javascript - 如何将存档 (zip) 通过管道传输到 S3 存储桶

标签 javascript node.js amazon-s3 archive

我对如何进行有点困惑。我正在使用存档( Node js 模块)作为将数据写入 zip 文件的方法。目前,当我写入文件(本地存储)时,我的代码可以正常工作。

var fs = require('fs');
var archiver = require('archiver');

var output = fs.createWriteStream(__dirname + '/example.zip');
var archive = archiver('zip', {
     zlib: { level: 9 }  
});

archive.pipe(output);
archive.append(mybuffer, {name: ‘msg001.txt’});

我想修改代码,使存档目标文件成为 AWS S3 存储桶。查看代码示例,我可以在创建存储桶对象时指定存储桶名称和键(和主体),如下所示:

var s3 = new AWS.S3();
var params = {Bucket: 'myBucket', Key: 'myMsgArchive.zip' Body: myStream};
s3.upload( params, function(err,data){
    … 
});

Or 

s3 = new AWS.S3({ parms: {Bucket: ‘myBucket’ Key: ‘myMsgArchive.zip’}});
s3.upload( {Body: myStream})
    .send(function(err,data) {
    …
    });

关于我的 S3 示例,myStream 似乎是一个可读流,我很困惑如何让它工作,因为 archive.pipe 需要一个可写流。这是我们需要使用直通流的地方吗?我找到了一个例子,其中有人创建了一个传递流,但这个例子太简洁了,无法获得正确的理解。我指的具体例子是:

Pipe a stream to s3.upload()

如果有人能给我任何帮助,我将不胜感激。谢谢。

最佳答案

这对于想知道如何使用 pipe 的其他人可能很有用。

由于您使用直通流正确引用了示例,因此这是我的工作代码:

1 - 例程本身,使用 node-archiver 压缩文件

exports.downloadFromS3AndZipToS3 = () => {
  // These are my input files I'm willing to read from S3 to ZIP them

  const files = [
    `${s3Folder}/myFile.pdf`,
    `${s3Folder}/anotherFile.xml`
  ]

  // Just in case you like to rename them as they have a different name in the final ZIP

  const fileNames = [
    'finalPDFName.pdf',
    'finalXMLName.xml'
  ]

  // Use promises to get them all

  const promises = []

  files.map((file) => {
    promises.push(s3client.getObject({
      Bucket: yourBubucket,
      Key: file
    }).promise())
  })

  // Define the ZIP target archive

  let archive = archiver('zip', {
    zlib: { level: 9 } // Sets the compression level.
  })

  // Pipe!

  archive.pipe(uploadFromStream(s3client, 'someDestinationFolderPathOnS3', 'zipFileName.zip'))

  archive.on('warning', function(err) {
    if (err.code === 'ENOENT') {
      // log warning
    } else {
      // throw error
      throw err;
    }
  })

  // Good practice to catch this error explicitly
  archive.on('error', function(err) {
    throw err;
  })

  // The actual archive is populated here 

  return Promise
    .all(promises)
    .then((data) => {
      data.map((thisFile, index) => {
        archive.append(thisFile.Body, { name: fileNames[index] })
      })

      archive.finalize()
    })
  }

2 - 辅助方法

const uploadFromStream = (s3client) => {
  const pass = new stream.PassThrough()

  const s3params = {
    Bucket: yourBucket,
    Key: `${someFolder}/${aFilename}`,
    Body: pass,
    ContentType: 'application/zip'
  }

  s3client.upload(s3params, (err, data) => {
    if (err)
      console.log(err)

    if (data)
      console.log('Success')
  })

  return pass
}

关于javascript - 如何将存档 (zip) 通过管道传输到 S3 存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51938654/

相关文章:

php - 谷歌地图保存优化方向

javascript - Highcharts Pie 不显示标签(对于某些数据系列)

javascript - 通过 SSL/TLS 连接访问 Vagrant 上的 Node.js 应用程序

node.js - Axios - 使用 url + 变量发送 get 请求

PHP 强制下载远程文件

Node.js Amazon S3 - 下载失败并出现 SignatureDoesNotMatch 错误

javascript - 触发其他函数后 Var 默认为 0

javascript - 用于将迭代值(例如数组元素)插入 HTML 的简单 JavaScript 模板文字

node.js - 在Node和sqlite3中插入多行多列

amazon-s3 - 如何将S3中的10,000个文件公开