func (c *Client) Upload(local, remote string) error {
    client, err := sftp.NewClient(c.SSHClient)
    if err != nil {
        return err
    }
    defer client.Close()
    localFile, err := os.Open(local)
    if err != nil {
        return err
    }
    defer localFile.Close()
    remoteFile, err := client.Create(remote)
    if err != nil {
        return err
    }
    _, err = io.Copy(remoteFile, localFile)
    return err
}

I call this function to upload a file to SFTP without any return error.

Use the command line to upload as follows: Command execution result

I am operating this function

var localFilePath = "./1234.zip"
var remoteDir = "upload/uuuuu.zip"
err = client.Upload(localFilePath, remoteDir)
if err != nil {
    panic(err)
}
fmt.Println("ok")

Execution result returns no error, only returns: Upload file completed

But landing SFTP, there is no upload file, ask everyone to point! Introduced: This service allows sftp connections only go version go1.10.2 linux/amd64

Simplify the code, the whole process is like this

func Consftp(username, host, privKeyPath, local, remote string) error {
    privKey, err := ioutil.ReadFile(privKeyPath)
    if err != nil {
        return err
    }
    signer, err := ssh.ParsePrivateKey(privKey)
    if err != nil {
        return err
    }

    authMethod := ssh.PublicKeys(signer)
    config := &ssh.ClientConfig{
        User:            username,
        Auth:            []ssh.AuthMethod{authMethod},
        HostKeyCallback: HostKeyCallback,
    }

    conn, err := ssh.Dial("tcp", host, config)
    if err != nil {
        return err
    }
    client, err := sftp.NewClient(conn)
    if err != nil {
        return err
    }
    defer client.Close()
    localFile, err := os.Open(local)
    if err != nil {
        return err
    }
    defer localFile.Close()
    remoteFile, err := client.Create(remote)
    println(err)
    if err != nil {
        return err
    }
    _, err = io.Copy(remoteFile, localFile)
    return err
}

code full

package until

import (
    "io"
    "io/ioutil"
    "os"

    "github.com/pkg/sftp"
    "golang.org/x/crypto/ssh"
)
func Consftp(username, host, privKeyPath, local, remote string) error {
    privKey, err := ioutil.ReadFile(privKeyPath)
    if err != nil {
        return err
    }
    signer, err := ssh.ParsePrivateKey(privKey)
    if err != nil {
        return err
    }

    authMethod := ssh.PublicKeys(signer)
    config := &ssh.ClientConfig{
        User:            username,
        Auth:            []ssh.AuthMethod{authMethod},
        HostKeyCallback: HostKeyCallback,
    }

    conn, err := ssh.Dial("tcp", host, config)
    if err != nil {
        return err
    }
    defer conn.Close()
    client, err := sftp.NewClient(conn)
    if err != nil {
        return err
    }
    defer client.Close()
    localFile, err := os.Open(local)
    if err != nil {
        return err
    }
    defer localFile.Close()
    remoteFile, err := client.Create(remote)
    println(err)
    if err != nil {
        return err
    }
    _, err = io.Copy(remoteFile, localFile)
    return err
}