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.

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

Upload Files

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

async function upload(){

  //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);

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

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

  //file reader method to upload file
  reader.onload = async function(e) {
      let link = filestorage.uploadFile(
        account, 
        file.name, 
        file.size, 
        reader.result 
        true, 
        pivateKey
      );
  };
  reader.readAsArrayBuffer(file);
}

Show Files

Displaying files can be accomplished by using the FilestorageClient.getFileInfoByAddress 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 FilestorageClient(web3);

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

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

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 FilestorageClient(web3);

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

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

Download Files

Downloading files can be accomplished by using the FilestorageClient.downloadFileIntoBrowser or the FilestorageClient.downloadFileIntoBuffer 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 FilestorageClient(web3);

  await filestorage.downloadFileIntoBrowser(link, true);
}

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);

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