从url下载文件并将其上传到AWS S3而不保存 - node.js

时间:2014-03-05 01:55:36

标签: javascript node.js amazon-web-services amazon-s3 fs

我正在编写一个从网址下载图片的应用程序,然后使用aws-sdk将其上传到S3存储桶。

显然我只是下载图像并将它们保存到磁盘上。

request.head(url, function(err, res, body){

    request(url).pipe(fs.createWriteStream(image_path));

});

然后像这样将图像上传到AWS S3

fs.readFile(image_path, function(err, data){
    s3.client.putObject({
        Bucket: 'myBucket',
        Key: image_path,
        Body: data
        ACL:'public-read'
    }, function(err, resp) {
        if(err){
            console.log("error in s3 put object cb");
        } else { 
            console.log(resp);
            console.log("successfully added image to s3");
        }
    });
});

但我想跳过将图像保存到磁盘的部分。有什么方法可以pipe request(url)对{{1}}的响应,然后上传它?

2 个答案:

答案 0 :(得分:24)

以下是一些很好地执行此操作的JavaScript:

    var options = {
        uri: uri,
        encoding: null
    };
    request(options, function(error, response, body) {
        if (error || response.statusCode !== 200) { 
            console.log("failed to get image");
            console.log(error);
        } else {
            s3.putObject({
                Body: body,
                Key: path,
                Bucket: 'bucket_name'
            }, function(error, data) { 
                if (error) {
                    console.log("error downloading image to s3");
                } else {
                    console.log("success uploading to s3");
                }
            }); 
        }   
    });

答案 1 :(得分:1)

那又怎么样呢?

df$ID.matches <- apply(outer(df$lat,   df$lat,   function(x, y) abs(x - y) <   1) &
                       outer(df$lon,   df$lon,   function(x, y) abs(x - y) <   1) &
                       outer(df$score, df$score, function(x, y) abs(x - y) < 0.7) &
                       diag(nrow(df)) == 0, 
                       MARGIN = 1,
                       function(x) paste(df$ID[x], collapse = ", "))
df
#>   ID  lat  long score ID.matches
#> 1  1 41.5 -62.3  22.4       3, 7
#> 2  2 41.0 -70.2  21.9           
#> 3  3 42.2 -63.0  22.7          1
#> 4  4 36.7 -72.9  20.0           
#> 5  5 36.2 -62.4  24.1          6
#> 6  6 35.8 -61.7  24.7          5
#> 7  7 40.8 -61.9  22.1          1