You need to enable JavaScript to run this app.
导航

在 Linux 系统挂载 TOS 存储桶

最近更新时间2024.04.16 11:22:51

首次发布时间2023.06.02 10:58:20

TOS 支持将存储桶作为磁盘挂载到 Linux 系统。本文以 CentOS stream8 的 ECS 实例为例,介绍使用 Goofys 和 S3FS 工具在 ECS 实例挂载存储桶的相关操作和常见问题。

前提条件

  • 确保账号拥有存储桶的读写权限,并获取该账号的 Access Key ID 和 Secret Access Key。更多信息,请参见 Access Key(密钥)管理
  • 已获取存储桶的 S3 Endpoint。更多信息,请参见地域和访问域名
  • 使用 root 用户或者具有 sudo 权限的用户。

注意事项

S3FS 无法提供与本地文件系统相同的性能。 由于网络延迟,元数据操作(例如列出目录)性能较差。随机或者追加写文件会导致整个文件的下载以及重新上传。当多个客户端挂载同一个 TOS 存储桶时,您需要自行协调各个客户端的行为。更多 S3FS 工具的注意事项,请参见S3FS
使用 Goofys 或 S3FS 工具挂载存储桶适用于小量测试等场景,不建议生产环境使用此方式。生产环境建议使用 S3 、TOS API 或 TOS SDK。

使用 Goofys 工具挂载 TOS 存储桶

步骤一:安装 Goofys 工具

  1. 登录 ECS 实例。具体操作,请参见登录 ECS 实例

  2. 执行以下命令,安装 Goofys 工具。

    wget https://github.com/kahing/goofys/releases/download/v0.24.0/goofys
    
  3. 执行以下命令,将 Goofys 工具移动到 /usr/local/bin/ 目录下。

    mv goofys /usr/local/bin/
    
  4. 执行以下命令,授予 Goofys 工具执行权限。

    chmod +x /usr/local/bin/goofys
    
  5. 执行以下命令,验证 Goofys 工具是否安装成功。

    goofys -version
    

    返回类似如下内容,说明 Goofys 工具安装成功。

    goofys version 0.24.0-45b8d78375af1b24604439d2e60c567654bcdf88
    

步骤二:挂载存储桶

  1. 执行以下命令,创建 .aws 目录。

    mkdir /root/.aws
    
  2. 执行以下命令,创建 credentials 文件。

    touch /root/.aws/credentials
    
  3. 执行以下命令,打开配置文件。

    vi /root/.aws/credentials
    
  4. 执行以下命令,在配置文件中按照以下格式添加 Access Key ID 和 Secret Access Key,然后保存并退出。

    [default]                      
    aws_access_key_id = AK         
    aws_secret_access_key = SK     
    s3 = 
       addressing_style = virtual
    

    参数说明如下:

    • [default]:profile 的名称,本文设置为default
    • aws_access_key_id = AK:设置 Access Key ID,您需要将 AK 替换为您账号的 Access Key ID。
    • aws_secret_access_key = SK:设置 Secret Access Key,您需要将 SK 替换为您账号的 Secret Access Key。
    • addressing_style = virtual:对象存储不支持 Path Style 形式访问,应使用 Virtual Style 形式(即虚拟主机访问方式)访问。
  5. 执行以下命令,创建挂载目录,本文为 goofys_dir

    mkdir /goofys_dir
    
  6. 执行以下命令,挂载存储桶。

    goofys --subdomain --dir-mode=0777 --file-mode=0666 --profile default -o allow_other --endpoint=https://<S3 Endpoint> <存储桶名称> <挂载目录> 
    

    注意

    • 命令中的 S3 Endpoint 指 TOS 对外服务的访问域名,不是存储桶的 S3 Endpoint。关于各个地域对应的 TOS 访问域名详细介绍,请参见地域和访问域名(Endpoint)
    • -o allow_other表示允许非 root 账号访问挂载的存储桶。

    示例:

    goofys --subdomain --dir-mode=0777 --file-mode=0666 --profile default -o allow_other --endpoint=https://tos-s3-cn-beijing.volces.com goofys-test /goofys_dir 
    
  7. 执行以下命令,验证挂载存储桶是否成功。

    df -hT|grep goofys
    

    返回类似如下内容,说明挂载存储桶成功。

    goofys-test    fuse      1.0P     0  1.0P   0% /goofys_dir
    

使用 S3FS 挂载 TOS 存储桶

步骤一:安装 S3FS 工具

说明

如果您使用的是 Debian 镜像,您需要执行 sudo apt-get update 更新镜像,然后执行 sudo apt install s3fs 安装 S3FS 工具。

  1. 登录 ECS 实例。具体操作,请参见登录 ECS 实例

  2. 执行以下命令,安装扩展软件源。

    yum install epel-release
    

    返回类似如下内容:

    Total download size: 24 k
    Installed size: 35 k
    Is this ok [y/N]:
    
  3. 执行以下命令,确认安装扩展软件源。

    y
    
  4. 执行以下命令,安装 S3FS。

    yum install s3fs-fuse
    

    返回类似如下内容:

    Total download size: 416 k
    Installed size: 927 k
    Is this ok [y/N]:
    
  5. 执行以下命令,确认安装 S3FS。

    y
    

    返回类似如下内容:

    Fingerprint: 94E2 79EB 8D8F 25B2 1810 ADF1 21EA 45AB 2F86 D6A1
    From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
    Is this ok [y/N]:
    
  6. 执行以下命令,确认安装 Linux 的扩展软件包。

    y
    

步骤二:挂载存储桶

  1. 执行以下命令,将 Access Key ID 和 Secret Access Key 信息存放到 .passwd-s3fs 文件。

    echo AK:SK > /root/.passwd-s3fs    //将 AK 和 SK 替换为您账号的 Access Key ID 和 Secret Access Key。 
    
  2. 执行以下命令,授予 .passwd-s3fs 文件 600 权限。

    chmod 600 /root/.passwd-s3fs
    
  3. 执行以下命令,创建挂载目录。

    mkdir /root/s3fs_dir
    
  4. 执行以下命令,将存储桶挂载到 ECS 实例。

    s3fs <存储桶的名称> <挂载目录> -o passwd_file=${HOME}/.passwd-s3fs -o url=http://<S3 Endpoint> -d -o f2
    

    注意

    命令中的 S3 Endpoint 指 TOS 对外服务的访问域名,不是存储桶的 S3 Endpoint。关于各个地域对应的 TOS 访问域名详细介绍,请参见地域和访问域名(Endpoint)

    示例:

    s3fs test /root/s3fs_dir   -o passwd_file=$HOME/.passwd-s3fs -o url=http://tos-s3-cn-beijing.volces.com -d -o f2
    
  5. 执行以下命令,验证挂载存储桶是否成功。

    df -hT
    

    返回结果中包含以下内容,说明挂载存储桶成功。

    Filesystem     Type       Size    Used  Avail   Use%    Mounted on
    s3fs           fuse.s3fs   16E     0    16E     0%      /root/s3fs_dir
    

相关命令

命令

描述

取消挂载存储桶

sudo umount <挂载目录>

注意

取消挂载存储桶之前,请确保已完成上传文件等操作。

查看日志

  • 查看 S3FS 工具日志:vi /var/log/messages
  • 查看 Goofys 工具日志:vi /var/log/syslog

常见问题

Q:挂载报错,如何查看错误信息?
A:您可以通过以下命令查看报错信息。

-o dbglevel=info -f -o curldbg

Q:使用 Goofys 挂载 TOS 失败,日志报错 ERROR Unable to access bucket_demo
A:有可能是 credentials 文件的格式问题导致解析失败。您需要按照以下格式正确编辑 credentials 文件。

[default] 
aws_access_key_id = AK     
aws_secret_access_key = SK 
s3 = 
   addressing_style = virtual

Q:使用 Goofys 挂载 TOS 时报错 s3.DEBUG HEAD https://xxx/goofys-test = 403 []
A:由于 Goofys 使用 path style 挂载 TOS,导致报错 403,您可以使用以下命令挂载 TOS。

goofys --subdomain --dir-mode=0777 --file-mode=0666 --debug_s3 --debug_fuse --profile default -o allow_other --endpoint=https://<S3 Endpoint> <存储桶名称> /<挂载目录>