<< Back to tutorials

Quick Storage API in Go with the SFTP protocol

Published {$created} by Carsten Blum


When you think about cloud storage APIs, you might picture complex SDKs or heavyweight integrations. But sometimes the simplest solution is the best one.With SFTP, every programming language that supports SSH can instantly act as a storage API — and Go makes this incredibly easy.


In this tutorial, we’ll show how to use Go to generate a small text file and upload it securely to ftpGrid using SFTP. This example demonstrates how quickly you can turn Go into a simple, reliable storage API.


Before starting this tutorial, you need an account with ftpGrid, which is a simple process outlined in our getting started with FTP/SFTP guide. If your are in doubt if managed FTP/SFTP hosting is for you, you can read our guide to ftpGrid's managed hosting. If you need help creating your SSH keys, follow our SSH key creation tutorial.


Basic knowledge with this programming language is assumed in this tutorial.



Requirements

Install the SFTP package:

#If new project without go.mod:
echo "module main" > go.mod

#Install packages:
go get github.com/pkg/sftp
go get golang.org/x/crypto/ssh



Example: Generate and Upload a File

Here’s a complete example in Go that generates a file called hello.txt and uploads it to your ftpGrid account using SFTP.

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"

	"golang.org/x/crypto/ssh"
	"github.com/pkg/sftp"
)

func main() {
	// Step 1: Create a local text file
	content := []byte("Hello from Go via ftpGrid SFTP!")
	localFile := "hello.txt"
	err := ioutil.WriteFile(localFile, content, 0644)
	if err != nil {
		log.Fatal("Error creating local file:", err)
	}

	// Step 2: Set up SSH client config
	config := &ssh.ClientConfig{
		User: "your_username",
		Auth: []ssh.AuthMethod{
			ssh.Password("your_password"), 
            //or use ssh.PublicKeys for key auth - see below
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
	}

	// Step 3: Connect to ftpGrid SFTP
	conn, err := ssh.Dial("tcp", "edge1.ftpgrid.com:22", config)
	if err != nil {
		log.Fatal("Failed to dial:", err)
	}
	defer conn.Close()

	client, err := sftp.NewClient(conn)
	if err != nil {
		log.Fatal("Error creating SFTP client:", err)
	}
	defer client.Close()

    // Step 4: Create remote directory if needed
    err = client.MkdirAll("/tutorial")
    if err != nil {
        log.Fatal("Error creating remote directory:", err)
    }

	// Step 5: Upload the file
	dstFile, err := client.Create("/tutorial/hello.txt")
	if err != nil {
		log.Fatal("Error creating remote file:", err)
	}
	defer dstFile.Close()

	srcFile, err := os.Open(localFile)
	if err != nil {
		log.Fatal("Error opening local file:", err)
	}
	defer srcFile.Close()

	bytes, err := dstFile.ReadFrom(srcFile)
	if err != nil {
		log.Fatal("Error uploading file:", err)
	}

	fmt.Printf("Uploaded %d bytes to ftpGrid!\n", bytes)
}


Using SSH key for authentication in Go lang

It is highly recommended to use SSH keys for authentication, instead of passwords.

In the above code example, replace the password with this code:

	// Load private key, below path is a typical Linux home path
	key, err := ioutil.ReadFile("/home/youruser/.ssh/id_ed25519")
	if err != nil {
		log.Fatal("Unable to read private key:", err)
	}

	// Parse the private key
	signer, err := ssh.ParsePrivateKey(key)
	if err != nil {
		log.Fatal("Unable to parse private key:", err)
	}

	// Use the signer for authentication
	config := &ssh.ClientConfig{
		User: "your_username",
		Auth: []ssh.AuthMethod{
			ssh.PublicKeys(signer),
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
	}



How the Code Works

  • Generate a file – The program writes hello.txt with a simple message.

  • Configure SSH – Using golang.org/x/crypto/ssh, we authenticate with ftpGrid.

  • Connect via SFTP – The pkg/sftp package establishes a secure SFTP session.

  • Upload the file – We create a remote file at /tutorial/hello.txt and copy the local file contents.

  • Verify – On success, the program prints how many bytes were transferred.



Running the Script

  • Replace your_username and your_password with your ftpGrid credentials. Or preferably use SSH keys instead of passwords!

  • Run: go run main.go

  • Log in to your ftpGrid account or connect with an SFTP client — you should see hello.txt in your /tutorial directory.



Why This Matters

With just a few lines of Go, you’ve created a working Storage API powered by SFTP.No proprietary SDKs, no third-party dependencies beyond pkg/sftp — just secure, standards-based file transfers.


This same pattern can be extended to handle:

  • Backups

  • Automated file ingestion

  • Data pipelines between services

At ftpGrid, we provide the infrastructure so you can focus on building.


Get started with a free ftpGrid account →



Next in the Quick Storage API series: We’ll explore the same example using PHP.



Signup now
© 2025 ftpGrid

ftpGrid ApS
Branebjerg 24
DK-5471
Gamby
Denmark

Looking for an all-in-one time tracking, timesheet, and invoicing solution - visit our sister company Nureti at https://nureti.com.