Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

s3.getObject(...).createReadStream is not a function

I am trying to send a file from my s3 bucket to the client with my NodeJS application. This is what I have so far:

import { S3 } from '@aws-sdk/client-s3';

const BUCKET_NAME = process.env.S3_BUCKET_NAME;

const s3 = new S3({
    region: 'ap-southeast-2',
    httpOptions: {
        connectTimeout: 2 * 1000,
        timeout: 60 * 1000,
    },
});

router.get('/download/:id', async (req, res) => {
    console.log('api: GET /download/:id');
    console.log('req.params.id: ', req.params.id);
    const result = await getRequiredInfo(req.params.id);
    if (typeof result !== 'number') {
        res.attachment(result.filename);
        await s3
            .getObject({
                Bucket: BUCKET_NAME,
                Key: result.location,
            })
            .createReadStream()
            .pipe(res);
    } else {
        res.sendStatus(result);
    }
});

And when I run this code, I receive this:

(node:11224) UnhandledPromiseRejectionWarning: TypeError: s3.getObject(...).createReadStream is not a function

I wondered around on SO and looks like others are working fine with the combination of getObject and createReadStream. Is there something I am missing at this point? How should I send a file as a stream in the response?

like image 886
Terry Windwalker Avatar asked Feb 07 '26 18:02

Terry Windwalker


2 Answers

.createReadStream() is a method in aws-sdk v2. @aws-sdk/client-s3 is part of aws-sdk v3.

To get the stream from the v3 you'll need to do the following:

const response = await s3.getObject({
    Bucket: BUCKET_NAME,
    Key: key,
});

response.Body.pipe(res);

For more information about aws-sdk v3: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/

like image 93
ZHENK Avatar answered Feb 09 '26 06:02

ZHENK


This is what worked for me

import { S3, GetObjectCommand } from "@aws-sdk/client-s3";
import { config } from 'dotenv';

// Load Environment Variable From a '.env' file
config();
const s3 = new S3( {
    region: process.env.S3_REGION,
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
});

const params = {
    Bucket: process.env.AWS_BUCKET_NAME,
    Key: key,
}
const command = new GetObjectCommand( params );
const response = await s3.send( command);

// More Logic Here

const stream = Readable.from( response.Body );
like image 23
Emmanuel Chalo Avatar answered Feb 09 '26 07:02

Emmanuel Chalo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!