本文介绍了从request.js到s3.upload的管道生成一个零字节文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将来自request.js的文件直接传送到s3.下面的代码是我设法找出的最接近的代码.它可以运行,但是以s3结尾的文件为零字节.我怎么了?

I'm trying to pipe a file from request.js straight to s3. The code below is the closest I've managed to figure out. It runs, but the file that ends up on s3 is zero bytes. What am I getting wrong?

var request = require('request');
var AWS = require('aws-sdk');
var s3 = new AWS.S3();

request('https://placekitten.com/g/2000/2000')
    .on('response', function(response) {
        response.on('data', function(data) {
            // this is getting called
            console.log(data.length);
        });

        s3.upload({
            Body: response,
            Bucket: 'myBucket'
            Key: 'foo.jpg',
            // if ContentLength is set, the request just seems to hang?
            // ContentLength: parseInt(response.headers['content-length']),
        }, function(err, data) {
            // no error, but zero byte file
            console.log(err, data);
        });
    });

如果有使用本机http/https模块的简便方法,那也可以.

If there's an easier way using the native http/https modules, that would be fine too.

推荐答案

我不确定如何使其与request.js一起使用,但是本地HTTP模块似乎可以使用.

I'm not sure how to make it work with request.js, but the native HTTP modules seem to work.

var AWS = require('aws-sdk');
var https = require('https');
var s3 = new AWS.S3();

https.request('https://placekitten.com/g/2000/2000', function(response) {
    s3.upload({
        Body: response,
        Bucket: 'myBucket',
        Key: 'foo.jpg',
    }, function(err, data) {
        console.log(err, data);
    });
}).end();

这篇关于从request.js到s3.upload的管道生成一个零字节文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:12