當前位置: 首頁>>代碼示例>>Golang>>正文


Golang sftp.Client類代碼示例

本文整理匯總了Golang中github.com/pkg/sftp.Client的典型用法代碼示例。如果您正苦於以下問題:Golang Client類的具體用法?Golang Client怎麽用?Golang Client使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Client類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: sftpMkdir

func sftpMkdir(path string, client *sftp.Client, fi os.FileInfo) error {
	log.Printf("[DEBUG] sftp: creating dir %s", path)

	if err := client.Mkdir(path); err != nil {
		// Do not consider it an error if the directory existed
		remoteFi, fiErr := client.Lstat(path)
		if fiErr != nil || !remoteFi.IsDir() {
			return err
		}
	}

	mode := fi.Mode().Perm()
	if err := client.Chmod(path, mode); err != nil {
		return err
	}
	return nil
}
開發者ID:threatstream,項目名稱:packer,代碼行數:17,代碼來源:communicator.go

示例2: mkdirAll

func mkdirAll(sftpClient *sftp.Client, dir string) error {
	if dir == "/" {
		// Must always exist
		return nil
	}

	stat, err := sftpClient.Lstat(dir)
	if err == nil {
		if !stat.IsDir() {
			return fmt.Errorf("not a directory: %q", dir)
		}
		return nil
	}

	parent := path.Dir(dir)
	err = mkdirAll(sftpClient, parent)
	if err != nil {
		return err
	}

	err = sftpClient.Mkdir(dir)
	if err != nil {
		return fmt.Errorf("error creating directory %q over sftp: %v", dir, err)
	}
	return nil
}
開發者ID:crohling,項目名稱:kops,代碼行數:26,代碼來源:sshfs.go

示例3: FileCreate

func FileCreate(s *sftp.Client, name string) (*File, error) {
	fd, err := s.Create(name)
	if err != nil {
		return &File{}, err
	}
	return &File{fd: fd}, nil
}
開發者ID:kubernetes,項目名稱:kubernetes,代碼行數:7,代碼來源:file.go

示例4: makeSftpDir

// make sftp directories
func makeSftpDir(client *sftp.Client, dirs []string) error {
	for i := len(dirs) - 1; i >= 0; i-- {
		if err := client.Mkdir(dirs[i]); err != nil {
			return err
		}
	}
	return nil
}
開發者ID:go-xiaohei,項目名稱:pugo,代碼行數:9,代碼來源:sftp.go

示例5: putDir

func putDir(p file, client *sftp.Client, basepath string, dryRun bool) {
	if dryRun {
		log.Info("!MKDIR", "path", p.relPath)
		return
	}

	log.Info("MKDIR", "path", p.relPath)
	err := client.Mkdir(basepath + "/" + p.relPath)
	if err != nil {
		log.Warn("MKDIR", "path", p.relPath, "err", err)
		return
	}
}
開發者ID:iwat,項目名稱:sftpsync,代碼行數:13,代碼來源:action.go

示例6: putFile

func putFile(p file, client *sftp.Client, basepath string, dryRun bool) {
	if dryRun {
		if p.offset > 0 {
			log.Info("!APPEND", "path", p.relPath, "size", p.size)
		} else {
			log.Info("!PUT", "path", p.relPath, "size", p.size)
		}
		return
	}

	var remote *sftp.File
	var err error
	if p.offset > 0 {
		log.Info("APPEND", "path", p.relPath, "size", p.size)
		remote, err = client.OpenFile(basepath+"/"+p.relPath, os.O_RDWR|os.O_APPEND)
	} else {
		log.Info("PUT", "path", p.relPath, "size", p.size)
		remote, err = client.Create(basepath + "/" + p.relPath)
	}
	if err != nil {
		log.Warn("PUT", "path", p.relPath, "size", p.size, "err", err)
		return
	}

	defer remote.Close()

	local, err := os.Open(p.path)
	if err != nil {
		log.Warn("PUT", "path", p.relPath, "size", p.size, "err", err)
		return
	}

	defer local.Close()

	if p.offset > 0 {
		_, err := local.Seek(p.offset, os.SEEK_SET)
		if err != nil {
			log.Warn("Seek error", "err", err)
			return
		}
	}

	_, err = io.Copy(remote, local)
	if err != nil {
		log.Warn("PUT", "path", p.relPath, "size", p.size, "err", err)
		return
	}
}
開發者ID:iwat,項目名稱:sftpsync,代碼行數:48,代碼來源:action.go

示例7: GlobHandler

func (conn *SshConfig) GlobHandler(rPath string, sftp *sftp.Client, env *Enviroment) error {
	rPaths := make([]string, 0)

	files, err := sftp.ReadDir(PathCutFile(rPath))
	if err != nil {
		return err
	}

	for _, file := range files {
		if glob.Glob(PathChomp(rPath), file.Name()) {

			if IsRegularFile(file) {
				rPaths = append(rPaths, JoinPath(rPath, file.Name()))
			}
		}
	}

	if len(rPaths) > 0 {
		if _, err := os.Stat(conn.Name); os.IsNotExist(err) {
			os.Mkdir(conn.Name, 0755)
		}

		for _, rp := range rPaths {
			conn.GetFile(rp, filepath.Join(conn.Name, PathChomp(rp)), sftp, env)
		}
	}

	return nil
}
開發者ID:kbdhero,項目名稱:cogs,代碼行數:29,代碼來源:ftp.go

示例8: ProcessDelete

func ProcessDelete(client *sftp.Client, dc <-chan file, dryRun bool, done chan<- bool) {
	for d := range dc {
		if dryRun {
			log.Info("!DEL", "path", d.relPath, "size", d.size)
			continue
		}

		log.Info("DEL", "path", d.relPath)
		err := client.Remove(d.path)
		if err != nil {
			log.Warn("DEL", "path", d.relPath, "err", err)
			continue
		}
	}

	done <- true
}
開發者ID:iwat,項目名稱:sftpsync,代碼行數:17,代碼來源:action.go

示例9: CopyFile

func (ssc *SSHConfig) CopyFile(local_path, remote_path string) (err tree_lib.TreeError) {
	var (
		sft       *sftp.Client
		f         *sftp.File
		file_data []byte
	)
	err.From = tree_lib.FROM_SSH_COPY_FILE
	sft, err.Err = sftp.NewClient(ssc.conn)
	if !err.IsNull() {
		return
	}
	defer sft.Close()

	file_data, err.Err = ioutil.ReadFile(local_path)
	if !err.IsNull() {
		return
	}

	f, err.Err = sft.Create(remote_path)
	if !err.IsNull() {
		return
	}

	_, err.Err = f.Write(file_data)
	f.Close()
	sft.Close()
	return
}
開發者ID:treescale,項目名稱:treescale,代碼行數:28,代碼來源:ssh.go

示例10: uploadFile

func uploadFile(name, dir string, sc *sftp.Client) error {
	f, err := os.Open(name)
	if err != nil {
		return err
	}
	defer f.Close()
	of, err := sc.Create(path.Join(dir, filepath.Base(name)))
	if err != nil {
		return err
	}
	defer of.Close()
	bufp := copyBufPool.Get().(*[]byte)
	defer copyBufPool.Put(bufp)
	if _, err := io.CopyBuffer(of, f, *bufp); err != nil {
		return err
	}
	return of.Close()
}
開發者ID:artyom,項目名稱:upload,代碼行數:18,代碼來源:upload.go

示例11: readSFTPTree

func readSFTPTree(sftpClient *sftp.Client, p *SSHPath, dest *[]Path) error {
	files, err := sftpClient.ReadDir(p.path)
	if err != nil {
		return err
	}
	for _, f := range files {
		child := NewSSHPath(p.client, p.server, path.Join(p.path, f.Name()), p.sudo)

		*dest = append(*dest, child)

		if f.IsDir() {
			err = readSFTPTree(sftpClient, child, dest)
			if err != nil {
				return err
			}
		}
	}
	return nil
}
開發者ID:crohling,項目名稱:kops,代碼行數:19,代碼來源:sshfs.go

示例12: GetFile

func (conn *SshConfig) GetFile(rPath, lPath string, sftp *sftp.Client, env *Enviroment) error {
	remoteFile, err := sftp.Open(rPath)
	if err != nil {
		return err
	}

	localFile, err := os.OpenFile(lPath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0755)
	if err != nil {
		return err
	}

	defer localFile.Close()

	if env.filter.on {
		// filter lines in the file that match the filter key
		fmt.Printf("downloading and applying filter to %s:%s\n", conn.Name, rPath)

		scanner := bufio.NewScanner(remoteFile)
		for scanner.Scan() {
			if LineMatch(scanner.Bytes(), env.filter.key) {

				if _, err = localFile.Write([]byte(scanner.Text() + "\n")); err != nil {
					return err
				}

			}
		}

		srcStat, err := localFile.Stat()
		if err != nil {
			return err
		}

		if (srcStat.Size()) == 0 {
			localFile.Write([]byte("No matches found."))
		}

		return nil
	}

	srcStat, err := remoteFile.Stat()
	if err != nil {
		return err
	}

	writer := io.Writer(localFile)

	io.Copy(writer, remoteFile)
	fmt.Printf("%s @filesize %dB <-- %s\n", lPath, srcStat.Size(), PathChomp(rPath))

	localFile.Sync()

	return nil
}
開發者ID:kbdhero,項目名稱:cogs,代碼行數:54,代碼來源:ftp.go

示例13: scp2Dest

// Scp one cpFile to destination.
// If destination is not exist, be regarded as a dir default, then make it.
// If destination is a dir, make file in it.
// If destination is a file, cover it.
func (t *SSHClient) scp2Dest(client *sftp.Client, cpFile *CpFile) (string, bool) {
	filename := cpFile.Filename
	dest := cpFile.Destination
	content := cpFile.Content

	// Dest dir not exist, make it.
	destType := "file"
	if fi, err := client.Lstat(dest); err != nil { // Dest not exist,make as a dir.
		if err := client.Mkdir(dest); err != nil {
			return fmt.Sprintf("mkdir %s, %s", dest, err.Error()), false
		}
	} else {
		if fi.IsDir() == true {
			destType = "dir"
		}
	}
	destF := dest
	if destType == "dir" { // dir, build filepath
		destF = fmt.Sprintf("%s/%s", dest, filename)
	}

	f, err := client.Create(destF)
	if err != nil {
		return fmt.Sprintf("create: %s, %s", destF, err.Error()), false
	}
	if _, err := f.Write(content); err != nil {
		return fmt.Sprintf("write: %s, %s", destF, err.Error()), false
	}
	return fmt.Sprintf("cp: %s, %s => %s", filename, t.host, dest), true
}
開發者ID:youngsn,項目名稱:go-ssh,代碼行數:34,代碼來源:ssh_client.go

示例14: sftpUploadFile

func sftpUploadFile(path string, input io.Reader, client *sftp.Client, fi *os.FileInfo) error {
	log.Printf("[DEBUG] sftp: uploading %s", path)

	f, err := client.Create(path)
	if err != nil {
		return err
	}
	defer f.Close()

	if _, err = io.Copy(f, input); err != nil {
		return err
	}

	if fi != nil && (*fi).Mode().IsRegular() {
		mode := (*fi).Mode().Perm()
		err = client.Chmod(path, mode)
		if err != nil {
			return err
		}
	}

	return nil
}
開發者ID:threatstream,項目名稱:packer,代碼行數:23,代碼來源:communicator.go

示例15: uploadDiffFiles

func (ft *SftpTask) uploadDiffFiles(client *sftp.Client, ctx *builder.Context) error {
	return ctx.Diff.Walk(func(name string, entry *builder.DiffEntry) error {
		rel, _ := filepath.Rel(ctx.DstDir, name)
		rel = filepath.ToSlash(rel)

		if entry.Behavior == builder.DIFF_REMOVE {
			log15.Debug("Deploy.Sftp.Delete", "file", rel)
			return client.Remove(path.Join(ft.opt.Directory, rel))
		}

		target := path.Join(ft.opt.Directory, rel)
		if entry.Behavior == builder.DIFF_KEEP {
			if fi, _ := client.Stat(target); fi != nil {
				// entry file should be older than uploaded file
				if entry.Time.Sub(fi.ModTime()).Seconds() < 0 {
					return nil
				}
			}
		}

		dirs := getDirs(path.Dir(rel))
		for i := len(dirs) - 1; i >= 0; i-- {
			client.Mkdir(path.Join(ft.opt.Directory, dirs[i]))
		}

		// upload file
		f, err := os.Open(name)
		if err != nil {
			return err
		}
		defer f.Close()

		f2, err := client.Create(target)
		if err != nil {
			return err
		}
		defer f2.Close()

		if _, err = io.Copy(f2, f); err != nil {
			return err
		}
		log15.Debug("Deploy.Sftp.Stor", "file", rel)
		return nil
	})
}
開發者ID:cnhans,項目名稱:pugo-static,代碼行數:45,代碼來源:sftp.go


注:本文中的github.com/pkg/sftp.Client類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。