Search code examples
typescriptamazon-web-servicesamazon-s3aws-lambda

s3.putObject() uploading file to S3 but then never returning a response?


import AWS from 'aws-sdk';
const s3 = new AWS.S3();

export async function putObjectToS3(
  data: any,
  nameOfFile: string,
  contentType: string
) {
  const params = {
    Bucket: process.env.S3_BUCKET || '',
    ContentType: contentType,
    Key: `${nameOfFile}`,
    Body: data
  };

  console.log('start s3 put object');
  const response = await s3.putObject(params).promise();

  console.log('end s3 put object: ', response);
  return response;
}


await putObjectToS3(artistsCsv, `artists/artists.csv`, 'text/csv');

We have the following function that uploads a file to AWS S3. This is run in an AWS lambda function. While our artistsCsv file is successfully uploaded (we see the CSV file in S3), the function never returns... We see the log 'start s3 put object' in our logging, but we never see 'end s3 put object: ', response.

Is there something wrong with the way we have built this function, that is causing it to never return the response? We are using aws-sdk version "aws-sdk": "^2.1147.0",.

Could this simply be an issue with AWS? (seems unlikely though). Perhaps something with the .promise(), and async/await, that we are missing? How can we even troubleshoot this?


Solution

  • You can use upload method if putObject doesn't work, it supports promise since 2.6.12(https://github.com/aws/aws-sdk-js/blob/master/CHANGELOG.md#2612), so something like this should work:

    const response = await s3.upload(uploadParams).promise();
    console.log('end s3 put object: ', response);

    You can also find a complete example from the original documentation here(it's with callback,but the concept is the same):
    https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascript/example_code/s3/s3_upload.js