In this tutorial, we will demonstrate how to upload and download files using the ethstorage-sdk tool.
The ethstorage-sdk provides APIs for file upload, download, and management, which are built on c-kzg and, therefore, are not compatible with a browser environment.
Step 1: Install ethstorage-sdk
You can install ethstorage-sdk by the following command:
npmiethstorage-sdk
Step 2: Upload Files
const {BlobUploader,EncodeBlobs} =require("ethstorage-sdk");constupload=async (filePath) => {constrpc="https://rpc.sepolia.org";constprivateKey="0x...";consttx= { value: cost, to: to, data: data, };constcontent=fs.readFileSync(filePath);constblobs=EncodeBlobs(content);constblobUploader=newBlobUploader(rpc, privateKey);consthash=awaitblobUploader.sendTx(tx, [blobs[0], blobs[1]...) // max is 6// hash : 0x37df32c7a3c30d3...52453dadacc838461d8629016}
Step 3: Manage Files
3.1 Create Flat Directory
In this section, you will create a FlatDirectory contract for managing files.
In this section, you will download files from the ethstorage network.
const {DownloadFile} =require("ethstorage-sdk");constdownload=async (fileName) => {constflatDirectory="0x37df32c7a3c30d352453dadacc838461d8629016";// Note: This should be ethstorage rpc, not eth rpcconstrpc="https://ethstorage.rpc.io";constdata=awaitDownloadFile(rpc, flatDirectory, fileName);// You can save the data as a file}