-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftp.go
51 lines (40 loc) · 822 Bytes
/
ftp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
"io"
"os"
"time"
"github.com/jlaffaye/ftp"
)
type fileData struct {
fileName string
number int
}
// retrieveFile download the file from the ftp server
// and copy the file locally in a tmp/ folder
func retrieveFile(f fileData) (string, error) {
c, err := ftp.Dial("ftp.ngs.noaa.gov:21", ftp.DialWithTimeout(5*time.Second))
if err != nil {
return "", err
}
err = c.Login("anonymous", "anonymous")
if err != nil {
return "", err
}
res, err := c.Retr(f.fileName)
if err != nil {
return "", err
}
defer res.Close()
fileName := fmt.Sprintf(pathTmp+"/file%d.gz", f.number)
outFile, err := os.Create(fileName)
if err != nil {
return "", err
}
defer outFile.Close()
_, err = io.Copy(outFile, res)
if err != nil {
return "", err
}
return fileName, nil
}