Movatterモバイル変換


[0]ホーム

URL:


rdav

Gunther Krauss

December 03, 2025

“rdav” is a simpleWebDAV client to upload and download datafrom cloud services.

1 A first example

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

ocs_create_share_mail(r,"data_plot.png","yourname@example.com",note ="Please check the plotted data.")

2 WebDAV, OwnCloud,NextCloud

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 form
https://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

3 Using the packagefunctions

3.1 Establishing theconnection

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:

keyring::key_set("mycloud","myusername")

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:

r<-wd_connect(url ="https://cloud.example.com/remote.php/dav/files/myname",username ="myname",password = keyring::get_key("mycloud","myusername"))

3.2 Downloading anduploading data

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.

wd_download(r,"data/data.csv","localdata/data_new.csv")

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.

wd_download(r,"data/data.csv","localdata")

You can download full directories. All the data within the directory,including subdirectories will be downloaded.

wd_download(r,"data","localdata")

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:

3.3 Copy, move, deletefiles on the server

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.

wd_mkdir(r,"data/soil/new")

Notice that parent directories have to exist. If not, you have tocreate them first

wd_mkdir(r,"data/soil")wd_mkdir(r,"data/soil/new")

3.4 Get informationsabout files and directories

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.

wd_dir(r)# get content of main folderwd_dir(r,"example")wd_dir(r,"example",full_names =TRUE)wd_dir(r,"example",as_df =TRUE)

4 Additional features forNextcloud based WebDAV servers

4.1 Handling base andshare URLs

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:

This simplifies the connection to Nextcloud based servers:

url<-ncl_baseurl("example.com","johndoe")r<-wd_connect(url)shareurl<-ncl_shareurl_from_publicurl("https://example.com/s/aXaejfaheDde")rs<-wd_connect(shareurl)

4.2 Managing shares

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)

4.3 Find users you wantto share with

You can search yourNextcloud based server for other usersor groups on that server as well as remote (e.g. federated shares ore-mail shares that are known to your server).

r<-wd_connect(url)ocs_find_users(r,"Doe, John")

5 Securityconsiderations

Here some thougths, how to better protect the data, if there is somerisk that your password get’s disclosed by using it within scripts.

5.1 Don’t put passwordsin your code

You should avoid putting passwords in your code. See the example inthe sections above how to use system credential store.

5.2 Use applicationpasswords instead of account passwords

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.

5.3 Use passwordprotected public shares instead of main account

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.

5.4 When sharing data,use different shares for writing and reading

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.

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

6 Some tips andtricks


[8]ページ先頭

©2009-2025 Movatter.jp