The example belows shows how we would get an object/file called list-of-cats.txt and save it to a file called list-of-cats.txt.
index.ts
Copy
import { StorageSyncClient } from "@storagesync/client";import fs from "fs";const client = new StorageSyncClient({ apiKey: "<YOUR_API_KEY>", bucket: "<YOUR_BUCKET_NAME>",});const response = await client.download("list-of-cats.txt");// The response will be a standard fetch response object// with the body being the data for the object.const data = await response.text();fs.writeFileSync("list-of-cats.txt", data);// Now there should be a file called list-of-cats.txt in the current directory.
Below is an example of how we would use streams to download a big file without having to load it all into memory.
index.ts
Copy
async function downloadBigFile() { // Fetch the file from StorageSync const res = await client.download("big-file.dmg"); if (res.status === 200) { // File was was found and we can start streaming it. // Create a write stream to a file on our local machine. const writer = fs.createWriteStream("./big-file.dmg"); // Get the readable stream from the response. const reader = res.body.getReader(); if (reader) { while (true) { // Read from the stream. const { done, value } = await reader.read(); if (done || !value) { reader.releaseLock(); break; } writer.write(value); } } writer.close(); }}
For more examples checkout out our Examples/Guides section