How to upload/download files from nextcloud by cURL?

nextcloud

Reading Time: 3 minutes

I think to upload my file to nextcloud by my application.
Is it possible to upload, download, or creating folder?

This article is helpful for you.

How to do them

Uploading file

You can do by below command.
– I show with curl command and you can do any other commands if you align HTTP header.

# <user> user name in nextcloud
# <pass> password <user>
# <nextcloud root> root folder of nextcloud ex) https://abc.com/nextcloud
# <path to upload> Path of nextcloud which you want to upload file
# <file path to send> Path of your system which file is
curl -X PUT -u <user>:<pass> https://<nextcloud root>/remote.php/dav/files/<user>/<path to upload> -T <file path to send>



This is example of uploading image.jpg by user: yasu.
– Password is <pass> to hide real string.

curl -X PUT -u yasu:<pass> https://yasufumi-yokoyama.gq/nextcloud/remote.php/dav/files/yasu/image.jpg -T ./image.jpg

Downloading file

This is the case of download.

# <user> user name in nextcloud
# <pass> password <user>
# <nextcloud root> root folder of nextcloud ex) https://abc.com/nextcloud
# <path of file to download> Path of nextcloud which you want to download
# <path to save> Path of your system which you want to download to
curl -X GET -u <user>:<pass> https://<nextcloud root>/remote.php/dav/files/<user>/<path of file to download> --output <path to save>



This is example of downloading image.jpg by user: yasu.

curl -X GET -u yasu:<pass> https://yasufumi-yokoyama.gq/nextcloud/remote.php/dav/files/yasu/Nextcloud.png --output Nextcloud.png

Creating folder

It can be done by MKCOL method.

# <user> user name in nextcloud
# <pass> password <user>
# <nextcloud root> root folder of nextcloud ex) https://abc.com/nextcloud
# <folder path to create> Path of nextcloud where you want to create folder as
curl -X MKCOL -u <user>:<pass> https://<nextcloud root>/remote.php/dav/files/<user>/<folder path to create>



This is example of creating folder test by user: yasu.

curl -X MKCOL -u yasu:<pass> https://yasufumi-yokoyama.gq/nextcloud/remote.php/dav/files/yasu/test

Deleting folder

It can be done by DELETE method.

# <user> user name in nextcloud
# <pass> password <user>
# <nextcloud root> root folder of nextcloud ex) https://abc.com/nextcloud
# <folder path to delete> Path of nextcloud which you want to delete folder
curl -X DELETE -u <user>:<pass> https://<nextcloud root>/remote.php/dav/files/<user>/<folder path to delete>



This is example of deleting folder test by user: yasu.

curl -X DELETE -u yasu:<pass> https://yasufumi-yokoyama.gq/nextcloud/remote.php/dav/files/yasu/test

Other operation

You can refer official document for other operation.

Tips in case of using 2 factor authentication

You can refer official manual(PDF) for adopting the environment with 2 factor authentication.

You can find Using client applications with two-factor authentication how to do it.

Once you have enabled 2FA, your clients will no longer be able to connect with just your password unless they also have support for two-factor authentication. To solve this, you should generate device specific passwords for them. See Manage connected browsers and devices for more information on how to do this.

https://docs.nextcloud.com/server/stable/Nextcloud_User_Manual.pdf



This means,

If 2 factor authentication is active you cannot login by password.
You need to create special password for each device.
Please see Manage connected browsers and devices.


Let’s do it.

First login and go to Settings.



Go to Security and input app name to App name edit box and click Create your app password.



Please take note of your password.
Be careful to copy here because you cannot confirm password if you move to other page.



Then you can use this password to authenticate in you application or using API.

Conclusion

How was it?

I am planning to use this feature to upload photoes taken by automatic watering system!

For automatic watering system you can find here!

Comments

Copied title and URL