一、环境准备
1.有访问对象存储权限的AKSK
2.服务器、对象存储
二、实施步骤
sudo apt update
sudo apt install s3fs
echo "AK:SK" >/home/ubuntu/.passwd-s3fs ---位置自定义
chmod 600 /home/ubuntu/.passwd-s3fs ---权限必须要有
mkdir /data
sudo s3fs yyy1 /data -o passwd_file=/home/ubuntu/.passwd-s3fs -o url=http://s3.cn-northwest-1.amazonaws.com.cn -o allow_other -o endpoint=cn-northwest-1
allow_other --其他用户也能看到 如只想让root看或者挂载用户看就不加
vi /usr/local/bin/mount-s3.sh
准备启动脚本
#!/bin/bash
# 定义变量
BUCKET_NAME="yyy1"
MOUNT_POINT="/data"
PASSWD_FILE="/home/ubuntu/.passwd-s3fs"
REGION="cn-northwest-1"
URL="http://s3.cn-northwest-1.amazonaws.com.cn"
# 检查挂载点是否存在,不存在则创建
if [ ! -d "$MOUNT_POINT" ]; then
mkdir -p "$MOUNT_POINT"
fi
# 尝试挂载S3存储桶
s3fs "$BUCKET_NAME" "$MOUNT_POINT" -o passwd_file="$PASSWD_FILE" -o url="$URL" -o allow_other -o endpoint="$REGION"
# 检查挂载是否成功
if mount | grep -q "s3fs on $MOUNT_POINT"; then
echo "S3 bucket $BUCKET_NAME mounted successfully on $MOUNT_POINT"
else
echo "Failed to mount S3 bucket $BUCKET_NAME on $MOUNT_POINT"
exit 1
fi
sudo chmod +x /usr/local/bin/mount-s3.sh
加权限
vi /etc/systemd/system/mount-s3.service
[Unit]
Description=Mount S3 bucket on /data
After=network.target
Requires=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/mount-s3.sh
RemainAfterExit=true
[Install]
WantedBy=multi-user.target
sudo systemctl enable mount-s3.service
sudo systemctl start mount-s3.service
三、测试验证
reboot测试一下 重启机器是否会成功挂载
测试验证成功
关闭的话:
sudo systemctl disable mount-s3.service
sudo systemctl stop mount-s3.service
在reboot 一下