go FTP file transfer

Introduction to FTP

FTPIt is File Transfer Protocolthe abbreviation of File Transfer Protocol. FTPis TCPa service based only on and is not supported UDP. It uses two ports, a data port and a command port (also called a control port). Generally speaking, these two ports are 21-命令端口and 20-数据端口.

FTPThere are two main modes: active mode ( PORT)/passive mode ( PASV), and two modes EPRT/ EPSV( Extended Port/Pasv) that extend FTP again for IPv6.

Currently, most FTPclients connect to FTPthe server in passive mode by default. The active/passive here refers to FTPthe server, whether the server opens the port and 被动waits for the client to connect, or the server 主动connects to the port opened by the client.

FileZilla tool

FTPThere are many commonly used tools, here is a brief introduction FileZilla. FileZilla Chinese website , we will use it to build a service locally FTPfor testing.

Server

windowsDownload address: windows FileZilla_Server_1.3.0_win64-setup.exe

After downloading, install it. Now create a user testwith the username and password 1234546and configure the mapping folder to prepare for subsequent testing.
Insert image description here

client

windowsGreen version download address: windows FileZilla_3.58.0_win32.zip

This client can be used to verify that our service is functioning correctly.
Insert image description here

go FTPclient

GitHubThere are many libraries available on the Internet. Here we mainly introduce FTPthe use of the client library: github.com/jlaffaye/ftp

Example

Basic example

import (
	"bytes"
	"commonTest/utils"
	"fmt"
	"github.com/jlaffaye/ftp"
)

func t1() {
    
    
	c, err := ftp.Dial("127.0.0.1:21")
	utils.CheckErr(err)
	defer c.Quit()

	// 可以重复连接,创建不同的实例
	//c1, err := ftp.Dial("127.0.0.1:21")
	//utils.CheckErr(err)
	//defer c1.Quit()

	err = c.Login("test", "123456")
	utils.CheckErr(err)

	// 新建文件
	//reader := bytes.NewReader([]byte("12356546"))
	//err = c.Stor("1.txt", reader)
	//utils.CheckErr(err)

	// 创建文件夹,可以嵌套 dir1/dir11
	err = c.MakeDir("dir1")
	utils.CheckErr(err)
	fmt.Println("创建文件夹成功")

	// 进入文件夹,可以嵌套 dir1/dir11
	err = c.ChangeDir("dir1")
	utils.CheckErr(err)
	fmt.Println("进入文件夹成功")

	// 新建文件
	reader := bytes.NewReader([]byte("12356546"))
	err = c.Stor("1.txt", reader)
	utils.CheckErr(err)
	fmt.Println("创建文件成功")

	dir, err := c.CurrentDir()
	utils.CheckErr(err)
	fmt.Println("current dir: ", dir)

	// 返回上一级
	err = c.ChangeDirToParent()
	utils.CheckErr(err)
	fmt.Println(c.CurrentDir())

	//entries, err := c.List("/")
	//utils.CheckErr(err)

}

Make sure the folder exists

func EnsureFtpDirExist(c *ftp.ServerConn, dir string) error {
    
    
	// 这里不能直接 MakeDir,有权限问题
	_, err := c.List(dir)
	if err != nil {
    
    
		if er := c.MakeDir(dir); er != nil {
    
    
			if er.Error() == "550 Directory with same name already exists." {
    
    
				return nil
			}
			return er
		}
	}
	return nil
}

Summarize

  • FTPMainly used for file transfer, it only supports TCPbut not UDPthe protocol.
  • Divided into active/passive mode, active/passive is relative to the server.
  • FileZilla is a relatively mature FTPproduct and is recommended to be used.
  • github.com/jlaffaye/ftp : The goimplemented FTPclient currently only supports passive mode connection to the server. Someone mentioned that issuethey hope to support active mode, and the author replied:

At present, basically all FTPservices support passive mode connections. We have not found any use cases for active mode and have no plans to add it for the time being.

In addition, I would like to mention that the above test codes are all in the files in the directory of my Gitee/GoTest warehouse . This project is mainly about some records and tests of daily learning. If you are interested, you can take a look.ftp

reference

Guess you like

Origin blog.csdn.net/DisMisPres/article/details/123740907
Recommended