LUCKY SHARMA
LUCKY SHARMA

Reputation: 21

s3.getObject not working from dev environment

This is my code, which works fine if i run it from my local using local aws account , but it doesn't work from my dev environment. S3.getobject api doesnt get executed and code prints the next log skipping the getobject call :

const unzipFromS3 = (key) => {
    return new Promise(async (resolve, reject) => {
        log.info("inside unzipfroms3");  
        var zlib = require('zlib');
        // let fileName = _.replace(key, 'Root/', '');
        let options = {
            'Bucket': config.bucketName,
            'Key': "Root/" + key,
        }
        log.info("Key:", options);    
        await s3.getObject(options).on('error', error => {
            log.error(error) }).promise().then((res) => {
                yauzl.fromBuffer(res.body, { lazyEntries: true }, function (err, zipfile) {
                    log.info("Inside Yauzl")
                    if (err) throw err;
                    zipfile.readEntry();
                    zipfile.on("entry", function (entry) {
                        if (/\/$/.test(entry.fileName)) {
                            zipfile.readEntry();
                        } else {
                            zipfile.openReadStream(entry, function (err, readStream) {
                                if (err) throw err;
                             // readStream.pipe(fs.createWriteStream(`result/${entry.fileName}`));
                                readStream
                               .pipe(uploadFromStream(s3));
                                function uploadFromStream(s3) {
                                    log.info("Inside uploadFromStream")
                                    var pass = new Stream.PassThrough();
                                    let options = {
                                       'Bucket': config.bucketName,
                                       'Key': entry.fileName,
                                    }
                                    var params = { ...options, Body: pass };
                                    s3.upload(params, function (err, data) {
                                        log.error(err, data);
                                    });
                                    return pass;
                                    }
                                    readStream.on("end", function () {
                                        zipfile.readEntry();
                                    });
                                });
                            }
                        });
                    });
                });
            });
      };

Upvotes: 2

Views: 474

Answers (1)

matsev
matsev

Reputation: 33779

In order to use await, i.e. the promised based version of S3.getObject(), you must add the promise() method to your method call as explained in the Using JavaScript Promises chapter of the AWS SDK developer guide. Moreover, there is also an Using async/await chapter that you can look into.

In your case, the code can be modified to something like:

await s3.getObject(options).promise()
    .then((res) => {
        yauzl.fromBuffer(/* more code */);
    });

Upvotes: 1

Related Questions