DEV Community 👩‍💻👨‍💻

Alexey Timin for Reduct Storage

Posted on • Updated on • Originally published at

Reduct Storage Client SDK for JavaScript 0.5 was released

Few days ago I released the next version of the SDK. Traditionally, we update our SDKs after we have introduced new changes in Reduct Storage HTTP API. But this release is a bit special because we added streaming data to there. Let'see how we can use it.

If you're new to Reduct Storage, you can have a look at this tutorial before we start.

Reading Data

This example shows the easiest way to read a record from the storage engine and write it into a file:

const {Client} = require("reduct-js");
const fs = require("fs");

client = new Client("");
const bucket = await client.getBucket("bucket");

const readStream = await bucket.readStream("entry-name");
const fileStream = fs.createWriteStream("somefile.txt");
Enter fullscreen mode Exit fullscreen mode

As you can see, we created a stream from the file and piped it with the record, which is read as a stream. Then we downloaded the record and wrote it to the file asynchronously in little chunks.

Writing Data

You can write data to the storage engine with streams as well. However, you must provide the content length at the beginning of the write operation. It means you have to know the size of the record in advance. Let's see in code:

const bucket = await client.getBucket("bucket");

const fileStream = fs.createReadStream("somefile.txt");
const {size} = fs.statSync("somefile.txt");

await bucket.writeStream("entry-name", fileStream, size);
Enter fullscreen mode Exit fullscreen mode

Here we read the same file as a stream and uploaded it to the storage engine. The syntax is a bit different, but essentially, it works like pipe().

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.