Golang SFTP Client: Download File, Upload File Example

Golang SFTP client examples


Golang SFTP ClientBuilding a Golang SFTP client is common when automating network systems. The github.com/pkg/sftp package is a fine tuned package that can get the job done. It works with SSH version 2 and is compatible with OpenSSH and other SSH spec servers.

Client setup may differ depending on your authentication requirements. In each example you’ll notice the HostKeyCallback to setup a FixedHostKey references the target host public key; this is used to prevent spoofing. If your host public key isn’t already archived in the your home directory OpenSSH known_hosts file, you can run ssh-keyscan to pull it off the target host.

Overview

Copy File Local to Remote

Here’s a simple example that copies a local file to a remote SFTP location.

Copy File Remote to Local

Here’s a simple example that copies a remote file from an SFTP local to a local location.

See also:
Golang DNS Lookup
Golang IP Address Manipulation
Golang Regular Expression Match
Golang SSH Client: Multiple Commands