A golang WebDAV client library.
gowebdav
library allows to perform following actions on the remote WebDAV server:
- create path
- get files list
- download file
- upload file
- get information about specified file/folder
- move file to another location
- copy file to another location
- delete file
First of all you should create Client
instance using NewClient()
function:
root := "https://webdav.mydomain.me"
user := "user"
password := "password"
c := gowebdav.NewClient(root, user, password)
After you can use this Client
to perform actions, described below.
NOTICE: we will not check errors in examples, to focus you on the gowebdav
library's code, but you should do it in your code!
err := c.Mkdir("folder", 0644)
In case you want to create several folders you can use c.MkdirAll()
:
err := c.MkdirAll("folder/subfolder/subfolder2", 0644)
files, _ := c.ReadDir("folder/subfolder")
for _, file := range files {
//notice that [file] has os.FileInfo type
fmt.Println(file.Name())
}
webdavFilePath := "folder/subfolder/file.txt"
localFilePath := "/tmp/webdav/file.txt"
bytes, _ := c.Read(webdavFilePath)
ioutil.WriteFile(localFilePath, bytes, 0644)
Also you can use c.ReadStream()
method:
webdavFilePath := "folder/subfolder/file.txt"
localFilePath := "/tmp/webdav/file.txt"
reader, _ := c.ReadStream(webdavFilePath)
file, _ := os.Create(localFilePath)
defer file.Close()
io.Copy(file, reader)
webdavFilePath := "folder/subfolder/file.txt"
localFilePath := "/tmp/webdav/file.txt"
bytes, _ := ioutil.ReadFile(localFilePath)
c.Write(webdavFilePath, bytes, 0644)
webdavFilePath := "folder/subfolder/file.txt"
localFilePath := "/tmp/webdav/file.txt"
file, _ := os.Open(localFilePath)
defer file.Close()
c.WriteStream(webdavFilePath, file, 0644)
webdavFilePath := "folder/subfolder/file.txt"
info := c.Stat(webdavFilePath)
//notice that [info] has os.FileInfo type
fmt.Println(info)
oldPath := "folder/subfolder/file.txt"
newPath := "folder/subfolder/moved.txt"
isOverwrite := true
c.Rename(oldPath, newPath, isOverwrite)
oldPath := "folder/subfolder/file.txt"
newPath := "folder/subfolder/file-copy.txt"
isOverwrite := true
c.Copy(oldPath, newPath, isOverwrite)
webdavFilePath := "folder/subfolder/file.txt"
c.Remove(webdavFilePath)
More details about WebDAV server you can read from following resources:
- RFC 4918 - HTTP Extensions for Web Distributed Authoring and Versioning (WebDAV)
- RFC 5689 - Extended MKCOL for Web Distributed Authoring and Versioning (WebDAV)
- RFC 2616 - HTTP/1.1 Status Code Definitions
- WebDav: Next Generation Collaborative Web Authoring By Lisa Dusseaul
NOTICE: RFC 2518 is obsoleted by RFC 4918 in June 2007
All contributing are welcome. If you have any suggestions or find some bug - please create an Issue to let us make this project better. We appreciate your help!
This library is distributed under the BSD 3-Clause license found in the LICENSE file.