question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AWS Lambda function for upload/putObject image works only on local machine

See original GitHub issue

I have a lambda function which uses ‘request’ to get a stream of a file by URL and suppose to upload it to a bucket on s3. It is working perfectly on my local machine using node but not inside the lambda. After running the lambda, I have an empty file with the name I wanted.

Stuff you should know

  • The lambda function is async
  • The node version is 8.10
  • In the example you see putObject, but I have also tried with upload
  • Even when adding a manual sleep of 90-120 seconds to let the lambda run, the file is not uploaded
  • I tried using context.succeed or callback(‘some result’), but it still did not work properly

This is the relevant part of the code

module.exports.handler = async(event, context, callback) => {
  const path = 'bucketToUpload';
  const name = 'imageFileName.jpg';
  let options = {
    uri: responseUrl, // This is the url of the image
    encoding: null
  };
  let reqRes = await request(options); // Here I have the stream
  let awsPutRes = await s3.client.putObject({
    Body: reqRes.body,
    Key: name,
    Bucket: path
  }).promise();
};

Would really appreciate any help or directions for this issue.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ChenFeldmancommented, Oct 3, 2018

So after some more tries I was able to solve the issue.

Here is part of the code:


async function upload(fileStream, fileName, bucketName) {
      let params = {
        Body: fileStream,
        Key: fileName,
        Bucket: bucketName
      };
      await s3.client.putObject(params).promise();
    }

module.exports.handler = async(event, context) => {
      try {
        let s3UploadParams = {
          uri: imageDownloadUrl,
          encoding: null
        };
        let imageFileStream = await request(s3UploadParams);
        await upload(imageFileStream, s3FileName, bucketName);
      } catch (err) {
        context.fail(null, 'Error trying to upload to aws' + err);
      }
    }

Instead of using ‘request’ lib I am using ‘request-promise-native’ to get the stream from the url. (thanks for that @srchase ) And also using the .promise() of aws-sdk library to make it fully work.

1reaction
srchasecommented, Oct 1, 2018

Hello @ChenFeldman,

This doesn’t look like an issue with the SDK, but with Lambda and reqeust.

I was able to get it working with request-promises-native instead though:

const req = require('request-promise-native');

async function upload(body) {
  let params = {
    Body: body,
    Key: 'imageFileName.jpg,
    Bucket: 'bucketToUpload'
  };
  await s3.putObject(params, function(err, data) {
    if(err) { console.log('error');
    } else { console.log('succes')}
  });
}

exports.handler = async (event) => {
  await req.get(myUrl)
  .then((response) => {
    upload(response)
  })
  .catch((err) => {
    console.log('error: ', err);
  })
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

AWS Lambda function for upload/putObject image works only ...
It is working perfectly on my local machine using node but not inside the lambda. After running the lambda, I have an empty...
Read more >
Uploading to Amazon S3 directly from a web or mobile ... - AWS
This Lambda function is granted the S3WritePolicy policy to the bucket by the AWS SAM template. The uploaded object must match the same...
Read more >
Upload image or PDF files to Amazon S3 through API Gateway
How do I upload an image or PDF file to Amazon S3 through API Gateway? ... to perform the PutObject and GetObject actions...
Read more >
Uploading objects - Amazon Simple Storage Service
Upload files or folders to an Amazon S3 bucket. ... You can upload any file type—images, backups, data, movies, etc.—into an S3 bucket....
Read more >
Using an Amazon S3 trigger to invoke a Lambda function
The Lambda function retrieves the source S3 bucket name and the key name of the uploaded object from the event parameter that it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found