ローカルのC:/screenshot.jpgをFTPサーバのscreenshot/172にポストしたいです。
io.Readerの扱い方を調べましたが、いまいちわからず。。。
宜しくお願いします。
エラー内容
2020/07/21 10:07:05 [FATAL] unexpected EOF
参考サイト
https://github.com/jlaffaye/ftp
GO
1package main 2 3import ( 4 "context" 5 "crypto/tls" 6 "io" 7 "log" 8 "net" 9 "os" 10 "time" 11 12 "github.com/jlaffaye/ftp" 13 "github.com/spiegel-im-spiegel/logf" 14) 15 16// DialOption dialOptions 17type DialOption struct { 18 setup func(do *dialOptions) 19} 20 21// dialOptions contains all the options set by DialOption.setup 22type dialOptions struct { 23 context context.Context 24 dialer net.Dialer 25 tlsConfig *tls.Config 26 explicitTLS bool 27 conn net.Conn 28 disableEPSV bool 29 location *time.Location 30 debugOutput io.Writer 31 dialFunc func(network, address string) (net.Conn, error) 32} 33 34// DialWithExplicitTLS returns a DialOption that configures the ServerConn to be upgraded to TLS 35// See DialWithTLS for general TLS documentation 36func DialWithExplicitTLS(tlsConfig *tls.Config) DialOption { 37 return DialOption{func(do *dialOptions) { 38 do.explicitTLS = true 39 do.tlsConfig = tlsConfig 40 }} 41} 42 43func main() { 44 45 ent, err := os.Stat("C:/screenshot.jpg") 46 if err != nil { 47 logf.Fatal(err) 48 } 49 50 if ent == nil { 51 } else { 52 53 //Upload a file 54 var r io.Reader 55 if r, err = os.Open("C:/screenshot.jpg"); err != nil { 56 logf.Fatal(err) 57 } 58 59 c, err := ftp.Dial("ftp.example.org:21", ftp.DialWithExplicitTLS(&tls.Config{ 60 // Set InsecureSkipVerify to skip the default validation we are 61 // replacing. This will not disable VerifyPeerCertificate. 62 InsecureSkipVerify: true, 63 64 // While packages like net/http will implicitly set ServerName, the 65 // VerifyPeerCertificate callback can't access that value, so it has to be set 66 // explicitly here or in VerifyPeerCertificate on the client side. If in 67 // an http.Transport DialTLS callback, this can be obtained by passing 68 // the addr argument to net.SplitHostPort. 69 ServerName: "ftp.example.org", 70 71 // On the server side, set ClientAuth to require client certificates (or 72 // VerifyPeerCertificate will run anyway and panic accessing certs[0]) 73 // but not verify them with the default verifier. 74 // ClientAuth: tls.RequireAnyClientCert, 75 })) 76 if err != nil { 77 log.Fatal(err) 78 } 79 80 err = c.Login("user", "password") 81 if err != nil { 82 log.Fatal(err) 83 } 84 85 c.ChangeDir("screenshot/172") 86 87 for range time.Tick(1 * time.Second) { 88 89 ch := make(chan string) 90 91 go ftps(c, r, ch) 92 93 cf := <-ch 94 95 if cf != "0" { 96 logf.Printf("スクリーンショット転送完了") 97 } 98 } 99 100 if err := c.Quit(); err != nil { 101 log.Fatal(err) 102 } 103 } 104} 105 106func ftps(c *ftp.ServerConn, r io.Reader, ch chan string) { 107 108 if err := c.Stor("screenshot.jpg", r); err != nil { 109 logf.Fatal(err) 110 ch <- "0" 111 } 112 ch <- "1" 113 114} 115 116
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/07/22 06:58
2020/07/22 10:09
2020/07/29 07:50