使用node.js中的Promise从s3存储桶下载映像

时间:2016-09-27 15:34:17

标签: node.js amazon-s3 promise

我想使用node.js中的Promises从本地系统上的s3存储桶下载一些图像文件。

var params = {
    Bucket: bucket_name',
    Key: 'key'
};

var fileStream = fs.createWriteStream('path/to/file.jpg');

我试过这个正在运作的

s3.getObject(params).createReadStream.pipe(fileStream);

但我希望我的代码看起来像这样

return s3.getObject(params).promise()
    .then(function(data) {
        //console.log(data.Body);
        // No idea about this section
    })
    .catch(function(err) {
        throw err;
    });

我必须使用Promise来确保下载所有图像。

5 个答案:

答案 0 :(得分:2)

一种可能的解决方案是使用bluebird并创建一个在流末尾返回promise的函数:

const B = require('bluebird');

function downloadFromS3 (object) {
    var p = B.Promise.defer();

    var stream = s3.getObject(params).createReadStream()

    stream.pipe(fileStream);

    stream.on('error', (e) => p.reject(e))

    stream.on('end', () => p.resolve())

    return p.promise;
}

downloadFromS3(params)
    .then(() => console.log('finished'))
    .catch(() => console.log('failed'))

不确定此代码是否具体可行,但它可能会为您提供一个方向来查看。

答案 1 :(得分:0)

streamToPromise = require('stream-to-promise');

var fileStream = fs.createWriteStream('path/to/file.jpg');
streamToPromise(fileStream).then(function () {
  console.log('Image saved to file.');
});
s3.getObject(params).createReadStream.pipe(fileStream);

答案 2 :(得分:0)

这是一个本机承诺解决方案,在读取流和写入流上检测错误。

function streamPromise(stream) {
    return new Promise((resolve, reject) => {
        stream.on('end', () => {
            resolve('end');
        });
        stream.on('finish', () => {
            resolve('finish');
        });
        stream.on('error', (error) => {
            reject(error);
        });
    });
}

async function s3Download(srcBucket, srcKey, outputPath) {
    var objReq = s3.getObject({
        Bucket: srcBucket,
        Key: srcKey
    });
    let outStream = fs.createWriteStream(outputPath);
    let readStream = objReq.createReadStream();
    readStream.on('error', (err) => {
        console.warn('s3download error', err);
        outStream.emit("error", err);
    });
    readStream.pipe(outStream);
    return streamPromise(outStream);
}

答案 3 :(得分:0)

以下代码段对我有用;

async function getObjectStreamSync(params, dest) {
    return new Promise((resolve, reject) => {

        // create read stream for object
        let stream = s3.getObject(params).createReadStream();

        var fileStream = fs.createWriteStream(dest);
        stream.pipe(fileStream);

        // on error reject the Promise
        stream.on('error', (err) => reject(new Error(err)));

        // on end resolve the Promise
        stream.on('end', () => resolve());
    });
}

await getObjectStreamSync(params, "path/to/file/file.ext");

在这里,将流包裹在 Promise 中。并通过监听发出的事件来拒绝/解决 Promise。

答案 4 :(得分:-1)

以下是与NodeJS 8一起使用async / await的代码段:

const AWS = require('aws-sdk');
const fs = require('fs-extra');
const decompress = require('decompress');

const s3 = new AWS.S3();
const s3Params = {
  Bucket: s3Location.bucketName,
  Key: s3Location.objectKey,
};
const s3Object = await s3.getObject(s3Params).promise();
await fs.writeFile('myfile.zip', s3Object.Body);
await decompress('myfile.zip', 'myFileDir');

/* The compressed file is retrieved as "myfile.zip".
   Content will be extracted in myFileDir directory */