Skip to content

An example to build or integrate Swan Cloud Storage to Applications

Notifications You must be signed in to change notification settings

swanchain/swan-cloud-storage-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Swan Cloud Storage Example

Discord Twitter Follow standard-readme compliant

This is a example to how to build your own Web3 cloud storage by Swan Cloud Storage Solution, or integrate the Swan Cloud Storage to your applications.


Swan Cloud Storage Solution consists of Multi-Chain Storage(MCS) and MetaArk.

Multi-Chain Storage (MCS) represents a revolutionary Web3 S3 storage gateway fortified by smart contract integration to accelerate decentralized storage adoption.

MetaArk is a robust data archiving and rebuilding service designed to securely store and automatically recover your most critical data assets. It encodes large datasets into CAR files, distributed seamlessly across decentralized storage networks.

Build Web3 Cloud Storage with MCS and MetaArk

Swan Network simplifies integrating decentralized storage into applications with Multi-Chain Storage (MCS) SDK and MetaArk SDK. The steps include:

  • Upload data to IPFS clusters via MCS for availability
  • Back up to the Filecoin network using MetaArk SDK for redundancy
  • Rebuild from MetaArk if data is lost(If needed)

Example

There is a simple case, you can integrate the code to your applications to build your own Web3 Storage solution.

Use Case Code

package main

import (
	"fmt"
	"log"

	"github.com/FogMeta/go-mc-sdk/client"
	"github.com/filswan/go-mcs-sdk/mcs/api/bucket"
	"github.com/filswan/go-mcs-sdk/mcs/api/user"
)

// Acquire MCS api_key 
const (
	mcsAPIKey = "MCS_xxx"

	swanKey   = "xxxxxxxxxxxxx"
	swanToken = "xxxxxxxxxxxxxxxx"

	metaServer = ""
)

func main() {
	bucketName := "bucket_name"
	fileName := "file_name"
	filePath := "/file_path"

	// Initialize the MCS client
	mcsClient, err := user.LoginByApikeyV2(mcsAPIKey, "")
	if err != nil {
		log.Println(err)
		return
	}
	bucketClient := bucket.GetBucketClient(*mcsClient)
	// if no bucket,then create it
	_, err = bucketClient.CreateBucket(bucketName)
	if err != nil {
		log.Println(err)
		return
	}
	// upload file to your bucket
	if err = bucketClient.UploadFile(bucketName, fileName, filePath, true); err != nil {
		log.Println(err)
		return
	}
	// get file info from the bucket
	info, err := bucketClient.GetFile(bucketName, fileName)
	if err != nil {
		log.Println(err)
		return
	}
	log.Printf("uploaded info :%#v\n", info)

	// get the download url for uploaded files
	gateway, err := bucketClient.GetGateway()
	if err != nil {
		log.Println(err)
		return
	}
	downloadURL := fmt.Sprintf("%s/ipfs/%s", *gateway, info.PayloadCid)

	// backup the file to Filecoin network by MetaArk
	datasetName := "my_dataset_name"
	metaClient := client.NewClient(swanKey, swanToken, &client.MetaConf{
		MetaServer: metaServer,
	})
	dataset_id, err := metaClient.Backup(datasetName, &client.IpfsData{
		DownloadUrl: downloadURL,
	})
	if err != nil {
		log.Println(err)
		return
	}

	log.Printf("backup id :%#v\n", dataset_id)

	// list the backups info 
	data, err := metaClient.List(datasetName, 0, 10)
	if err != nil {
		log.Println(err)
		return
	}
	log.Printf("backup list info :%#v\n", data)

	if len(data.DatasetList) == 0 {
		return
	}

	// if needed, you can rebuild dataset from filecoin network by MetaArk
	rebuildList, err := metaClient.Rebuild(dataset_id)
	if err != nil {
		log.Println(err)
		return
	}

	log.Printf("rebuild info :%#v\n", rebuildList)
}

How to get the MCS API_key and MetaArk metaServer

  • MCS API_key: Acquired from "https://www.multichain.storage" -> setting -> Create API Key
  • MetaArk metaServer: if you want to get a availabe metaServer, please open an issue to contact with Swan Cloud team

Use Case

Chainnode as a blockchain snapshots-as-a-service provider, ensuring swift downloads of the most up-to-date node snapshots and blockchain data are available in the future. The storage and backup functions are supported by the MCS and MetaArk.

References

About

An example to build or integrate Swan Cloud Storage to Applications

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published