Categorygithub.com/rickb777/gowebdav
modulepackage
0.1.0
Repository: https://github.com/rickb777/gowebdav.git
Documentation: pkg.go.dev

# README

GoWebDAV

Build Status GoDoc Go Report Card

A golang WebDAV client library with a command line tool included.

Main features

This API allows the following actions on remote WebDAV servers:

Get started

go get github.com/rickb777/gowebdav

Usage

Start by creating a Client instance using the NewClient() function:

root := "https://webdav.mydomain.me"
user := "user"
password := "password"

c := gowebdav.NewClient(root)

Then 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.

Create folders on a WebDAV server

err := c.Mkdir("folder", 0644)

If you want to create several nested folders, you can use c.MkdirAll():

err := c.MkdirAll("folder/subfolder/subfolder2", 0644)

List files

files, _ := c.ReadDir("folder/subfolder")
for _, file := range files {
    //notice that [file] has os.FileInfo type
    fmt.Println(file.Name())
}

Download file to byte array

webdavFilePath := "folder/subfolder/file.txt"
localFilePath := "/tmp/webdav/file.txt"

bytes, _ := c.ReadFile(webdavFilePath)
ioutil.WriteFile(localFilePath, bytes, 0644)

Download file via reader

Alternatively, use the 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)

Upload file from byte array

webdavFilePath := "folder/subfolder/file.txt"
localFilePath := "/tmp/webdav/file.txt"

bytes, _ := ioutil.ReadFile(localFilePath)

c.WriteFile(webdavFilePath, bytes, 0644)

Upload file via writer

webdavFilePath := "folder/subfolder/file.txt"
localFilePath := "/tmp/webdav/file.txt"

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

c.WriteStream(webdavFilePath, file, 0644)

Get information about specified file/folder

webdavFilePath := "folder/subfolder/file.txt"

info := c.Stat(webdavFilePath)
//notice that [info] has os.FileInfo type
fmt.Println(info)

Move file to another location

oldPath := "folder/subfolder/file.txt"
newPath := "folder/subfolder/moved.txt"
isOverwrite := true

c.Rename(oldPath, newPath)

or use RenameWithoutOverwriting(oldpath, newpath string).

Copy file to another location

oldPath := "folder/subfolder/file.txt"
newPath := "folder/subfolder/file-copy.txt"
isOverwrite := true

c.Copy(oldPath, newPath)

or use CopyWithoutOverwriting(oldpath, newpath string) error.

Delete file

webdavFilePath := "folder/subfolder/file.txt"

c.Remove(webdavFilePath)

Links

You can read more details about WebDAV from the following resources:

Contributing

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!

License & Acknowledgement

This library is distributed under the BSD 3-Clause license found in the LICENSE file.

My thanks to

  • Studio-b12: did much of the original work - see gowebdav.
  • Andrew Koltyakov: wrote Gosip, on which the SAML authentication used here is based.

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

AddHeader lets us set arbitrary headers for a given client.
NewClient creates a new Client.
SetAuthentication sets the authentication credentials and method.
SetHttpClient changes the http.Client.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

Client is compatible with Afero.Fs.
No description provided by the author

# Type aliases

No description provided by the author