RobbieR
Deno13mo ago
Robbie

Replacing `fs.createWriteStream()` with Deno equivalent

Hi, I am trying to download files from S3 to disk and it works if I use
fs.createWriteStream()
as follows:

import * as fs from "node:fs";
import { S3 } from "npm:@aws-sdk/client-s3@3.701.0";

const s3 = new S3();
const response = await s3.getObject({ Bucket: "mybucket", Key: "mykey" });
const result = await response.Body.pipe(fs.createWriteStream("out.dat"));


How would I achieve the same thing with Deno's standard library instead of
node:fs
? I have tried the following:

const s3 = new S3();
const response = await s3.getObject({ Bucket: "mybucket", Key: "mykey" });
const output = await Deno.open("out.dat", { write: true, create: true });
const outputWriter = output.writable.getWriter();
await outputWriter.ready;
const result = await response.Body.pipe(outputWriter);


But I get an error:

Stack trace:
TypeError: dest.on is not a function
    at IncomingMessageForClient.Readable.pipe (ext:deno_node/_stream.mjs:2688:12)
    at <anonymous>:11:36
    at eventLoopTick (ext:core/01_core.js:175:7)


Any ideas?
Was this page helpful?