Copy large files to Nextcloud

I'm running my own Nextcloud instance in a docker container with the data directory mounted from a local folder on my disk. Unfortuantely it's not very satisfying to use the web UI or using a davfs2 mount of the disk to copy large files to the Nextcloud instance. This is because files must be passed through the full LAMP stack which involves a lot overhead and slows down any large file uploads.

An alternative to upload files via the browser or WebDAV is to copy them directly into the user data folder and then force Nextcloud to sync the content with its internal file cache and database entries.

Say our Nextcloud instance data lives in /opt/nextcloud/data and our source files live in /tmp/stage on the server. Further let the Nextcloud username be my-user and say the source data should be copied to ~/my-folder in Nextcloud.

Copying the data would then look like this:

# copy files
rsync -avP /tmp/stage/ /opt/nextcloud/data/my-user/files/my-folder/
# fix permissions
chmod www-data:www-data /opt/nextcloud/data/my-user/files/my-folder

It's necessary to fix the permissions since the default user in the official Nextcloud docker images is the www-data user. See also the Nextcloud documentation. Details:

$ docker-compose exec nextcloud id www-data
uid=33(www-data) gid=33(www-data) groups=33(www-data)

After the files have been copied, Nextcloud must be forced to re-index all files within this directory:

$ docker-compose exec -u www-data nextcloud ./occ files:scan --path="/my-user/files/my-folder"
Starting scan for user 1 out of 1 (my-user)
+---------+-------+--------------+
| Folders | Files | Elapsed time |
+---------+-------+--------------+
| 6       | 354   | 00:00:01     |
+---------+-------+--------------+

The internal database and caches have been updated and your files will pop up in the web UI afterwards.