質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

Q&A

解決済

2回答

1182閲覧

FTPサーバに画像をポストしたいです。

Gunjirk

総合スコア23

Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

0グッド

0クリップ

投稿2020/07/21 01:10

編集2020/07/29 07:48

ローカルの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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

1秒おきにr io.Readerの内容を繰り返し転送されようとしています。
1度目の転送はおそらく正しく処理できているのだと思いますが、
その時点でrはEOFまで読み込み済みになります。
io.Readerは繰り返し内容を読める機能はありません(一度読んだらそれっきり)
2度目の転送ではいきなりEOFが来ることでエラーになっているのではないでしょうか。

投稿2020/07/21 21:15

編集2020/07/21 21:25
nobonobo

総合スコア3367

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Gunjirk

2020/07/22 06:58

ありがとうございます。 おそらくそのようです。 間隔の秒数を変えてみたら、うまくいきました!
nobonobo

2020/07/22 10:09

可能であれば、うまくいった例も貼っていただけると助かります!
Gunjirk

2020/07/29 07:50

teratail開いておらず、気づきませんでした! すみません! 自己解決にて、うまくいった例を記載しておいたので、ご参考までに!
guest

0

for range time.Tick(1 * time.Second)

for range time.Tick(10 * time.Second)
に変更で動きました!

投稿2020/07/29 07:49

Gunjirk

総合スコア23

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問