EthStorage
  • Overview
    • Key Terms
    • How EthStorage Works
    • Overview FAQ
    • Support
    • Whitepaper
  • Storage Provider Guide
    • Storage Provider Tutorial
    • Configuration
    • Storage Provider FAQ
  • Dapp Developer Guide
    • Introduction
    • Tutorials
      • Upload your file/folder with ethfs-cli
      • Use ethstorage-sdk to upload and download files
  • Rollup Guide
    • OP Stack Tutorial
  • Information
Powered by GitBook
On this page
  • Introduction
  • Step 1: Install ethstorage-sdk
  • Step 2: Manage Files
  • 2.1 Create FlatDirectory
  • 2.2: Upload Files To FlatDirectory
  • 2.3: Download Files From FlatDirectory
  • Step 3: Manage Blobs
  • 3.1 Create EthStorage
  • 3.2 Write Blob
  • 3.3 Read Blob
  1. Dapp Developer Guide
  2. Tutorials

Use ethstorage-sdk to upload and download files

PreviousUpload your file/folder with ethfs-cliNextRollup Guide

Last updated 2 months ago

Introduction

In this tutorial, we will demonstrate how to upload and download files using the tool.

The ethstorage-sdk provides APIs for file upload, download, and management.

In the following code examples, beta testnet is used by default. You can easily switch to other chains by specify a different RPC endpoint, such as "https://rpc.sepolia.org" for Sepolia.

Step 1: Install ethstorage-sdk

You can install ethstorage-sdk by the following command:

 npm i ethstorage-sdk

Step 2: Manage Files

2.1 Create FlatDirectory

In this section, you will create a FlatDirectory contract for managing files.

const { FlatDirectory } = require("ethstorage-sdk");

const rpc = "https://rpc.beta.testnet.l2.quarkchain.io:8545";
// For Sepolia:
// const rpc = "https://rpc.sepolia.org";
const privateKey = "0x...";

const flatDirectory = await FlatDirectory.create({
    rpc: rpc,
    privateKey: privateKey,
});
const contracAddress = await flatDirectory.deploy();
console.log(`FlatDirectory address: ${contracAddress}.`);
// FlatDirectory address: 0x37df32c7a3c30d352453dadacc838461d8629016

If you already have a FlatDirectory contract, you can set it.

const address = "0x37df32c7a3c30d352453dadacc838461d8629016"; // FlatDirectory address

const flatDirectory = await FlatDirectory.create({
    rpc: rpc,
    privateKey: privateKey,
    address: address,
});

2.2: Upload Files To FlatDirectory

In this section, you will upload some files into the FlatDirectory that you just created.

You can use Buffer to upload files.

const request = {
    key: "test.txt",
    content: Buffer.from("big data"),
    type: 2, // 1 for calldata and 2 for blob
    callback: callback
}
await flatDirectory.upload(request);

You can also upload File objects. This applies to both browser and Node.js environments.

Browser

// <input id='fileToUpload' />
const file = document.getElementById('fileToUpload').files[0];

const request = {
    key: "test.txt",
    content: file,
    type: 2,
    callback: callback
}
await flatDirectory.upload(request);

Node.js

const {NodeFile} = require("ethstorage-sdk/file");
const file = new NodeFile("/usr/download/test.jpg");

const request = {
    key: "test.jpg",
    content: file,
    type: 2,
    callback: callback
}
await flatDirectory.upload(request);
const callback = {
    onProgress: function (progress, count, isChange) {
       ...
    },
    onFail: function (err) {
        ...
    },
    onFinish: function (totalUploadChunks, totalUploadSize, totalStorageCost) {
        ...
    }
};

2.3: Download Files From FlatDirectory

In this section, you will download files from the ethstorage network.

// Note: To download files, you need to specify the `ethStorageRpc`.
// For Sepolia:
// const ethStorageRpc = "http://65.108.236.27:9540";
const ethStorageRpc = "https://rpc.beta.testnet.l2.ethstorage.io:9596";
const flatDirectory = await FlatDirectory.create({
    rpc: rpc,
    ethStorageRpc: ethStorageRpc,
    privateKey: privateKey,
    address: address,
});

const key = "test.txt";
await flatDirectory.download(key, {
    onProgress: function (progress, count, chunk) {
        ...
    },
    onFail: function (error) {
        ...
    },
    onFinish: function () {
        ...
    }
});

Step 3: Manage Blobs

3.1 Create EthStorage

In this section, you can create an EthStorage instance to interact directly with EthStorage for managing blobs.

const { EthStorage } = require("ethstorage-sdk");

// For Sepolia:
// const rpc = "https://rpc.sepolia.org";
const rpc = "https://rpc.beta.testnet.l2.quarkchain.io:8545";

// For Sepolia:
// const ethStorageRpc = "http://65.108.236.27:9540";
const ethStorageRpc = "https://rpc.beta.testnet.l2.ethstorage.io:9596";

const privateKey = "0x...";

const ethStorage = await EthStorage.create({
    rpc: rpc,
    ethStorageRpc: ethStorageRpc,
    privateKey: privateKey,
});

3.2 Write Blob

Write blob data to the EthStorage network.

const key = "test.txt";
const data = Buffer.from("test data");
await ethStorage.write(key, data);

3.3 Read Blob

Read the written data from the EthStorage network.

const key = "test.txt";
const data = await ethStorage.read(key);
ethstorage-sdk
Super World Computer