一尘不染

从nodejs请求到s3的流响应

node.js

如何使用request下载文件内容,并使用aws-sdk for
node直接将其流式传输到s3?

下面的代码给了我Object #<Request> has no method 'read',这使得请求似乎没有返回可读流…

var req = require('request');
var s3 = new AWS.S3({params: {Bucket: myBucket, Key: s3Key}});
var imageStream = req.get(url)
    .on('response', function (response) {
      if (200 == response.statusCode) {
        //imageStream should be read()able by now right?
        s3.upload({Body: imageStream, ACL: "public-read", CacheControl: 5184000}, function (err, data) {  //2 months
          console.log(err,data);
        });
      }
    });
});

每个aws-
sdk文件都
Body必须是一个ReadableStream对象。

我在这里做错了什么?

可以使用s3-upload-stream模块将其关闭,但是我希望限制我的依赖关系。


阅读 336

收藏
2020-07-07

共1个答案

一尘不染

response如果要手动侦听响应流,则要使用该对象:

var req = require('request');
var s3 = new AWS.S3({params: {Bucket: myBucket, Key: s3Key}});
var imageStream = req.get(url)
    .on('response', function (response) {
      if (200 == response.statusCode) {
        s3.upload({Body: response, ACL: "public-read", CacheControl: 5184000}, function (err, data) {  //2 months
          console.log(err,data);
        });
      }
    });
});
2020-07-07