The nextcloud API allows all kind of operations using curl. A few examples are given below. For the full documentation see https://docs.nextcloud.com/server/latest/developer_manual/client_apis/OCS/index.html. The examples assume that an API Token exists and has been stored in ~/.netrc.
Managing Public Shares
Getting a list of all your public shares:
# Getting a list of public shares - you might need to install jq and json python module. On PAL both are pre-installed curl -s -n -X GET -H 'Accept: application/json' -H "OCS-APIRequest: true" https://desycloud.desy.de/ocs/v1.php/apps/files_sharing/api/v1/shares -d path="blubber" # in detail # -s: curl operates silently # -X GET: a readonly query to the service # -H 'Accept: application/json': return output json formatted. default is xml # -H "OCS-APIRequest: true": you need to tell nextcloud that it's an API request # https://desycloud.desy.de/ocs/v1.php/apps/files_sharing/api/v1/share: the API endpoint # -d path="blubber": a PATH which is entirely ignored by the API! The API will always return ALL shares for the account in use!
Getting share for a particular PATH:
nextcloud API won't let you get the share for an individual path. However the information is relatively easy to extract.
# Getting a list of public shares and corresponding IDs and Tokens curl -s -n -X GET -H 'Accept: application/json' -H "OCS-APIRequest: true" https://desycloud.desy.de/ocs/v1.php/apps/files_sharing/api/v1/shares \ | jq '.ocs.data[] | "\(.path) \(.id) \(.token) \(.url)"' > "/MyShares/folder1 73807 AT2jdqZPEAnn6Zw https://desycloud.desy.de/index.php/s/AT2jdqZPEAnn6Zw" > "/MyShares/folder2 73808 APiwwYEXzzbHNj3 https://desycloud.desy.de/index.php/s/APiwwYEXzzbHNj3"
Creating shares for a particular PATH:
# Creating a share for "$targetdir". This uses xml and simple parsing. json and jq would be the better choice.... x=$(curl -n -X POST https://desycloud.desy.de/ocs/v1.php/apps/files_sharing/api/v1/shares -d path="$targetdir" -d shareType=3 -H "OCS-APIRequest: true") url=$(echo "$x" | grep url | tr '[<>]' ' ' | awk '{print $2}') echo created share $targetdir $url # It's possible to create any kind of share (not only public), w/o password or expiration date. See API documentation
Deleting a share link
Deletion needs the to use the id of the share. The recipe for getting a share for a path would work here as well:
# Getting a list of public shares and corresponding IDs and Tokens id=$(curl -s -n -X GET -H 'Accept: application/json' -H "OCS-APIRequest: true" https://desycloud.desy.de/ocs/v1.php/apps/files_sharing/api/v1/shares \ | jq '.ocs.data[] | "\(.path) \(.id) \(.token) \(.url)"' \ | grep $targetdir | awk '{print $2}') curl -s -n -X DELETE https://desycloud.desy.de/ocs/v1.php/apps/files_sharing/api/v1/shares/$id -H "OCS-APIRequest: true"
Managing Folder and Files
# checking if a folder or file exists: exists=$(curl -s -n -X PROPFIND -H "Depth: 1" https://desycloud.desy.de/remote.php/webdav/MyFolder | grep 200 | wc -l) if [[ $exists -eq 0 ]]; then echo MyFolder does not exist fi # create a folder curl -s -n -X MKCOL https://desycloud.desy.de/remote.php/webdav/MyFolder curl -s -n -X PROPFIND -H "Depth: 1" https://desycloud.desy.de/remote.php/webdav/MyFolder | grep 200 | wc -l > 1 # uploading a file curl -s -n -T "myfile.txt" https://desycloud.desy.de/remote.php/webdav/MyFolder/myfile.txt # list all files in folder curl -s -n -X PROPFIND -H "Depth: 1" https://desycloud.desy.de/remote.php/webdav/MyFolder | xmllint --pretty 2 - | grep -o '/remote.*<' |awk -F'<' '{print $1}' | grep -v '/$' > /remote.php/webdav/MyFolder/myfile2.txt > /remote.php/webdav/MyFolder/myfile.txt # getting all files from folder (I'm sure there are better ways to do that) for file in $(curl -s -n -X PROPFIND -H "Depth: 1" https://desycloud.desy.de/remote.php/webdav/MyFolder | xmllint --pretty 2 - | grep -o '/remote.*<' |awk -F'<' '{print $1}' | grep -v '/$') ; do curl -s -n https://desycloud.desy.de/$file -o $(echo $file | rev | cut -d/ -f1 | rev) done # deleting a remote file curl -n -X DELETE https://desycloud.desy.de/remote.php/webdav/MyFolder/myfile.txt # list files in trash curl -s -n -X PROPFIND -H "Depth: 1" https://desycloud.desy.de/remote.php/dav/trashbin/$USER/trash | xmllint --pretty 2 - # empty trash curl -n -X DELETE https://desycloud.desy.de/remote.php/dav/trashbin/$USER/trash