skip to Main Content

using "@aws-sdk/client-s3": "^3.481.0", and keep getting the error at send line


      let S3_Config = {
          region: process.env.S3_REGION,
          credentials: {
            accessKeyId:  process.env.S3_ACCESS_KEY_ID,
            secretAccessKey: process.env.S3_ACCESS_SECRET_KEY,
      },
    };
    const s3_client = new S3Client(S3_Config);

    const s3_params = {
      Bucket: process.env.S3_BUCKET_NAME , // required
      MaxKeys: 20,
    };

    const _command = new ListBucketsCommand(s3_params);
    const response = await s3_client.send(_command);

Argument of type ‘ListBucketsCommand’ is not assignable to parameter
of type ‘Command<ServiceInputTypes, ServiceInputTypes,
ServiceOutputTypes, ServiceOutputTypes,
SmithyResolvedConfiguration>’. Type
‘ListBucketsCommand’ is missing the following properties from type
‘Command<ServiceInputTypes, ServiceInputTypes, ServiceOutputTypes,
ServiceOutputTypes, SmithyResolvedConfiguration>’:
input, middlewareStack, resolveMiddlewarets(2345)

This is the exact example as shown https://www.npmjs.com/package/@aws-sdk/client-s3?activeTab=readme

2

Answers


  1. ListBucketsCommand in AWS SDK v3 doesn’t require any input parameters, so pass an empty object {} when creating the command.

    let S3_Config = {
      region: process.env.S3_REGION,
      credentials: {
        accessKeyId:  process.env.S3_ACCESS_KEY_ID,
        secretAccessKey: process.env.S3_ACCESS_SECRET_KEY,
      },
    
    };
    const s3_client = new S3Client(S3_Config);
    
    const s3_params = {
        Bucket: process.env.S3_BUCKET_NAME , // required
        MaxKeys: 20,
    };
    
    const _command = new ListBucketsCommand({}); // No input params are needed for ListBucketsCommand
    const response = await s3_client.send(_command);
    
    Login or Signup to reply.
  2. Your code looks fine . Not sure why it is throwing error .

    But below there’s one alternate solution —

    import {
      S3
    } from '@aws-sdk/client-s3';   
    
     const s3 = new S3({
                region: process.env.S3_REGION,
                credentials: {
                  accessKeyId:  process.env.S3_ACCESS_KEY_ID,
                  secretAccessKey: process.env.S3_ACCESS_SECRET_KEY,
          },
              });
        
       const response = await this.s3.listObjects({ Bucket: process.env.S3_BUCKET_NAME , MaxKeys:20 });
    

    Hope it helps .

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search