shell

package module
v0.0.0-...-16d1cd6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2021 License: MIT Imports: 19 Imported by: 0

README

go-mefs-api

A go interface to mefs's HTTP API

install

go get -u github.com/memoio/mefs-go-http-client

prepare

To interact with the API, you need to have a local daemon running. It needs to be open on the right port. 5001 is the default, and is used in the examples below, but it can be set to whatever you need.

# Show the mefs config API port
> mefs config Addresses.API
/ip4/127.0.0.1/tcp/5001
# set api port and binding to all ip 
> mefs config Addresses.API /ip4/0.0.0.0/tcp/5001
# Restart the daemon after changing the config
> mefs shutdown
# Run the daemon
> mefs daemon
CORS

In a web browser mefs HTTP client (either browserified or CDN-based) might encounter an error saying that the origin is not allowed. This would be a CORS ("Cross Origin Resource Sharing") failure: mefs servers are designed to reject requests from unknown domains by default. You can whitelist the domain that you are calling from by changing your mefs config like this:

> mefs config --json API.HTTPHeaders.Access-Control-Allow-Origin  '["http://example.com"]'
> mefs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "POST", "GET"]'
# Restart the daemon after changing the config
> mefs shutdown
# Run the daemon
> mefs daemon

example

see example directory

package main

import (
	"fmt"
	"os"
	"path"
	"time"

	"github.com/memoio/mefs-go-http-client"
)

func main() {
	
	// your local address
	p := path.Join(os.Getenv("HOME"), "test")
	file, err := os.Open(p)
	ob, err := sh.PutObject(file, path.Base(file.Name()), "bucket01")
	fmt.Println(ob, err)
	bks, err := sh.ListBuckets()
	fmt.Println(bks, err)
	obs, err := sh.ListObjects(bks.Buckets[0].BucketName)
	fmt.Println(obs, err)
}

Usage

See mefs docs

LFS

The API enables users to use the LFS abstraction of MEFS.

StartUser

start user's lfs service

CreateBucket(addr,ops...)

addr AddressID. Initialize user service with the given address. Type is string.

options is an optional object argument that might include the following keys:

  • pwd PassWord. Password of the actual user that you want to execute. Type is string.
  • sk SecreteKey. Private key of the actual user that you want to execute. Type is string. If sk is not nil, mefs will store sk in the keystore with pwd; otherwise, mefs tries to load the private key from the keystore use the addr and pwd.
	sh = shell.NewShell("localhost:5001")
	
	// set multiple parameters
	op1 := shell.SetOp(option1, opsValue1)
	op2 := shell.SetOp(option2, opsValue2)
	...
	// start user
	sh.StartUser(addr, op1, op2,...)
CreateBucket

create a bucket in lfs.

CreateBucket(bucketName,ops...)

bucketName is a string of the bucket name we want.

options is an optional object argument that might include the following keys:

  • addr AddressID. The actual user's addressid that you want to execute. Type is string.
  • pl Policy. The Storage policy you want to use. Type is bool. truefor erasure coding and false for copyset. default is true.
  • dc DataCount. default is 3.
  • pc ParityCount. default is 2.
DeleteBucket

delete a bucket in lfs.

DeleteBucket(bucketName,ops...)

bucketname is a string of the bucket name .

options is an optional object argument that might include the following keys:

  • addr AddressID. The actual user's addressid that you want to execute. Type is string.
PutObject

put an object to a bucket

PutObject(data, objectName, bucketName, ops...)

data is the data we want to store. bucketName is a string of the bucket name. objectName is a string of the object name.

options is an optional object argument that might include the following keys:

  • addr AddressID. The actual user's addressid that you want to execute. Type is string.
GetObject

get an object in a bucket.

GetObject(objectName, bucketName, ops...)

bucketName is a string of the bucket name. objectName is a string of the object name.

options is an optional object argument that might include the following keys:

  • addr AddressID. The actual user's addressid that you want to execute. Type is string.
DeleteObject

delete an object in a bucket.

DeleteObject(bucketName,objectName)

bucketName is a string of the bucket name. objectName is a string of the object name.

Documentation

Overview

该文件用于测试mefs各种功能

package shell implements a remote API interface for a running ipfs daemon

Index

Constants

View Source
const (
	TESTBUCKET     = "b0"
	RandomDataSize = 1024 * 1024 * 100
)
View Source
const (
	DefaultPathName = ".mefs"
	DefaultPathRoot = "~/" + DefaultPathName
	DefaultApiFile  = "api"
	EnvDir          = "MEFS_PATH"
)
View Source
const (
	DirectPin    = "direct"
	RecursivePin = "recursive"
	IndirectPin  = "indirect"
)

Variables

This section is empty.

Functions

func ReslultSumaryTest

func ReslultSumaryTest(userPort, keeperPort string)

测试时空值的计算,流程:没过一段时间,user发送一段数据,keeper进行一次计算,将实际值与理论值对比 目前使用metb进行测试,传入进行测试的user和keeper端口

Types

type BlockStat

type BlockStat struct {
	Key  string
	Size int
}

type BucketStat

type BucketStat struct {
	Name        string
	BucketID    int64
	Ctime       string
	Policy      int32
	DataCount   int32
	ParityCount int32
	Encryption  int32
}

func (BucketStat) String

func (bk BucketStat) String() string

type Buckets

type Buckets struct {
	Method  string
	Buckets []BucketStat
}

func (Buckets) String

func (bus Buckets) String() string

type Error

type Error struct {
	Command string
	Message string
	Code    int
}

func (*Error) Error

func (e *Error) Error() string

type GetBlockResult

type GetBlockResult struct {
	IsExist bool
}

type IdOutput

type IdOutput struct {
	NetworkAddr  string
	AccountAddr  string
	PublicKey    string
	Addresses    []string
	AgentVersion string
}

type IntList

type IntList struct {
	ChildLists []int
}

func (IntList) String

func (fl IntList) String() string

type LfsOpts

type LfsOpts = func(*RequestBuilder) error

func ForceFlush

func ForceFlush(enabled bool) LfsOpts

func SetAddress

func SetAddress(addr string) LfsOpts

func SetAvailTime

func SetAvailTime(enabled bool) LfsOpts

func SetCrypto

func SetCrypto(crypto bool) LfsOpts

func SetDataCount

func SetDataCount(dataCount int) LfsOpts

func SetObjectName

func SetObjectName(objectName string) LfsOpts

func SetOp

func SetOp(key, value string) LfsOpts

func SetParityCount

func SetParityCount(parityCount int) LfsOpts

func SetPassword

func SetPassword(pwd string) LfsOpts

func SetPolicy

func SetPolicy(policy int) LfsOpts

func SetPrefixFilter

func SetPrefixFilter(prefix string) LfsOpts

func SetSecretKey

func SetSecretKey(sk string) LfsOpts

func UseErasureCodeOrMulRep

func UseErasureCodeOrMulRep(enabled bool) LfsOpts

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is used to handle incoming logs from the ipfs node

func (Logger) Close

func (l Logger) Close() error

Close is used to close our reader

func (Logger) Next

func (l Logger) Next() (map[string]interface{}, error)

Next is used to retrieve the next event from the logging system

type ObjectStat

type ObjectStat struct {
	Name           string
	Size           int64
	MD5            string
	Ctime          string
	Dir            bool
	LatestChalTime string
}

func (ObjectStat) String

func (ob ObjectStat) String() string

type Objects

type Objects struct {
	Method  string
	Objects []ObjectStat
}

func (Objects) String

func (obs Objects) String() string

type PeerInfo

type PeerInfo struct {
	Addrs []string
	ID    string
}

type PeerList

type PeerList struct {
	Peers []PeerState
}

func (PeerList) String

func (pl PeerList) String() string

type PeerState

type PeerState struct {
	PeerID    string
	Connected bool
}

func (PeerState) String

func (ps PeerState) String() string

type PeersList

type PeersList struct {
	Peers []string
}

type Request

type Request struct {
	Ctx     context.Context
	ApiBase string
	Command string
	Args    []string
	Opts    map[string]string
	Body    io.Reader
	Headers map[string]string
}

func NewRequest

func NewRequest(ctx context.Context, url, command string, args ...string) *Request

func (*Request) Send

func (r *Request) Send(c *http.Client) (*Response, error)

type RequestBuilder

type RequestBuilder struct {
	// contains filtered or unexported fields
}

RequestBuilder is an IPFS commands request builder.

func (*RequestBuilder) Arguments

func (r *RequestBuilder) Arguments(args ...string) *RequestBuilder

Arguments adds the arguments to the args.

func (*RequestBuilder) Body

func (r *RequestBuilder) Body(body io.Reader) *RequestBuilder

Body sets the request body to the given reader.

func (*RequestBuilder) BodyBytes

func (r *RequestBuilder) BodyBytes(body []byte) *RequestBuilder

BodyBytes sets the request body to the given buffer.

func (*RequestBuilder) BodyString

func (r *RequestBuilder) BodyString(body string) *RequestBuilder

BodyString sets the request body to the given string.

func (*RequestBuilder) Exec

func (r *RequestBuilder) Exec(ctx context.Context, res interface{}) error

Exec sends the request a request and decodes the response.

func (*RequestBuilder) Header

func (r *RequestBuilder) Header(name, value string) *RequestBuilder

Header sets the given header.

func (*RequestBuilder) Option

func (r *RequestBuilder) Option(key string, value interface{}) *RequestBuilder

Option sets the given option.

func (*RequestBuilder) Send

func (r *RequestBuilder) Send(ctx context.Context) (*Response, error)

Send sends the request and return the response.

type Response

type Response struct {
	Output io.ReadCloser
	Error  *Error
}

func (*Response) Close

func (r *Response) Close() error

func (*Response) Decode

func (r *Response) Decode(dec interface{}) error

type Shell

type Shell struct {
	// contains filtered or unexported fields
}

func NewLocalShell

func NewLocalShell() *Shell

func NewShell

func NewShell(url string) *Shell

func NewShellWithClient

func NewShellWithClient(url string, c *gohttp.Client) *Shell

func (*Shell) BlockGet

func (s *Shell) BlockGet(path string) ([]byte, error)

func (*Shell) BlockPut

func (s *Shell) BlockPut(block []byte, format, mhtype string, mhlen int) (string, error)

func (*Shell) BlockStat

func (s *Shell) BlockStat(path string) (string, int, error)

func (*Shell) BootstrapAdd

func (s *Shell) BootstrapAdd(peers []string) ([]string, error)

func (*Shell) BootstrapAddDefault

func (s *Shell) BootstrapAddDefault() ([]string, error)

func (*Shell) BootstrapRmAll

func (s *Shell) BootstrapRmAll() ([]string, error)

func (*Shell) CreateBucket

func (s *Shell) CreateBucket(BucketName string, options ...LfsOpts) (*Buckets, error)

func (*Shell) CreateUser

func (s *Shell) CreateUser(options ...LfsOpts) (*UserPrivMessage, error)

func (*Shell) DeleteBucket

func (s *Shell) DeleteBucket(BucketName string, options ...LfsOpts) (*Buckets, error)

func (*Shell) DeleteFrom

func (s *Shell) DeleteFrom(key, to string, options ...LfsOpts) (string, error)

func (*Shell) DeleteObject

func (s *Shell) DeleteObject(ObjectName, BucketName string, options ...LfsOpts) (*Objects, error)

func (*Shell) FindPeer

func (s *Shell) FindPeer(peer string) (*PeerInfo, error)

func (*Shell) Fsync

func (s *Shell) Fsync(options ...LfsOpts) error

func (*Shell) GenShare

func (s *Shell) GenShare(ObjectName, BucketName string, options ...LfsOpts) (string, error)

func (*Shell) GetBlockFrom

func (s *Shell) GetBlockFrom(key, id string, options ...LfsOpts) (string, error)

func (*Shell) GetFrom

func (s *Shell) GetFrom(key, id string, options ...LfsOpts) (*queryEvent, error)

func (*Shell) GetLogs

func (s *Shell) GetLogs(ctx context.Context) (Logger, error)

GetLogs is used to retrieve a parsable logger object

func (*Shell) GetObject

func (s *Shell) GetObject(ObjectName, BucketName string, options ...LfsOpts) (io.ReadCloser, error)

func (*Shell) GetObjectToFile

func (s *Shell) GetObjectToFile(ObjectName, BucketName, outPath string, options ...LfsOpts) error

func (*Shell) GetShare

func (s *Shell) GetShare(shareLink, outputName string, options ...LfsOpts) (io.ReadCloser, error)

func (*Shell) HeadBucket

func (s *Shell) HeadBucket(BucketName string, options ...LfsOpts) (*Buckets, error)

func (*Shell) HeadObject

func (s *Shell) HeadObject(ObjectName, BucketName string, options ...LfsOpts) (*Objects, error)

func (*Shell) ID

func (s *Shell) ID(peer ...string) (*IdOutput, error)

ID gets information about a given peer. Arguments:

peer: peer.ID of the node to look up. If no peer is specified,

return information about the local peer.

func (*Shell) IsUp

func (s *Shell) IsUp() bool

func (*Shell) Kill

func (s *Shell) Kill(addr string, options ...LfsOpts) (*StringList, error)

func (*Shell) ListBuckets

func (s *Shell) ListBuckets(options ...LfsOpts) (*Buckets, error)

func (*Shell) ListKeepers

func (s *Shell) ListKeepers(options ...LfsOpts) (*PeerList, error)

func (*Shell) ListObjects

func (s *Shell) ListObjects(BucketName string, options ...LfsOpts) (*Objects, error)

func (*Shell) PutObject

func (s *Shell) PutObject(r io.Reader, ObjectName, BucketName string, options ...LfsOpts) (*Objects, error)

func (*Shell) Request

func (s *Shell) Request(command string, args ...string) *RequestBuilder

func (*Shell) ResolvePath

func (s *Shell) ResolvePath(path string) (string, error)

func (*Shell) ResultSummary

func (s *Shell) ResultSummary() int

keeper计算时空值命令,用于测试,返回计算好的时空值

func (*Shell) SetTimeout

func (s *Shell) SetTimeout(d time.Duration)

func (*Shell) ShowStorage

func (s *Shell) ShowStorage(options ...LfsOpts) (string, error)

func (*Shell) StartUser

func (s *Shell) StartUser(address string, options ...LfsOpts) error

func (*Shell) SwarmConnect

func (s *Shell) SwarmConnect(ctx context.Context, addr ...string) error

SwarmConnect opens a swarm connection to a specific address.

func (*Shell) SwarmPeers

func (s *Shell) SwarmPeers(ctx context.Context) (*SwarmConnInfos, error)

SwarmPeers gets all the swarm peers

func (*Shell) TestLocalinfo

func (s *Shell) TestLocalinfo()

获取节点信息的操作

func (*Shell) Version

func (s *Shell) Version() (string, string, error)

returns ipfs version and commit sha

type StringList

type StringList struct {
	ChildLists []string
}

func (StringList) String

func (fl StringList) String() string

type SwarmConnInfo

type SwarmConnInfo struct {
	Addr    string
	Peer    string
	Latency string
	Muxer   string
	Streams []SwarmStreamInfo
}

type SwarmConnInfos

type SwarmConnInfos struct {
	Peers []SwarmConnInfo
}

type SwarmStreamInfo

type SwarmStreamInfo struct {
	Protocol string
}

type UserPrivMessage

type UserPrivMessage struct {
	Address string
	Sk      string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL