Search code examples
amazon-s3aws-sdk-jsaws-sdk-nodejs

Using AWS SDK (JS) for s3.selectObjectContent gives error on 'on' keyword


I'm using AWS SDK for Javascript version 2.730.0 (latest at time of writing) in a Typescript file in Node.JS.

I'm using the selectObjectContent operation to query a CSV file, and following the guide in the documentation I have this block:

import * as S3 from 'aws-sdk/clients/s3';
const s3 = new S3();

...

s3.selectObjectContent(params, (err, data) => {
   if (!err){
      data.Payload.on('data', (event) => {

         // Do something with returned records

      });
   }
});

The line data.Payload.on('data', (event) => { is giving this error in the linter:

Property 'on' does not exist on type 'EventStream<{ Records?: RecordsEvent; Stats?: StatsEvent; Progress?: ProgressEvent; Cont?: ContinuationEvent; End?: EndEvent; }>'.

What do I need to change for on to work?


Solution

  • I ran into the same problem myself. Found this problem post on another forum:

    https://www.gitmemory.com/issue/aws/aws-sdk-js/3525/725076849

    It does not explicitly show code to solve the problem but based on the information, I solved it as follows:

    import { ReadStream } from "fs";
    const eventStream = data.Payload as ReadStream;
    eventStream.on("data", ({ Records, Stats, Progress, Cont, End }: ...
    

    TypeScript no longer complains.