API Integration

Installation

To use the titan storage sdk, you'll first need to install Go and set up a Go development environment. Once you have Go installed and configured, you can install the titan storage sdk using Go modules:

go get github.com/Filecoin-Titan/titan-storage-sdk

API

UploadFilesWithPath(ctx context.Context, filePath string, progress ProgressFunc) (cid.Cid, error)
UploadFileWithURL(ctx context.Context, url string, progress ProgressFunc) (string, string, error)
UploadStream(ctx context.Context, r io.Reader, name string, progress ProgressFunc) (cid.Cid, error)
ListUserAssets(ctx context.Context, limit, offset int) (*client.ListAssetRecordRsp, error)
Delete(ctx context.Context, rootCID string) error
GetURL(ctx context.Context, rootCID string) (string, error)
GetFileWithCid(ctx context.Context, rootCID string) (io.ReadCloser, error)
package main

import (
	"context"
	"flag"
	"fmt"
	"os"

	storage "github.com/Filecoin-Titan/titan-storage-sdk"
)

func main() {
	titanURL := os.Getenv("TITAN_URL")
	apiKey := os.Getenv("API_KEY")

	if len(titanURL) == 0 {
		fmt.Println("please set environment variable TITAN_URL, example: export TITAN_URL=Your_titan_url")
		return
	}

	if len(apiKey) == 0 {
		fmt.Println("please set environment variable API_KEY, example: export API_KEY=Your_API_KEY")
		return
	}

	flag.Parse()
	args := flag.Args()
	if len(args) == 0 {
		fmt.Println("Please specify the name of the file to be uploaded")
		return
	}
	filePath := args[0]

	storage, close, err := storage.NewStorage(titanURL, apiKey)
	if err != nil {
		fmt.Println("NewSchedulerAPI error ", err.Error())
		return
	}
	defer close()

	//show upload progress
	progress := func(doneSize int64, totalSize int64) {
		fmt.Printf("upload %d, total %d\n", doneSize, totalSize)
		if doneSize == totalSize {
			fmt.Printf("upload success\n")
		}
	}

	root, err := storage.UploadFilesWithPath(context.Background(), filePath, progress)
	if err != nil {
		fmt.Println("UploadFile error ", err.Error())
		return
	}

	fmt.Printf("upload %s success\n", root.String())
}

Last updated