Second Brain: Crafted, Curated, Connected, Compounded on 10月02日 20:53
使用s3fs将S3存储桶挂载为本地文件系统
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文档介绍了如何使用s3fs-fuse工具,在Linux、macOS和FreeBSD系统上将Amazon S3存储桶挂载为本地文件系统。s3fs利用FUSE(Filesystem in Userspace)技术,使得用户能够像操作本地文件一样访问和管理S3存储桶中的文件和目录,同时保留了S3对象的原生格式,方便与其他AWS工具协同使用。文章详细阐述了s3fs的安装、配置(包括凭证文件的创建和使用)、以及两种挂载方式:针对用户主目录的临时挂载和通过fstab配置的系统级持久化挂载,并提供了详细的命令示例和注意事项,还解释了`allow_other`选项的用途及安全性考量。

📦 **S3存储桶本地化访问**: s3fs-fuse允许用户将S3存储桶挂载为本地文件系统,从而可以在Linux、macOS和FreeBSD系统上像操作本地文件一样便捷地管理S3中的数据。这种方式保留了S3对象的原始格式,便于结合AWS CLI等其他工具进行操作。

🛠️ **安装与配置指南**: 文章提供了s3fs-fuse的安装命令,并详细说明了如何创建和使用凭证文件(`~/.passwd-s3fs`)来配置AWS访问密钥和秘密密钥。同时,也提及了若AWS凭证已配置为环境变量,s3fs可自动识别。

🚀 **挂载方式与持久化**: 提供了两种挂载S3存储桶的模式:一种是直接在用户主目录下创建挂载点进行临时挂载,无需sudo权限;另一种是通过编辑`/etc/fstab`文件实现系统启动时自动挂载,确保持久化访问。文章还详细列出了fstab配置项,包括挂载点、文件系统类型、挂载选项(如`endpoint`、`uid`、`gid`、`passwd_file`)以及如何测试挂载。

🔒 **安全与用户权限**: 重点解释了`allow_other`挂载选项的作用——允许非root用户访问FUSE文件系统,以及如何在`/etc/fuse.conf`中启用它。同时,也提供了在单用户系统下,为提高安全性而省略`allow_other`选项的建议,并说明如何通过`id -u`和`id -g`命令检查用户ID和组ID以进行精确配置。

    s3fs allows Linux, macOS, and FreeBSD to mount an S3 bucket via FUSE(Filesystem in Userspace).s3fs makes you operate files and directories in S3 bucket like a local file system.s3fs preserves the native object format for files, allowing use of other tools like AWS CLI.

GitHub

# Setup with S3

Install s3fs-fuse:

1
sudo pacman -S s3fs-fuse

Create credentials file (optional if using environment variables):

12
echo "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" > ~/.passwd-s3fschmod 600 ~/.passwd-s3fs

Mount the sspaeti bucket:

 1 2 3 4 5 6 7 8 9101112
# Create mount point in home directory (no sudo needed)mkdir -p ~/mnt/s3/sspaeti# Mount the bucket - specify correct endpoint for your regions3fs sspaeti ~/mnt/s3/sspaeti -o endpoint=eu-central-1# Alternative: using credentials file explicitlys3fs sspaeti ~/mnt/s3/sspaeti -o passwd_file=~/.passwd-s3fs,endpoint=eu-central-1# For system-wide mount (requires sudo):sudo mkdir -p /mnt/s3/sspaetisudo s3fs sspaeti /mnt/s3/sspaeti -o passwd_file=/home/$(whoami)/.passwd-s3fs,endpoint=eu-central-1,allow_other,uid=$(id -u),gid=$(id -g)

Unmount when done:

12345
# For home directory mountfusermount -u ~/mnt/s3/sspaeti# For system mountsudo umount /mnt/s3/sspaeti

Important Notes:

    Use home directory (~/) to avoid needing sudo - mounting to /mnt/ or other system directories requires root permissionsSpecify the correct endpoint - use endpoint=eu-central-1 for EU buckets, endpoint=us-west-2 for US West, etc.If your AWS credentials are already in environment variables, s3fs-fuse will pick them up automatically without needing the .passwd-s3fs fileWhen using sudo, the ~ path doesn’t expand correctly - use full paths like /home/$(whoami)/

AWS S3 setup see AWS S3.

# Create persistent Mounts

Above solution is gone after reboot, if you want persistence, we need to use fstab.

1234
# Create mount points if they don't existsudo mkdir -p /mnt/s3/sspaetisudo mkdir -p /mnt/synology/backupsudo mkdir -p /mnt/synology/photo

Above credentials still need to be created or are been used here - check if they exist:

12
ls ~/.passwd-s3fssudo cat /etc/cifs-credentials

Edit fstab config with sudo nvim /etc/fstab and add e.g. for my synology (Adding Network Drive - NAS Synology) and S3 drive the following configs:

123456
# S3 mount via s3fssspaeti /mnt/s3/sspaeti fuse.s3fs _netdev,allow_other,use_path_request_style,url=https://s3.eu-central-1.amazonaws.com,endpoint=eu-central-1,uid=1000,gid=1000,passwd_file=/home/sspaeti/.passwd-s3fs 0 0# Synology CIFS mounts//192.168.1.111/backup /mnt/synology/backup cifs _netdev,credentials=/etc/cifs-credentials,uid=1000,gid=1000,iocharset=utf8 0 0//192.168.1.111/photo /mnt/synology/photo cifs _netdev,credentials=/etc/cifs-credentials,uid=1000,gid=1000,iocharset=utf8 0 0

Enable allow_other for s3fs with:

Edit fuse config sudo nvim /etc/fuse.conf

12
# Uncomment this line (remove the # if present):user_allow_other

What does user_allow_other do?

The user_allow_other setting in /etc/fuse.conf allows non-root users to use the allow_other mount option with FUSE filesystems. By default, FUSE mounts are only accessible by the user who mounted them. allow_other makes the mount accessible to all users on the system

Single user system? You don’t need allow_other

If you’re the only user on your system, you can skip the allow_other option and the fuse.conf change for better security.

Check your user ID with:

12
id -u  # Should return 1000id -g  # Should return 1000

If your uid/gid match what’s in the fstab entry (1000), simply remove allow_other from the s3fs line:

1
sspaeti /mnt/s3/sspaeti fuse.s3fs _netdev,use_path_request_style,url=https://s3.eu-central-1.amazonaws.com,endpoint=eu-central-1,uid=1000,gid=1000,passwd_file=/home/sspaeti/.passwd-s3fs 0 0

And keep user_allow_other commented in /etc/fuse.conf. The mount will still work perfectly and be more secure.

# Test the mounts without rebooting

 1 2 3 4 5 6 7 8 91011
# Test mounting all entries in fstabsudo mount -a# Verify they're mountedmount | grep s3fsmount | grep cifs# Check if you can see filesls /mnt/s3/sspaetils /mnt/synology/backupls /mnt/synology/photo

Origin: stu
References: On MacOS see Mounting Amazon S3 as a File System, Adding Network Drive - NAS Synology
Created 2025-09-19

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

s3fs S3 FUSE 文件系统 挂载 AWS Linux macOS FreeBSD s3fs-fuse Cloud Storage Object Storage Mounting Filesystem Cloud Computing
相关文章