decompress icon indicating copy to clipboard operation
decompress copied to clipboard

Use decompress in lambda function

Open alex8276 opened this issue 5 years ago • 1 comments

Hello, Is it possible to use decompress in a lambda function ? I use this code but I never entered in "then" code

var obj = await s3.getObject(params2).promise();
    console.log(obj.Body);
   decompress(obj.Body, '.').then(files => {
       console.log(files)
        files.forEach(function(val,index){
           s3.putObject({
               Bucket:'xxx',
               Key: val.path
           }) 
        });
    })

Best regards

alex8276 avatar Nov 30 '20 11:11 alex8276

I succeed to do that I want !

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

const s3 = new AWS.S3();

exports.handler = async (event) => {
    const file = decodeURIComponent(event.Records[0].s3.object.key);
    
    const srcBucket = event.Records[0].s3.bucket.name;
    var params = { 
        Bucket: srcBucket,
        Key:file
    }

    var obj = await s3.getObject(params).promise();

   var s = await decompress(obj.Body, '/tmp/').then(files => {
        return files;
    })
    for(let i=0;i<s.length;i++){
        await s3.upload({Bucket:'xxx',Key: s[i].path, Body: s[i].data}).promise().then(function(data) {
                console.log(`File uploaded successfully. ${data.Location}`);
            }, function (err) {
                console.error("Upload failed", err);
            })
    }
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    
    return response;
};

alex8276 avatar Nov 30 '20 14:11 alex8276