“rdav” is a simpleWebDAV client to upload and download datafrom cloud services.
library(rdav)r<-wd_connect("https://example.com/remote.php/dav/files/myname/","myname")wd_download(r,"data/data.csv","localdata/data.csv")data<-read.table("localdata/data.csv")png("data_plot.png")plot(data)dev.off()wd_upload(r,"data_plot.png","data_plot.png")On servers that are based onNextcloud, you can also sharethe files
TheWebDAVprotocol provides a framework to create, change and move documents on aserver. Many cloud based storage systems provideWebDAV access,e.g. systems based onOwnCloud orNextcloud. This includes manycloud services offered by universities, organisations companies as wellas many self hosted clouds on NAS or Raspberry Pi.
To accessWebDAV one typically needs the URL to the WebDAVserver, an username and a password. There is also the possibility toshare data via public links with or without password protection.
Accounts on anOwnCloud orNextcloud based serverare typically addressed by WebDAV via the URL:https://example.com/remote.php/dav/files/username/.
When sharing data via a public link from those servers, the link istypically of the formhttps://example.com/s/yxcFKRWBJqYYzp4/
and will be used to access the files via a web browser. To access apublic share viaWebDAV, one has to use the URL:https://example.com/public.php/dav/files/username/
As username one has to use the share token, i. e. the cryptic number andletter string of the share link following/s/ - in theexample above the username would beyxcFKRWBJqYYzp4.
The package provides functions
wd_)ncl_)ocs_)To interact with theWebDAV server, you have to create aconnection viawd_connect by supplying the URL, usernameand password.
In an interactive R session, you may omit the password when callingwd_connect. Then R will ask you to type the password.
# no password given, R will ask.r<-wd_connect(url ="https://cloud.example.com/remote.php/dav/files/myname",username ="myname")When usingWebDAV in a script, you have to pass the passwordtowd_connect. It is not good practice, to write thepassword literally in your script
# Don"t do this! You would reveal your super secret password to others when# sharing your script.r<-wd_connect(url ="https://cloud.example.com/remote.php/dav/files/myname/",username ="myname",password ="12345")Better use your system”s credential store (i. e. the password will bestored encrypted in your user account). You may use the packagekeyringto access the system”s credential store.
You once have to set a key / password combination on your computer bycalling:
You will be asked for the password and then the credentials arestored on your system.
In your scripts you can then usekeyring::get_key("mycloud","myusername") to pass the storedpassword towd_connect:
To download a file, you have to give the path of the filename on theserver, as well as the path to the location where you want to store thefile on your computer.
Instead of giving the full path of the target file, you can use onlythe directory name. Then the file will be stored in that folder with thesame name as it has on the server.
You can download full directories. All the data within the directory,including subdirectories will be downloaded.
For uploading data from your computer, you have to usewd_upload in a similar way aswd_download:
wd_upload(r,"localdata/data.csv","data/data_new.csv")wd_upload(r,"localdata/data.csv","data")wd_upload(r,"localdata","data")Notice:
You can copy, move, delete files or directory on the server andcreate new directories.
By default existing files will be overwritten when using copy andmove operation, unless you specify the parameteroverwrite=FALSE. Notice: Some WebDAV servers may behavedifferently and won’t overwrite files. In these case you have to deletefiles first.
wd_copy(r,"actual.csv","backup.csv")wd_move(r,"backup.csv","backup-2024-02-27.csv")wd_move(r,"backup.csv","backup-2024-02-27.csv",overwrite =FALSE)wd_delete(r,"obsolete.csv")You can create new directories.
Notice that parent directories have to exist. If not, you have tocreate them first
You can get the list of files and subfolders as a character vector byusingwd_dir. Using the parameterfull_names = TRUE will give you the files with relativepaths.
To get more information about files, you can use the optionas_df = TRUE. You will then get a data.frame withadditional columns like size, last modification date andcontenttype.
BaseWebDAV entry point URLs for Nextcloud based servers areof the form<hostname>/<optional_subfolder>/remote.php/dav/files/<username>.Public share URLs of are of the form<hostname>/<optional_subfolder>/s/<token>where the token is used as username for authentication. TheWebDAV entry point for public shares is of the form<hostname>/<optional_subfolder>/public.php/dav/files/<username>.The package provides some functions to deal with these URLs:
ncl_baseurl(hostname, username) creates the base urlfrom host and user name.ncl_shareurl(hostname, username) creates the base urlfor public shares from host and username (token).ncl_shareurl_from_publicurl(url) creates the base urlfor public shares from the public share url.ncl_username_from_from_url extracts the username fromthe base urls.This simplifies the connection to Nextcloud based servers:
OnNextcloud based servers one can manage shares via theOCS API.
The package provides functions for
r<-wd_connect(url)# list all sharesocs_child_shares(r)# list the shares of a specific file or folderocs_shares(r,"folder_to_share")# create an e-mail sharesh<-ocs_create_share_mail(r,"folder_to_share","jackdoe@example.com")# modify the share by adding a password and notifying the userocs_modify_share(r, sh$id,password ="super_secret")ocs_send_mail(r, sh$id,password ="super_secret")# delete the shareocs_delete_share(r, sh$id)Here some thougths, how to better protect the data, if there is somerisk that your password get’s disclosed by using it within scripts.
You should avoid putting passwords in your code. See the example inthe sections above how to use system credential store.
Some cloud services offer you the possibility to create additionalapplication passwords. If you use an application password withrdav, and the password gets somehow disclosed, you can justdelete the application password and create a new one. You don’t have tochange then your account’s password.
Instead accessing the full account, you may create a subfolder andshare it via a public link, adding a password. You can give the sharefull read / write and edit rights, so you can manage the content of thefolder as you were logged in with your main account.
If someone gets access to your share, only the data in this foldercan then be manipulated.
If you want to share data with other people, create public shares.Don’t give them your account credentials.
You may create separated shares for reading and writing data ifappropriate.
your_upload and create a write-only shareto receive data from other usersyour_download and create a read-onlyshare to make data available to other usersThen you can check the uploaded files and move them to the downloadfolder, or process uploaded files and save results you want to share tothe download folder.
This will prevent that someone who gained access to the share todistribute unwanted or illegal content. Even if unwanted or illegalcontent might get uploaded, no one else can download it.
/sub/dir,you can set it via thedirectory parameter onwd_connect or usewd_setwd to change theworking directory.