Documentation ¶
Index ¶
- Constants
- func Concat(strs ...string) string
- func ConcatSlash(strs ...string) string
- func ConfigUserKey(user, keyString string) (*ssh.ClientConfig, error)
- func ConfigUserPass(user, password string) *ssh.ClientConfig
- func NewStore(conf *cloudstorage.Config) (cloudstorage.Store, error)
- type ByModTime
- type Client
- func (m *Client) Client() interface{}
- func (m *Client) Close()
- func (m *Client) Delete(ctx context.Context, filename string) error
- func (m *Client) Exists(filename string) bool
- func (m *Client) Folders(ctx context.Context, q cloudstorage.Query) ([]string, error)
- func (m *Client) Get(ctx context.Context, name string) (cloudstorage.Object, error)
- func (m *Client) List(ctx context.Context, q cloudstorage.Query) (*cloudstorage.ObjectsResponse, error)
- func (m *Client) NewObject(objectname string) (cloudstorage.Object, error)
- func (m *Client) NewReader(o string) (io.ReadCloser, error)
- func (m *Client) NewReaderWithContext(ctx context.Context, name string) (io.ReadCloser, error)
- func (m *Client) NewWriter(objectName string, metadata map[string]string) (io.WriteCloser, error)
- func (m *Client) NewWriterWithContext(ctx context.Context, name string, metadata map[string]string, ...) (io.WriteCloser, error)
- func (m *Client) Objects(ctx context.Context, q cloudstorage.Query) (cloudstorage.ObjectIterator, error)
- func (m *Client) String() string
- func (m *Client) Type() string
- type Uploader
Constants ¶
const ( // StoreType = "sftp" this is used to define the storage type to create // from cloudstorage.NewStore(config) StoreType = "sftp" AuthUserKey cloudstorage.AuthMethod = "userkey" AuthUserPass cloudstorage.AuthMethod = "userpass" // ConfKeyUser config key name of the username ConfKeyUser = "user" // ConfKeyPassword config key name of the password ConfKeyPassword = "password" // ConfKeyPrivateKey config key name of the privatekey ConfKeyPrivateKey = "privatekey" // ConfKeyHost config key name of the server host ConfKeyHost = "host" // ConfKeyPort config key name of the sftp port ConfKeyPort = "port" // ConfKeyFolder config key name of the sftp folder ConfKeyFolder = "folder" )
Variables ¶
This section is empty.
Functions ¶
func Concat ¶
Concat concats strings with "/" but ignores empty strings so an input of "portland", "", would yield "portland" instead of "portland/"
func ConcatSlash ¶
ConcatSlash concats strings and ensures ends with "/"
func ConfigUserKey ¶
func ConfigUserKey(user, keyString string) (*ssh.ClientConfig, error)
ConfigUserKey creates ssh config with ssh/private rsa key
func ConfigUserPass ¶
func ConfigUserPass(user, password string) *ssh.ClientConfig
ConfigUserPass creates ssh config with user/password HostKeyCallback was added here https://github.com/golang/crypto/commit/e4e2799dd7aab89f583e1d898300d96367750991 currently we don't check hostkey, but in the future (todo) we could store the hostkey and check on future logins if there is a match.
func NewStore ¶
func NewStore(conf *cloudstorage.Config) (cloudstorage.Store, error)
Types ¶
type Client ¶
type Client struct { ID string // contains filtered or unexported fields }
Client is the sftp client
func NewClient ¶
func NewClient(clientCtx context.Context, conf *cloudstorage.Config, host string, port int, folder string, config *ssh.ClientConfig) (*Client, error)
NewClient returns a new SFTP Client Make sure to close SFTP connection when done
func NewClientFromConfig ¶
NewClientFromConfig validates configuration then creates new client from token
func (*Client) Close ¶
func (m *Client) Close()
// MkDir creates new folder in base dir
func (m *Client) MkDir(dir string) error { dirs, err := m.listDirs("", "", false) if err != nil { gou.WarnCtx(m.clientCtx, "error listing dirs: %v", err) return err } for _, d := range dirs { if d == dir { return nil } } return m.client.Mkdir(Concat(m.Folder, dir)) }
// Cd changes the base dir
func (m *Client) Cd(dir string) { m.Folder = Concat(m.Folder, dir) }
func (m *Client) FilesAfter(t time.Time) ([]os.FileInfo, error) { fi, err := m.fetchFiles("") if err != nil { return nil, err } sort.Sort(ByModTime(fi)) var files []os.FileInfo for _, f := range fi { if f.IsDir() { continue } if strings.Index(f.Name(), ".") != 0 && f.ModTime().After(t) { files = append(files, f) } } return files, nil }
Close closes underlying client connection
func (*Client) Exists ¶
// Rename renames a file
func (m *Client) Rename(oldname, newname string) error { if !m.Exists(oldname) { return os.ErrNotExist } o := Concat(m.bucket, oldname) n := Concat(m.bucket, newname) gou.InfoCtx(m.clientCtx, "renaming file %q to %q", o, n) return m.client.Rename(o, n) }
Exists checks to see if files exists
func (*Client) Folders ¶
// ListFiles lists files in a directory
func (m *Client) ListFiles(folder string, hidden bool) ([]string, error) { return m.filterFileNames(folder, false, true, hidden) }
Folders lists directories in a directory
func (*Client) List ¶
func (m *Client) List(ctx context.Context, q cloudstorage.Query) (*cloudstorage.ObjectsResponse, error)
List lists files in a directory
func (*Client) NewObject ¶
func (m *Client) NewObject(objectname string) (cloudstorage.Object, error)
NewObject create a new object with given name. Will not write to remote sftp until Close is called.
func (*Client) NewReader ¶
func (m *Client) NewReader(o string) (io.ReadCloser, error)
NewReader create file reader.
func (*Client) NewReaderWithContext ¶
NewReaderWithContext create new File reader with context.
func (*Client) NewWriterWithContext ¶
func (m *Client) NewWriterWithContext(ctx context.Context, name string, metadata map[string]string, opts ...cloudstorage.Opts) (io.WriteCloser, error)
NewWriterWithContext create writer with provided context and metadata.
func (*Client) Objects ¶
func (m *Client) Objects(ctx context.Context, q cloudstorage.Query) (cloudstorage.ObjectIterator, error)
// Open opens a file for read or writing
func (m *Client) Open(prefix, filename string) (io.ReadCloser, error) { fn := Concat(prefix, filename) if !m.Exists(fn) { return nil, os.ErrNotExist } get := Concat(m.bucket, fn) gou.InfoCtx(m.clientCtx, "getting file %q", get) return m.client.Open(get) }
Objects returns an iterator over the objects in the google bucket that match the Query q. If q is nil, no filtering is done.
type Uploader ¶
Store interface { Open(prefix, filename string) (io.ReadCloser, error) NewFile(filename string) (Uploader, error) Remove(filename string) error Rename(old, new string) error Exists(filename string) bool Files(folder string) ([]os.FileInfo, error) ListFiles(folder string, hidden bool) ([]string, error) ListDirs(folder string, hidden bool) ([]string, error) Cd(dir string) FilesAfter(t time.Time) ([]os.FileInfo, error) Close() }