To copy the files from a local folder to an R2 Cloudflare bucket run the rclone copy command, passing the source directory and the destination bucket. If you don't have the rclone command installed you can follow this guide to install and configure rclone.

How to copy from local to remote?

Let's look at an example that copies the local folder cars to an R2 Cloudflare bucket named cars-bucket.

rclone copy cars r2:cars-parking

The command doesn't generate output on success, output is generated only if an error occurs during execution. 

However, if you feel more comfortable seeing some output, you can run the command with the -v (meaning verbose) option or --progress.

$ rclone copy cars r2:car-parking -v
2023/08/09 16:50:01 INFO  : bmw.jpg: Copied (new)
2023/08/09 16:50:01 INFO  : skoda.jpg: Copied (new)
2023/08/09 16:50:01 INFO  : lambo.jpg: Copied (new)
2023/08/09 16:50:01 INFO  :
Transferred:            273 B / 273 B, 100%, 0 B/s, ETA -
Transferred:            3 / 3, 100%
Elapsed time:         0.7s

What is rclone?

Rclone is a powerful command-line tool for efficiently managing files on various cloud storage platforms. It serves as a robust alternative to the web storage interfaces provided by cloud vendors. With support for over 70 cloud storage products, including Amazon S3, Cloudflare R2, Azure Blob Storage and a lot more.

Rclone has powerful cloud equivalents to the popular unix commands rsync, cp, scp, mv, ls etc.

How to replace files with newer ones?

Probably by now, you start wondering what will happen if we rerun the command again.

$ rclone copy cars r2:car-parking -v
2023/08/09 16:51:03 INFO  : There was nothing to transfer
2023/08/09 16:51:03 INFO  :
Transferred:              0 B / 0 B, -, 0 B/s, ETA -
Checks:                 3 / 3, 100%
Elapsed time:         0.3s

During the copy process, the files are compared to the local ones, and only if there is a difference replacing will occur.

Let's replace the local bmw.jpg file with another one and see what will happen.

$ rclone copy cars r2:car-parking -v
2023/08/09 16:52:48 INFO  : bmw.jpg: Copied (replaced existing)
2023/08/09 16:52:48 INFO  :
Transferred:             49 B / 49 B, 100%, 0 B/s, ETA -
Checks:                 3 / 3, 100%
Transferred:            1 / 1, 100%
Elapsed time:         0.6s

As expected, the new file replaced the old one.

How to copy from remote to local?

By reverting the position of the remote and local directory arguments you can copy from remote to local.

$ rclone copy r2:car-parking cars_from_remote -v
2023/08/09 17:06:12 INFO  : lambo.jpg: Copied (new)
2023/08/09 17:06:12 INFO  : skoda.jpg: Copied (new)
2023/08/09 17:06:12 INFO  : bmw.jpg: Copied (new)
2023/08/09 17:06:12 INFO  :
Transferred:            277 B / 277 B, 100%, 0 B/s, ETA -
Transferred:            3 / 3, 100%
Elapsed time:         0.9s

How to sync a local folder to a remote one?

The rclone sync command makes the source and destination identical, modifying the destination only. This includes replacing and deleting files if necessary. Let's run the sync command to our car-parking bucket.

$ rclone sync cars r2:car-parking -v
2023/08/09 17:22:21 INFO  : There was nothing to transfer
2023/08/09 17:22:21 INFO  :
Transferred:              0 B / 0 B, -, 0 B/s, ETA -
Checks:                 3 / 3, 100%
Elapsed time:         0.3s

This does nothing since the local and remote are already in sync because we copied the files. Let's remove one file and see what happens.

$ rm cars/bmw.jpg
$ rclone sync cars r2:car-parking -v
2023/08/09 17:24:06 INFO  : bmw.jpg: Deleted
2023/08/09 17:24:06 INFO  : There was nothing to transfer
2023/08/09 17:24:06 INFO  :
Transferred:              0 B / 0 B, -, 0 B/s, ETA -
Checks:                 3 / 3, 100%
Deleted:                1 (files), 0 (dirs)
Elapsed time:         0.4s

I've removed the bmw.jpg locally and ran rclone sync and now the files is also removed from the bucket.

sync similar to copy works the other way around as well. You can sync remote to local. Let's remove our local cars folder and sync it from remote.

$ rm -rf cars
$ rclone sync r2:car-parking cars -v
2023/08/09 17:25:44 INFO  : lambo.jpg: Copied (new)
2023/08/09 17:25:44 INFO  : skoda.jpg: Copied (new)
2023/08/09 17:25:44 INFO  :
Transferred:            228 B / 228 B, 100%, 0 B/s, ETA -
Transferred:            2 / 2, 100%
Elapsed time:         0.4s

How to move files?

We've tested copy and sync but there is also move, which does exactly what you would expect, copies the file from local to remote (or vice versa) and removes them.

$ rclone move cars r2:car-parking -v

Useful options

Limit download and upload speed

--bwlimit 10M to limit the upload and download bandwidth to 10 MiB/s. You can control both download and upload speed separately.

--bwlimit 10M:100k limits download to 10MiB/s and upload to 100k/s.

Test run command

-n, --dry-run to test run your command. Use this command to see what will rclone do without actually doing it. 

$ rclone move r2:car-parking cars --dry-run
2023/08/09 17:35:14 NOTICE: skoda.jpg: Skipped delete as --dry-run is set (size 178)
2023/08/09 17:35:14 NOTICE: lambo.jpg: Skipped delete as --dry-run is set (size 50)
2023/08/09 17:35:14 NOTICE:
Transferred:              0 B / 0 B, -, 0 B/s, ETA -
Checks:                 4 / 4, 100%
Deleted:                2 (files), 0 (dirs)
Elapsed time:         0.2s

List of all commands and options supported by rclone.