NSR
NSR

Reputation: 877

An error occured to read the PNG file from file system on AWS Lambda

I am using AWS Lambda and will read some file from Lambda function.

A text file can be read, even though A png file can not be read. It seems to be kind of strange. Does AWS lambda can not read the PNG file?

I did the following process:

  1. In order to upload to AWS Lambda, archiving a project.

    $ ls -1
    index.js
    test.png
    test.txt
    
    
    $ cat index.js
    exports.handler = function(event, context) {
      var fs = require('fs');
    
      var path = __dirname + '/test.txt';
    
      fs.readFile(path, function(err, buffer) {
          console.log('err = ' + err);
          console.log(buffer.length);
          context.done();
      });
    }
    
    $ zip -r lambda.zip .
      adding: index.js (deflated 35%)
      adding: test.png (deflated 1%)
      adding: test.txt (stored 0%)
    
  2. Upload an archive file to AWS Lambda and invoke the function.

  3. Invoked result is successful.

    START RequestId: 2997ce37-2628-11e5-9e15-456a1ab0c0d0
    2015-07-09T10:49:24.660Z    2997ce37-2628-11e5-9e15-456a1ab0c0d0    err = null
    2015-07-09T10:49:24.661Z    2997ce37-2628-11e5-9e15-456a1ab0c0d0    5
    END RequestId: 2997ce37-2628-11e5-9e15-456a1ab0c0d0
    REPORT RequestId: 2997ce37-2628-11e5-9e15-456a1ab0c0d0  Duration: 123.68 ms Billed Duration: 200 ms     Memory Size: 128 MB Max Memory Used: 9 MB   
    
  4. Cnahge reading file from test.txt to test.png.

  5. Upload an archive file to AWS Lambda and invoke the function.

  6. Invoked result is failure.

    START RequestId: d9d5b6ff-2627-11e5-92ff-85a0d38392ee
    2015-07-09T10:47:10.689Z    d9d5b6ff-2627-11e5-92ff-85a0d38392ee    err = Error: EACCES, open '/var/task/resource/test.png'
    END RequestId: d9d5b6ff-2627-11e5-92ff-85a0d38392ee
    REPORT RequestId: d9d5b6ff-2627-11e5-92ff-85a0d38392ee  Duration: 214.73 ms Billed Duration: 300 ms     Memory Size: 128 MB Max Memory Used: 10 MB  
    Process exited before completing request
    

Upvotes: 0

Views: 738

Answers (1)

zhiyelee
zhiyelee

Reputation: 439

I think there are something wrong with the file permission. Try ls -l

AWS Lambda use v0.10.36, indeed you can test locally. For example, create a test.js, like below:

var handler = require('.').handler;
var ctx = {
  done: function () {
    console.log('Done');
  }
};
handler('', ctx);

For this scenario, you can simply use test.js as below:

  var fs = require('fs');

  var path = __dirname + '/test.png';

  fs.readFile(path, function(err, buffer) {
      console.log('err = ' + err);
      console.log(buffer.length);
      context.done();
  });

Upvotes: 1

Related Questions