管道读取流,密码,请求未正确上传

时间:2019-06-05 19:15:40

标签: node.js encryption request cypher

我试图通过PUT请求加密和上传文件,当我将readStream传递给加密函数(crypto.createCipheriv),然后传递给请求时,文件仅部分上传(前200个字节)

如果有帮助-我正在使用签名的URL上传到S3存储桶。

当我跳过加密步骤时文件完全上传(没有readStream.pipe(cipher)

const fs = require('fs');
const crypto = require('crypto');
const request = require('request');
const FormData = require('form-data');

const key = Buffer.alloc(32);
const iv = Buffer.alloc(16);

const url = 'https://some-url-here.com';

const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
const readStream = fs.createReadStream('testfile.txt');

readStream.setEncoding('utf8');
cipher.setEncoding('utf8');

readStream.pipe(cipher)

cipher.on('data', (chunk) => {
  console.log(chunk);
});

const formData = {
  'file.jpg': cipher
}

request({
  method: 'PUT',
  url,
  formData: formData
});

最终,我想先加密然后上传一个非常大的文件,而不必将整个文件读到内存中(因此使用流)

0 个答案:

没有答案