「S3」Rcloneを使用してS3バケットをマウント

AWSのS3(Simple Storage Service)のバケットをLinuxサーバにマウントします。

1. 準備作業

(1) unzipのインストール
# dnf install unzip

(2) AWS CLIのインストール
S3を操作するためにAWS CLI(AWSコマンドラインインターフィース)をインストールします。
(a) ダウンロードおよびインストール
AWS コマンドラインインターフェイスをインストールします。
# curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
# unzip awscliv2.zip
# cd aws
# ./install
確認のためバージョン情報を表示します。
# aws --version
(b) 環境設定
コマンドプロンプトを起動して、以下のコマンドを実行します。
C:\>aws configure
ASW Access Key ID:
ASW Secret Access KeyDefault region name:ap-northeast-1
Default output format:json
を入力します。

(3) fuse3のインストール
fuse3をインストールします。
# dnf install fuse3

2. Rcloneの導入

(1) Rcloneのインストール
# curl https://rclone.org/install.sh | sudo bash
動作確認のためバージョンを表示します。
# rclone --version
rclone v1.69.1
- os/version: almalinux 9.4 (64 bit)
- os/kernel: 5.14.0-427.22.1.el9_4.x86_64 (x86_64)
- os/type: linux
- os/arch: amd64
- go/version: go1.24.0
- go/linking: static
- go/tags: none
 

(2) S3用Rcloneの設定
アクセスするS3の設定を行います。
# rclone config
対話メニューが表示されるので、問に答えて設定を実行していきます。
2024/06/26 07:16:52 NOTICE: Config file "/root/.config/rclone/rclone.conf" not found - using defaults
No remotes found, make a new one?
n) New remote
s) Set configuration password
q) Quit config
n/s/q>
「~/.config/rclone/rclone.conf」に保存されます。
設定例は以下の通りです。
[s3-mount]
type = s3
provider = AWS
access_key_id = (アクセスキー)
secret_access_key = (シークレットアクセスキー)
region = ap-northeast-1
endpoint = s3-ap-northeast-1.amazonaws.com
location_constraint = ap-northeast-1
acl = private
bucket_acl = private

3. S3のマウント

「s3-mount」は「rclone config」で設定したプロファイル名なので、適宜変更して下さい。
(1) マウントディレクトリの作成
ローカルサーバにマウント用のディレクトリを作成します。
ここでは「/mnt/s3」とします。
# mkdir /mnt/s3

(2) S3マウント確認
RcloneのコマンドでS3のバケットがマウントできるかの確認をします。
# rclone mount s3-mount:(バケット名) /mnt/s3 --daemon
もしくは
# rclone mount s3-mount:(バケット名) /mnt/s3 &

S3がマウントされているか確認します。
# df -hT
Filesystem                     Type         Size  Used Avail Use% Mounted on
devtmpfs                       devtmpfs     4.0M     0  4.0M   0% /dev
tmpfs                          tmpfs        481M     0  481M   0% /dev/shm
tmpfs                          tmpfs        193M  5.3M  187M   3% /run
/dev/vda2                      ext4          50G  1.5G   46G   4% /
tmpfs                          tmpfs         97M     0   97M   0% /run/user/1000
s3-mount:(バケット名)          fuse.rclone  1.0P     0  1.0P   0% /mnt/s3
「/mnt/s3」にS3バケットがマウントされていたらOKです。

一旦マウントを解除します。
# umount /mnt/s3

4. 自動S3マウント設定

サーバを再起動したときでも自動でS3がマウントされるように設定します。
(1) rcloneをマウントタイプに追加
mountコマンドで「rclone」がType指定できるように設定を行います。
# ln -s /usr/bin/rclone /sbin/mount.rclone
# systemctl daemon-reload
マウントが実行できるか確認します。
# mount s3-mount:(バケット名) /mnt/s3 -t rclone -o vfs_cache_mode=writes,config=/root/.config/rclone/rclone.conf
S3がマウントされているか確認します。
# df -hT
「/mnt/s3」にS3バケットがマウントされていたらOKです。
一旦マウントを解除します。
# umount /mnt/s3

(2) マウント設定ファイルの作成
# vi /etc/systemd/system/mnt-s3.mount
[Unit]
Description=S3 Mount
Wants=network-online.target
After=network-online.target
[Mount]
Type=rclone
What=s3-mount:(バケット名)
Where=/mnt/s3
Options=rw,allow_other,args2env,vfs-cache-mode=writes,config=/root/.config/rclone/rclone.conf,cache-dir=/var/cache/rclone
[Install]
WantedBy=multi-user.target

キャッシュ用ディレクトリを作成します。
# mkdir /var/chache/rclone

マウントを実行します。
# systemctl start mnt-s3.mount
# df -hT
「/mnt/s3」にS3バケットがマウントされていたらOKです。
マウントを解除します。
# systemctl stop mnt-s3.mount

マウントの自動起動を設定します。
# systemctl enable mnt-s3.mount
Created symlink /etc/systemd/system/multi-user.target.wants/mnt-s3.mount → /etc/systemd/system/mnt-s3.mount.

(3) トラブルシューティング
マウント設定ファイルを作成する場合ファイル名が「Where」のディレクトリ名と異なる名前を付けると以下のエラーが発生します。
systemd[1]: (マウントファイル名): Where= setting doesn't match unit name. Refusing.
例えば「/mnt/s3」の場合は「mnt-s3.mount」とします。
これを「s3-mount.mount」のように別のファイル名にするとエラーが発生します。
最終更新:2025年05月08日 07:58