JS SDK createPresignedPost returns error Cannot read properties of undefined (reading 'endsWith')

0

I have the following code in my Node server:

const { S3Client } = require('@aws-sdk/client-s3');
const { createPresignedPost } = require('@aws-sdk/s3-presigned-post');

router.post(
  '/sign-s3', async (req, res, next) => {
    const { name, type } = req.body;
    const client = new S3Client({
      region: 'eu-central-1'
    });
    const params = {
      Bucket: process.env.S3_BUCKET_NAME,
      Expires: 60,
      Conditions: [
        ['content-length-range', 100, 5242880],
        { 'Content-Type': 'image/jpeg' },
      ],
      Fields: {
        key: `blog/${name}`,
        'Content-Type': type,
        success_action_status: '201',
      },
    };
    try {
      const data = await createPresignedPost(client, params);
      return res.json(data);
    } catch (err) {
      return next({ status: 500, message: err.message });
    }
  }
);

This route returns the following: Cannot read properties of undefined (reading 'endsWith'). The error is not really helpful. I've tried the following possible solutions:

  • Checking if AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are available from process.env
  • Passing credentials into the S3Client, like so:
credentials: {
      accessKeyId: process.env.AWS_ACCESS_KEY_ID,
      secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
}
  • Removing credentials from .env and S3Object to narrow down the issue - it does throw a credential error if both are removed, however, if for instance I pass an empty credentials object into S3Object, I still get the Cannot read properties of undefined (reading 'endsWith') error. It's almost like there's no credentials validation? I also tried passing completly wrong credentials.
  • Passing credentials DIRECTLY into the credentials object (omitting .env altogether)
  • Uninstalling and installing SDK again
  • Removing params and leaving bucket name only

Nothing works. Please help.

asked 2 years ago130 views
No Answers

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions