下载解压上传cloudinary

Download Unzip Upload clodinary

我正在尝试下载一个 ZIP 文件,请求通过管道将其传输到 node-unzip,然后通过管道将其传输到 cloudinary。

var stream = cloudinary.uploader.upload_stream(function(result) {
  console.log(result)
});

request(file_url).pipe(unzip.Parse()).pipe(stream); //<-- This do not work!
ERROR: { error: { message: 'Empty file', http_code: 400 } }

fs.createReadStream('my_picture.jpg').pipe(stream); //<-- This works!

(你没有提到,但我假设你的 request var 是 request 的一个实例)

首先检查您的 zip 是否有效并包含您期望的内容:

(代码取自 node-unzip's readme

var unzip = require('unzip');
var request = require('request');
var fstream = require('fstream');
var writeStream = fstream.Writer('output/path');

request(file_url)
  .pipe(unzip.Parse())
  .pipe(writeStream);

然后,从一个 zip 文件中发送一个条目(即使只有一个):

var cloudinary = require('cloudinary');
var unzip = require('unzip');
var request = require('request');
var stream = cloudinary.uploader.upload_stream(function(result) {
  console.log(result)
});
request(file_url)
  .pipe(unzip.Parse())
  .on('entry', function (entry) {
    var fileName = entry.path;
    var type = entry.type; // 'Directory' or 'File'
    var size = entry.size;
    if (fileName === "your_image.png") {
      entry.pipe(stream);
    } else {
      entry.autodrain();
    }
  });