1

I'm trying to get a list of files within an S3 folder in a lambda written in typescript. I've added the following dependencies to my package.json.

"@aws-sdk/client-s3": "^3.41.0",
"@aws-sdk/node-http-handler": "^3.40.0",

I then use the S3 client like this:

const client = new S3Client({
    maxAttempts: 3,
    retryMode: 'STANDARD',
    region: getAwsRegion(),
    requestHandler: new NodeHttpHandler({
      connectionTimeout: 3000, // Timeout requests after 3 seconds
      socketTimeout: 5000, // Close socket after 5 seconds
    }),
    credentials: args.credentials,
  });


  const listObjectsCommand = new ListObjectsCommand({
    Bucket: args.bucketName,
    Delimiter: '/',
    Prefix: pathToPartition,
  });
  const objects = await client.send(listObjectsCommand);

I've tried using ListObjectsV2Command as well, but it has the same error. The error is:

TypeError: input.useDualstackEndpoint is not a function\n at Object.getEndpointFromRegion (/var/task/node_modules/<my_module>/node_modules/@aws-sdk/config-resolver/dist-cjs/endpointsConfig/utils/getEndpointFromRegion.js:12:46)\n at processTicksAndRejections (internal/process/task_queues.js:95:5)\n at async Object.serializeAws_restXmlListObjectsCommand (/var/task/node_modules/<my_module>/node_modules/@aws-sdk/client-s3/dist/cjs/protocols/Aws_restXml.js:2386:68)\n at async /var/task/node_modules/<my_module>/node_modules/@aws-sdk/middleware-serde/dist-cjs/serializerMiddleware.js:5:21 .

Any idea what I may be doing wrong?

3 Answers 3

2

This happened due to a mismatch is the aws-sdk version used in the package.json I had and the package.json of a dependency. Updating it to be the same version fixed this!

Sign up to request clarification or add additional context in comments.

Comments

0

Here is a working minimal version of your function. Get this simple version working first and incrementally add complexity.

AWS_REGION is a lambda-provided env var. I defined the BUCKET env var. You can pass the bucket name with the event payload (or, to begin with, hard-code it to minimise sources of error).

import { S3Client, ListObjectsCommand} from '@aws-sdk/client-s3';

const client = new S3Client({ region: process.env.AWS_REGION });
const bucket = process.env.BUCKET;

export async function handler(): Promise<void> {
  const cmd = new ListObjectsCommand({
    Bucket: bucket
  });

  const objects = await client.send(cmd);

  console.log(objects)
}

2 Comments

Yes, the getAwsRegion() function gets it using the environment variable AWS_REGION as well, just with some logging. I've tried the simple command with only the bucket path earlier but it failed with the exact same error
That's disappointing. I cannot replicate your error. You need to do some debugging: Try it with another bucket. Try another SDK command (like list buckets to see if the Client is working. Try running aws s3 listObjects using the AWS CLI from your computer. You get the picture.
0

I ran into a similar error: "useDualstackEndpoint: options.useDualstackEndpoint ?? false"

The problem: I was using a node version that was NOT compatible with aws sdk JavaScript v3.

Running nvm use 18 and then rerunning my script fixed it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.