@aws-sdk/client-s3 S3Client returning Error TypeError: Cannot read properties of undefined (reading 'send')

Viewed 42

Following along with the AWS-SDK examples/documentation in a Gatsby app, I have the workflow below setup to upload a file to my S3 bucket from the client directly however, the const data = await client.send(...) is returning Error TypeError: Cannot read properties of undefined (reading 'send') even though client definitely exists.

AWS is a new frontier, forgive me if my mistake is painfully obvious.

full error:

Error TypeError: Cannot read properties of undefined (reading 'send')
at eval (fromCognitoIdentityPool.js:26:1)
at step (tslib.es6.js:102:1)
at Object.eval [as next] (tslib.es6.js:83:1)
at fulfilled (tslib.es6.js:73:1)

Client initializer and accompanying uploader AJAX:

import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { fromCognitoIdentityPool } from "@aws-sdk/credential-provider-cognito-identity";

const client = new S3Client({
  region: process.env.GATSBY_AWS_S3_REGION,
  credentials: fromCognitoIdentityPool({
    clientConfig: { region: process.env.GATSBY_AWS_S3_REGION },
    identityPoolId: process.env.GATSBY_AWS_USER_POOL_ID,
  })
})

// Upload file to specified bucket.
export const run = async ({ email, photo }) => {
  const uploadParams = {
    Bucket: process.env.GATSBY_AWS_BUCKET_NAME,
    Key: email + photo.name,
    Body: photo,
  };

  try {
    const data = await client.send(new PutObjectCommand(uploadParams));
    console.log("Success", data);
    return data; 
  } catch (err) {
    console.log("Error", err);
  }
};

1 Answers

The mistake was in the import:

import { fromCognitoIdentityPool } from "@aws-sdk/credential-provider-cognito-identity";

when it should have been:

import { fromCognitoIdentityPool } from "@aws-sdk/credential-providers";
Related