Filestorage.js
A JavaScript library for using SKALE Network’s native and decentralized onchain filestorage.
For an example of how to use filestorage, checkout using filestorage.
Overview
Section titled “Overview”Storing files on the blockchain is possible within the SKALE Network. You can use SKALE to host your text, image, HTML, and other file formats through the Filestorage.js NPM package.
See the file storage demo on GitHub.
Installation
Section titled “Installation”npm install @skalenetwork/filestorage.js@~1.0.1
Once installed, you can instantiate the client by passing a SKALE endpoint into the constructor.
JavaScript
Section titled “JavaScript”const Filestorage = require('@skalenetwork/filestorage.js');const Web3 = require('web3');
const web3Provider = new Web3.providers.HttpProvider('----SKALE ENDPOINT----');let filestorage = new Filestorage(web3Provider);
// Directly with http(s)/ws(s) endpointslet filestorage = new Filestorage('----HTTP(s)/WS(s) SKALE ENDPOINT----');
<script src="PATH_TO_PACKAGE/@skalenetwork/filestorage.js/dist/filestorage.min.js"></script><script type="text/javascript"> async function downloadFile() { let fs = new filestorage('----SKALE ENDPOINT----', true); await fs.downloadToFile('----STORAGE_PATH----'); }</script>
Upload Files
Section titled “Upload Files”Uploading files can be accomplished using the uploadFile
method available within the Filestorage.js NPM package.
JavaScript
Section titled “JavaScript”<input onChange={(e) => upload(e)} type="file" id="files" />
async function upload(event, specificDirectory=''){ event.preventDefault(); const web3Provider = new Web3.providers.HttpProvider("[YOUR_SKALE_CHAIN_ENDPOINT]"); let web3 = new Web3(web3Provider); let filestorage = new Filestorage(web3, true); let privateKey = '0x' + '[YOUR_PRIVATE_KEY]'; let account = "[YOUR_ACCOUNT_ADDRESS]"; let file = document.getElementById('files').files[0]; let reader = new FileReader(); let filePath = specificDirectory ? `${specificDirectory}/${file.name}` : file.name; reader.onload = async function(e) { const bytes = new Uint8Array(reader.result); let link = filestorage.uploadFile(account, filePath, bytes, privateKey); }; reader.readAsArrayBuffer(file);}
Show Contents
Section titled “Show Contents”async function getFiles(storagePath){ const web3Provider = new Web3.providers.HttpProvider("[YOUR_SKALE_CHAIN_ENDPOINT]"); let web3 = new Web3(web3Provider); let filestorage = new Filestorage(web3, true); let account = "[YOUR_ACCOUNT_ADDRESS]"; let files = await filestorage.listDirectory(storagePath);}
Download Files
Section titled “Download Files”async function downloadFileToDesktop(storagePath) { const web3Provider = new Web3.providers.HttpProvider("[YOUR_SKALE_CHAIN_ENDPOINT]"); let web3 = new Web3(web3Provider); let filestorage = new Filestorage(web3, true); await filestorage.downloadToFile(storagePath);}
async function downloadFileToVariable(storagePath) { const web3Provider = new Web3.providers.HttpProvider("[YOUR_SKALE_CHAIN_ENDPOINT]"); let web3 = new Web3(web3Provider); let filestorage = new Filestorage(web3, true); let file = await filestorage.downloadToBuffer(storagePath); file = 'data:image/png;base64,' + file.toString('base64');}
Delete Files
Section titled “Delete Files”async function deleteFile(filePath) { const web3Provider = new Web3.providers.HttpProvider("[YOUR_SKALE_CHAIN_ENDPOINT]"); let web3 = new Web3(web3Provider); let filestorage = new Filestorage(web3, true); let privateKey = '[YOUR_PRIVATE_KEY]'; let account = "[YOUR_ACCOUNT_ADDRESS]"; await filestorage.deleteFile(account, filePath, privateKey);}
Create Directory
Section titled “Create Directory”async function createDirectory(directoryPath) { const web3Provider = new Web3.providers.HttpProvider("[YOUR_SKALE_CHAIN_ENDPOINT]"); let web3 = new Web3(web3Provider); let filestorage = new Filestorage(web3, true); let privateKey = '[YOUR_PRIVATE_KEY]'; let account = "[YOUR_ACCOUNT_ADDRESS]"; await filestorage.createDirectory(account, directoryPath, privateKey);}
Delete Directory
Section titled “Delete Directory”async function deleteDirectory(directoryPath) { const web3Provider = new Web3.providers.HttpProvider("[YOUR_SKALE_CHAIN_ENDPOINT]"); let web3 = new Web3(web3Provider); let filestorage = new Filestorage(web3, true); let privateKey = '[YOUR_PRIVATE_KEY]'; let account = "[YOUR_ACCOUNT_ADDRESS]"; await filestorage.deleteDirectory(account, directoryPath, privateKey);}
Reserve Space
Section titled “Reserve Space”async function reserveSpace(addressToReserve, reservedSpace) { const web3Provider = new Web3.providers.HttpProvider("[YOUR_SKALE_CHAIN_ENDPOINT]"); let web3 = new Web3(web3Provider); let filestorage = new Filestorage(web3, true); let privateKey = '[YOUR_PRIVATE_KEY]'; let account = "[YOUR_ACCOUNT_ADDRESS]"; await filestorage.reserveSpace(account, addressToReserve, reservedSpace, privateKey);}
Additional Notes
Section titled “Additional Notes”Storage Path
Section titled “Storage Path”Storage path is a label used to point to a file or directory in Filestorage. It contains two parts separated by a slash:
- File owner address (LOWERCASE & without
0x
) - File/directory path in owner’s root directory
Examples:
77333da3492c4bbb9ccf3ea5bb63d6202f86cda8/directoryA/random_text.txt77333da3492c4bbb9ccf3ea5bb63d6202f86cda8/random_text.txt0x77333da3492c4bbb9ccf3ea5bb63d6202f86cda8/random_text.txt # Invalid storage path