asm
asm

Reputation: 947

Node S3Client Error - input.useDualstackEndpoint is not a function

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?

Upvotes: 1

Views: 3191

Answers (3)

rishikarri
rishikarri

Reputation: 4530

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

Upvotes: 0

asm
asm

Reputation: 947

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!

Upvotes: 2

fedonev
fedonev

Reputation: 25679

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)
}

Upvotes: 0

Related Questions