从SFTP读取文件

huangapple go评论68阅读模式
英文:

Read file from sftp

问题

我在使用github.com/pkg/sftp来处理golang中的sftp服务器。

我想要从sftp服务器下载文件。

为此,我需要获取该文件的字节并将其复制到本地文件中,对吗?

首先,我使用OpenFile函数获取我的文件:

		file, err := sc.OpenFile("/backup/" + serverid + "/" + f.Name())
		if err != nil {
			fmt.Fprintf(os.Stderr, "无法打开文件:%v\n", err)

			return err
		}
		myfiles, err := file.HERE()
		os.WriteFile("/text.txt", myfiles, perm)
		return nil

但之后,我需要获取该文件的字节,但是我该如何做到呢?
在HERE的位置应该输入什么?

英文:

I use github.com/pkg/sftp for work with a sftp server in golang.

I want to download file from sftp server.

For that i need to get bytes of this file and copy it to a local file right?

First i get my file with a OpenFile function :

		file, err := sc.OpenFile("/backup/" + serverid + "/" + f.Name())
		if err != nil {
			fmt.Fprintf(os.Stderr, "Unable to open file: %v\n", err)

			return err
		}
		myfiles, err := file.HERE()
		os.WriteFile("/text.txt", myfiles, perm)
		return nil

But after i need to get bytes of this file but how i can do that?
What should i enter instead of HERE?

答案1

得分: 0

使用以下代码进行解决:

myfile, err := io.ReadAll(file)

这段代码用于读取文件的全部内容。

英文:

Resolved with:

myfile, err := io.ReadAll(file)

huangapple
  • 本文由 发表于 2022年1月13日 21:56:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/70697794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定