Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use decompress in lambda function #88

Open
alex8276 opened this issue Nov 30, 2020 · 1 comment
Open

Use decompress in lambda function #88

alex8276 opened this issue Nov 30, 2020 · 1 comment

Comments

@alex8276
Copy link

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
Copy link
Author

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;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant