Node.js knox s3图像检索

Node.js knox s3 image retrieval

本文关键字:图像检索 s3 knox js Node      更新时间:2023-09-26

我正在尝试使用以下方法从s3节点检索图像:

app.get('/photos', function(req, res, next) {
var data = '';
s3.get('/tmp/DSC_0904.jpg').on('response', function(s3res){
    console.log(s3res.statusCode);
    console.log(s3res.headers);
    s3res.setEncoding('binary');
    s3res.on('data', function(chunk){
      data += chunk;
    });
    s3res.on('end', function() {
      res.contentType('image/jpeg');
      res.send(data);
    });
  }).end();
});

我能够通过在结束事件回调中进行以下修改来下载图像:

s3res.on('end', function() {
    res.contentType('image/jpeg');
    res.write(data, encoding='binary')
    res.end()
});

我和原来的海报有同样的问题。我怀疑既然我们将传入缓冲区的编码设置为二进制,那么我们需要在输出流上做同样的事情。经过一番研究,我发现write方法除了编码类型作为参数。

您可能喜欢使用AwsSum,因为它的功能和维护齐全。这里还有一个examples/目录里面有很多Amazon S3的例子:

  • https://github.com/appsattic/node-awssum/

在node-awssum-scripts存储库中也有一个示例,它与node-awssum存储库是分开的:

  • https://github.com/appsattic/node-awssum-scripts/blob/master/bin/amazon-s3-download.js

如果你进展顺利或者需要任何帮助,请告诉我。免责声明:我是AwsSum的作者。:)

我用它来获取我的图像,它工作得很好。

//  Create the new file using fs
var new_file = fs.createWriteStream(destination_file);
//  Now grab the file from s3
aws_connection.getFile(f, function(err, res) {
    if(err) return err;
    res.on('data', function(chunk) {
        new_file.write(chunk);
    });
    res.on('end', function(chunk) {
        new_file.end();
    });
});