김동오
김동오

Reputation: 69

node-archiver does not work in aws lambda

This code works well locally. However, it does not behave strangely in the aws lambda environment. No error occurs. However, it does not actually work. ( lambda ) The entry and progress events of the node-archiver are not raised. What is the problem?

I can push my code to the GitHub if it's necessary.

Envirment

'use strict';

const axios= require('axios')
const archiver= require('archiver')
const AWS = require('aws-sdk')

const { log } = console
const logging = value => log(value)
const errorHandler = logging

const loggingW = (value, b) => v2 => {
  if(b) console.log(value,v2)
}

const stream2 = (Bucket, Key) => {
  const stream = require('stream');
  const Body = new stream.PassThrough();
  s3.upload({
    Bucket, Key, Body,
  }, errorHandler)
  return Body
}

const s3 = new AWS.S3({
  accessKeyId: '===', secretAccessKey: '===' // ur key
})

const hello = async (event, context) => {

  const res = await axios({
    url: 'some image', // some image url
    responseType: 'stream'
  })

  const archive = archiver('zip')

  archive
    .on('entry', loggingW('a-e', true))
    .on('progress', loggingW('a-p', true))
    .on('warning', loggingW('a-w', true))
    .on('fininsh', loggingW('a-finish', true))
    .on('end', loggingW('a-end', true))
    .on('close', loggingW('a-close', true))
    .on('error', function(err) {
      console.log('archie', err)
      throw err;
    });

  const dest = stream2('bucket', 'ttt.zip')
  dest
    .on('drain', loggingW('a-d', false))
    .on('pipe', loggingW('s-p', false))
    .on('unpipe', loggingW('s-up', false))
    .on('close', loggingW('close', false))
    .on('finish', loggingW('s-f', false))
    .on('end', loggingW('end'))
    .on('error', loggingW('error', true))

  archive.pipe(dest)
  archive.append(res.data, { name: '/ok/tt.jpg' }).finalize()

  return {
    statusCode: 200,
    body: JSON.stringify({
      message: '1',
      input: event,
    }),
  };

  // Use this code if you don't use the http event with the LAMBDA-PROXY integration
  // return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};

module.exports.hello = hello

// hello()

Console Result

a-e { name: 'ok/tt.jpg',      
  type: 'file',
  date: 2019-01-30T11:31:19.255Z,
  mode: 420,
  prefix: null,
  sourcePath: null,
  stats: false,
  sourceType: 'stream',
  linkname: null,
  store: false,
  comment: '' }
a-p { entries: { total: 1, processed: 1 },
  fs: { totalBytes: 0, processedBytes: 0 } }
a-end undefined
null

// lambda
{
    "statusCode": 200,
    "body": "{\"message\":\"1\",\"input\":\"\"}"
}

Upvotes: 1

Views: 505

Answers (0)

Related Questions