0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Go: Synology の WebDAV にアクセスする

Last updated at Posted at 2021-03-04

こちらのライブラリーを使いました。
studio-b12/gowebdav

フォルダーの一覧

webdav_list.go
// ---------------------------------------------------------------
//
//	webdav_list.go
//
//					Mar/04/2021
// ---------------------------------------------------------------
package main

import (
	"fmt"
	"os"
	"github.com/studio-b12/gowebdav"
)

// ---------------------------------------------------------------
func main() {
	fmt.Fprintf (os.Stderr,"*** 開始 ***\n")

	server := "https://example.synology.me:5006"
	user := "scott"
	password := "secret"

	cc := gowebdav.NewClient(server, user, password)

	files, _ := cc.ReadDir("/homes/scott/tmp")

	for _, file := range files {
		fmt.Println(file.Name())
	}

	fmt.Fprintf (os.Stderr,"*** 終了 ***\n")
}

// ---------------------------------------------------------------

ファイルのアップロード

webdav_put.go
// ---------------------------------------------------------------
//
//	webdav_put.go
//
//					Mar/04/2021
// ---------------------------------------------------------------
package main

import (
	"fmt"
	"os"
	"github.com/studio-b12/gowebdav"
)

// ---------------------------------------------------------------
func main() {
	fmt.Fprintf (os.Stderr,"*** 開始 ***\n")
	server := "https://example.synology.me:5006"
	user := "scott"
	password := "secret"

	cc := gowebdav.NewClient(server, user, password)

	webdavFilePath := "/homes/scott/tmp/tmp02.txt"
	localFilePath := "./tmp02.txt"

	file, _ := os.Open(localFilePath)
	defer file.Close()

	cc.WriteStream(webdavFilePath, file, 0644)

	fmt.Fprintf (os.Stderr,"*** 終了 ***\n")
}

// ---------------------------------------------------------------

ファイルのダウンロード

webdav_get.go
// ---------------------------------------------------------------
//
//	webdav_get.go
//
//					Mar/04/2021
// ---------------------------------------------------------------
package main

import (
	"fmt"
	"os"
	"io"
	"github.com/studio-b12/gowebdav"
)

// ---------------------------------------------------------------
func main() {
	fmt.Fprintf (os.Stderr,"*** 開始 ***\n")
	server := "https://example.synology.me:5006"
	user := "scott"
	password := "secret"

	cc := gowebdav.NewClient(server, user, password)

	webdavFilePath := "/homes/scott/tmp/tmp01.txt"
	localFilePath := "./tmp01.txt"

	reader, _ := cc.ReadStream(webdavFilePath)

	file, _ := os.Create(localFilePath)
	defer file.Close()

	io.Copy(file, reader)

	fmt.Fprintf (os.Stderr,"*** 終了 ***\n")
}

// ---------------------------------------------------------------

実行方法についてはこちら
https://qiita.com/ekzemplaro/items/0781f7d242cdda52eb87

次のファイルが作成されます。

go.mod
go.sum
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?