Upload a file - type io.Reader is not an expression

I have some problem to upload an existing file to a ftp using jlaffaye/ftp. There is no “upload” command. The closest I have found is StorFrom()

But the syntax is a hidden secret for me. I cannot find any example how to use this.

package main

import (
	"fmt"
	"github.com/jlaffaye/ftp"
	"io"
	"os"
	"time"
)

func send2ftp(filename string) {
	c, err := ftp.Dial("ftp.test.com:21", ftp.DialWithTimeout(5*time.Second))
	if err != nil {
		fmt.Println(err)
	}

	err = c.Login("test", "test")
	if err != nil {
		fmt.Println(err)
	}

	if _, err := os.Stat(filename); err != nil {
		if os.IsNotExist(err) {
			fmt.Println("does not exist")
		}
	} else {
		fmt.Println("exists")
	}

	err = c.StorFrom(filename, io.Reader, 0)
	if err != nil {
		panic(err)
	}
}

https://play.golang.com/p/xPykHDRei-U

This attempt to find a syntax gives this error:

type io.Reader is not an expression

The question is simple: How do I upload an existing file to ftp? Or how do I create a file at ftp using a []string value.

(*ServerConn).StorFrom expects to be passed something that implements io.Reader. If you want to upload a file from your computer, you can use os.Open to open the file and then pass the file to StorFrom. Don’t forget to close the file when you’re done!

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.