# Use S4 with Rclone

> Configure Rclone to connect to Bitiful S4 for uploads, sync, migration, verification, and mounting.

Rclone is a command-line sync tool for Windows, macOS, and Linux. It suits bulk uploads, backups, cross-cloud migration, and mounting.

## Prerequisites

- You have [created a bucket](https://docs.bitiful.com/en/bitiful-s4/handbook/buckets.md). Sub-users cannot create buckets, and `rclone mkdir` returns 403;
- You have [created a sub-user and AccessKey](https://docs.bitiful.com/en/bitiful-s4/handbook/access-keys.md) and [granted](https://docs.bitiful.com/en/bitiful-s4/handbook/permissions.md) it read, write, and list permissions on the bucket.

## Install

```bash macOS
brew install rclone
```

```bash Linux
curl https://rclone.org/install.sh | sudo bash
```

```powershell Windows
winget install Rclone.Rclone
```

## Configure

Run `rclone config file` to find the configuration file location, and write the following content:

```ini title="rclone.conf"
[s4]
type = s3
provider = Other
env_auth = false
access_key_id = Your AccessKey
secret_access_key = Your SecretKey
endpoint = https://s3.bitiful.net
region = cn-east-1
list_version = 2
no_check_bucket = true
```

| Setting                  | Notes                                                                                                                   |
| :----------------------- | ----------------------------------------------------------------------------------------------------------------------- |
| `endpoint`, `region`     | Fixed to the values above                                                                                               |
| `list_version = 2`       | Use ListObjectsV2                                                                                                       |
| `no_check_bucket = true` | Do not check or create the bucket before uploading. Sub-users have no permission to create buckets, so this is required |

Verify the configuration by listing the bucket contents:

```bash
rclone lsf s4:my-bucket
```

## Common commands

Upload and download:

```bash
# Upload a single file to a directory
rclone copy ./logo.png s4:my-bucket/images/

# Upload a directory, showing progress
rclone copy ./documents s4:my-bucket/documents -P

# Download
rclone copy s4:my-bucket/images/logo.png ./
```

Sync:

```bash
# Dry run first, without making any changes
rclone sync ./site s4:my-bucket/site --dry-run

# Make the destination identical to the source; extra files in the destination are deleted
rclone sync ./site s4:my-bucket/site -P
```

> **Warning**
>
> `sync` deletes files in the destination that are not in the source, and S4 has no recycle bin. When in doubt, use `copy`, or add `--dry-run` first.

View and verify:

```bash
# Count objects and total size
rclone size s4:my-bucket/documents

# Compare local files with the bucket
rclone check ./documents s4:my-bucket/documents
```

Generate a temporary download link:

```bash
rclone link s4:my-bucket/images/logo.png --expire 1h
```

## Many files and large files

```bash
# Many small files: increase concurrency
rclone copy ./data s4:my-bucket/data -P --transfers 32 --checkers 32

# Large files: increase the chunk size and per-file concurrency
rclone copy ./video.mp4 s4:my-bucket/videos/ -P \
  --s3-chunk-size 64M --s3-upload-concurrency 8
```

Excessive concurrency triggers the [QPS limit](https://docs.bitiful.com/en/bitiful-s4/limits.md). When you see 429, lower `--transfers`, or add `--tpslimit 100`.

## Migrate from other clouds

Add another remote to the configuration file, for example Alibaba Cloud OSS, then copy directly between the two clouds:

```bash
rclone copy oss:old-bucket s4:my-bucket -P --transfers 16
rclone check oss:old-bucket s4:my-bucket --size-only
```

The data passes through the machine running Rclone, and the source provider may charge for outbound internet traffic. To migrate without interrupting your service, combine it with [mirror back-to-origin](https://docs.bitiful.com/en/bitiful-s4/handbook/mirror.md).

## Mount as a local directory

Linux requires fuse3, macOS requires [macFUSE](https://github.com/macfuse/macfuse/releases), and Windows requires [WinFsp](https://github.com/winfsp/winfsp/releases).

```bash
mkdir -p ~/s4
rclone mount s4:my-bucket ~/s4 \
  --daemon \
  --vfs-cache-mode full \
  --vfs-cache-max-size 10G \
  --vfs-cache-max-age 24h \
  --vfs-read-chunk-size 16M \
  --vfs-read-chunk-size-limit 256M \
  --dir-cache-time 1m \
  --log-file ~/rclone.log
```

Unmount:

```bash
fusermount3 -u ~/s4   # Linux
umount ~/s4           # macOS
```

Mounting suits browsing and sequential reads and writes, but not programs such as databases that need random writes or file locks. Writes go to the local cache first and are uploaded in the background, so make sure uploads have finished before you shut down. For Bitiful's official mounting tool, see [Bitifs](https://docs.bitiful.com/en/best-practice/solutions/bitifs.md).

## FAQ

<details>
<summary>403 AccessDenied</summary>

Check the sub-user grants. If only a directory is granted, point the path to the granted directory, for example `s4:my-bucket/images`, and do not operate on the bucket root.

</details>

<details>
<summary>Uploading reports no permission to create the bucket</summary>

`no_check_bucket = true` is missing from the configuration.

</details>

<details>
<summary>Mismatched modification times cause repeated uploads</summary>

Use `--checksum` to compare by size and checksum, or `--size-only` to compare by size only.

</details>

## Further reading

1. [Official Rclone documentation](https://rclone.org/s3/)
2. [Ugediao: Mount Bitiful with rclone on a Debian server](https://ugediao.com/debian-rclone-bitiful.html)
