File Storage API

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 file-storage npm package.
See the file storage demo on Github.

Usage

You have full control over maintaining your files on the SKALE Network, and you can maintain your files by uploading, downloading, or deleting files within your account. Additional documentation on the methods available within File Storage can be found here.

npm i @skalenetwork/filestorage.js

Instantiate the Client

To instantiate the client you only need to pass the SKALE endpoint into the constructor.

const Filestorage = require('@skalenetwork/filestorage.js/src/index');

let filestorage = new Filestorage("[YOUR_SKALE_CHAIN_ENDPOINT]");

Upload Files

Uploading files can be accomplished by using the FilestorageClient.uploadFile method available within the NPM package.

//Input field to add to your HTML
<input onChange={(e) => upload(e)} 
type="file" id="files" / >

//JavaScript function for handling the file upload
async function upload(event){
  event.preventDefault();
  //create web3 connection
  const web3Provider = new Web3.providers.HttpProvider(
    "[YOUR_SKALE_CHAIN_ENDPOINT]"
  );
  let web3 = new Web3(web3Provider);

  //get filestorage instance
  let filestorage = new Filestorage(web3, true);

  //provide your account & private key
  //note this must include the 0x prefix
  let privateKey = '0x' + '[YOUR_PRIVATE_KEY]';
  let account = "[YOUR_ACCOUNT_ADDRESS]";

  //get file data from file upload input field
  let file = document.getElementById('files').files[0];
  let reader = new FileReader();

  //file storage method to upload file
  reader.onload = async function(e) {
    const arrayBuffer = reader.result
    const bytes = new Uint8Array(arrayBuffer);
    let link = filestorage.uploadFile(
      account, 
      file.name, 
      bytes,
      pivateKey
    );
  };
  reader.readAsArrayBuffer(file);
}

Show Files

Displaying files can be accomplished by using the FilestorageClient.getFileInfoListByAddress method available within the NPM package.

async function getFiles(){
  //create web3 connection
  const web3Provider = new Web3.providers.HttpProvider(
    "[YOUR_SKALE_CHAIN_ENDPOINT]"
  );
  let web3 = new Web3(web3Provider);

  //get filestorage instance
  let filestorage = new Filestorage(web3, true);

  //provide your account & private key
  let account = "[YOUR_ACCOUNT_ADDRESS]";

  let files = await filestorage.getFileInfoListByAddress(account);
}

Download Files

Downloading files can be accomplished by using the FilestorageClient.downloadToFile or the FilestorageClient.downloadToBuffer method available within the NPM package.

async function downloadFileToDesktop(link) {
  //create web3 connection
  const web3Provider = new Web3.providers.HttpProvider(
    "[YOUR_SKALE_CHAIN_ENDPOINT]"
  );
  let web3 = new Web3(web3Provider);

  //get filestorage instance
  let filestorage = new Filestorage(web3, true);

  await filestorage.downloadToFile(link);
}

async function downloadFileToVariable(link) {
  //create web3 connection
  const web3Provider = new Web3.providers.HttpProvider(
    "[YOUR_SKALE_CHAIN_ENDPOINT]"
  );
  let web3 = new Web3(web3Provider);

  //get filestorage instance
  let filestorage = new FilestorageClient(web3, true);

  let file = await filestorage.downloadToBuffer(link);
  file = 'data:image/png;base64,' + file.toString('base64');
}

Delete Files

Deleting files can be accomplished by using the FilestorageClient.deleteFile method available within the NPM package.

async function deleteFile(fileName) {
  //create web3 connection
  const web3Provider = new Web3.providers.HttpProvider(
    "[YOUR_SKALE_CHAIN_ENDPOINT]"
  );
  let web3 = new Web3(web3Provider);

  //get filestorage instance
  let filestorage = new Filestorage(web3, true);

  //provide your account & private key
  //note this must include the 0x prefix
  let privateKey = '[YOUR_PRIVATE_KEY]';
  let account = "[YOUR_ACCOUNT_ADDRESS]";

  await filestorage.deleteFile(account, fileName, pivateKey);
}