Skip to content

使用goofys挂载阿里云OSS,替代ossfs

tssujt edited this page Aug 3, 2022 · 4 revisions

1. 安装FUSE

sudo yum install automake gcc-c++ git libcurl-devel libxml2-devel \ fuse-devel make openssl-devel
sudo yum install fuse

2. 安装GO语言

首先打开GO语言官方下载页 https://golang.google.cn/dl/ ,这里使用二进制源码包来安装:

wget https://golang.google.cn/dl/go1.15.linux-amd64.tar.gz #获取安装包
tar zxvf go1.15.linux-amd64.tar.gz #解压
go version #查看go版本,此处返回'command not found',原因为缺少环境变量配置

当前解压目录为:/usr/local/go/bin

vim /etc/profile 
export PATH=$PATH:/usr/local/go/bin #在末尾添加配置
source /etc/profile #使配置生效
go version #再次查看,返回'go version go1.15 lunx/amd64',说明已经恢复正常

主机升级已安装过的GO语言

rm -rf /usr/lib/golang/* #删除原GO安装包
rm -rf /usr/bin/go #删除软连接
rm -rf /usr/bin/gofmt #删除软连接
cd oneinstack
wget https://golang.org/dl/go1.15.linux-amd64.tar.gz #下载最新golang
tar -xzf go1.15.linux-amd64.tar.gz -C /usr/lib/golang #解压到/usr/lib/golang
#将/usr/lib/golang/go 的全部移动到上级/usr/lib/golang/ 
ln -s /usr/lib/golang/bin/* /usr/bin/ #创建软连接

3.1 goofys安装方式1:GO语言安装

$ export GOPATH=$HOME/work
$ go get github.com/kahing/goofys
$ go install github.com/kahing/goofys
#安装后发现goofys提示:command not found,解决方法如下:
ln -s /root/work/bin/* /usr/bin/ #在/usr/bin创建goofys的软连接

3.2 goofys安装方式2:直接下载

wget https://github.com/kahing/goofys/releases/latest/download/goofys #直接下载编译好的goofys
chmod +x goofys #赋予执行权限
goofys --version #验证安装版本,返回'goofys version 0.24.0-45b8d78375af1b24604439d2e60c567654bcdf88'
ln -s /goofys上级安装文件夹/* /usr/bin

4. 挂载文件系统

下面示例将阿里云OSS中名为oss-mybucket的bucket挂载到服务器的/data/wwwroot/example.com/test-folder上:

cat ~/.aws/credentials #编辑密钥文件
[default]
aws_access_key_id = <AccessKey ID>  	
aws_secret_access_key = <AccessKey Secret>  
#<AccessKey ID>替换为阿里云OSS的AccessKey ID,<AccessKey Secret>替换为阿里云OSS的AccessKey Secret,建议使用RAM访问控制生成AccessKey,获取地址:https://ram.console.aliyun.com/
mkdir /data/wwwroot/example.com/test-folder #新建空文件夹
goofys --subdomain --endpoint http://oss-us-east-1.aliyuncs.com --region oss-us-east-1 --uid=1001 --gid=1001 --file-mode=0666 --dir-mode=0777 -o allow_other facfox-upload /data/wwwroot/facfox.com/wp-content/uploads/orderfile
#挂载阿里云OSS。其中'facfox-upload'是阿里云OSS的bucket名称

5. 常用设置

  • 设置阿里云OSS的节点和区域: 挂载命令中,--regionendpoint去掉.aliyuncs.com的部分,例如本实例中:--endpointhttp://oss-us-east-1.aliyuncs.com,那么--region后填写oss-us-east-1
  • 如果要使挂载的文件夹属于某个user
    • 首先通过id命令获得指定用户的uid/gid信息。例如获取www用户的uid/gid信息:id www
    • 然后挂载时指定uid/gid参数:--uid=1001 --gid=1001
    • 注意:uid/gid都是数字。
  • 挂载时如何设置权限? 如果要允许其他用户访问挂载文件夹,可以在运行goofys的时候指定allow_other参数: goofys <your_bucket> <your_mount_point> -o allow_other 注意:allow_other是赋予挂载目录其他用户访问的权限,不是里面的文件!如果您要更改文件夹中的文件,请用chmod命令。
  • 开机自动挂载OSS:
    • 通过开机自启动脚本mount(适用于centos7.0及以下的系统)

      • 在/etc/fstab中加入下面的命令:
      • goofys#oss-mybucket /data/wwwroot/example.com/test-folder fuse _netdev,allow_other,--file-mode=0666,--dir-mode=0777,--uid=1001,--gid=1001,--region=oss-us-east-1,--endpoint=http://oss-us-east-1.aliyuncs.com 0 0
      • 其中上述命令中的oss-mybucket,--region,--endpoint,--uid,--gid信息自行填入。保存/etc/fstab文件。执行mount -a命令,如果没有报错,则说明设置正常。到这一步,ubuntu14.04就能自动挂载了。
      • 还需要执行命令:chkconfig goofys on
    • 通过开机自启动脚本mount(适用于centos7.0及以上的系统)

      • 在/etc/init.d/目录下建立文件goofys,把下方代码中的内容拷贝到这个新文件中。并将其中的参数改成您自己的信息:
      #! /bin/bash
      #
      # goofys  Automount Aliyun OSS Bucket in the specified direcotry using goofys.
      #
      # chkconfig: 2345 90 10
      # description: Activates/Deactivates goofys configured to start at boot time.
      goofys --subdomain --endpoint http://oss-us-east-1.aliyuncs.com --region oss-us-east-1 --uid=1001 --gid=1001 --file-mode=0666 --dir-mode=0777 -o allow_other oss-mybucket /data/wwwroot/example.com/test-folder
      
      • 还需要执行命令:chkconfig goofys on
    • 检查开机启动服务是否生效

      # chkconfig --list  goofys
      注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。
        如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
        欲查看对特定 target 启用的服务请执行
        'systemctl list-dependencies [target]'。
      goofys  0:off   1:off   2:on    3:on    4:on    5:on    6:off
      
    说明goofys开机启动已经生效
    • 解除OSS挂载

      umount /data/wwwroot/example.com/test-folder #root用户命令
      fusermount -u /data/wwwroot/example.com/test-folder #非root用户命令
      #若出现device is busy无法卸载的情况:
      umount -f /data/wwwroot/example.com/test-folder
      #若依然失败:
      lsof /data/wwwroot/example.com/test-folder #找到对应使用文件夹的PID,kill it.
      fuser -km /data/wwwroot/example.com/test-folder #结束占用文件夹的进程
      

6. 注意事项

  • goofys和ossfs的部分挂载命令参数不兼容,例如ossfs中的-o ax_stat_cache_size=xxx, -o noxattr等均不起作用。此外``
  • goofs挂载参数文档:
    NAME:
    goofys - Mount an S3 bucket locally
    
    USAGE:
    goofys [global options] bucket[:prefix] mountpoint
    
    VERSION:
    0.24.0-45b8d78375af1b24604439d2e60c567654bcdf88
    
    GLOBAL OPTIONS:
       -o value            Additional system-specific mount options. Be careful!
       --cache value       Directory to use for data cache. Requires catfs and `-o allow_other'. Can also pass in other catfs options (ex: --cache "--free:10%:$HOME/cache") (default: off)
       --dir-mode value    Permission bits for directories. (default: 0755) (default: 493)
       --file-mode value   Permission bits for files. (default: 0644) (default: 420)
       --uid value         UID owner of all inodes. (default: 0)
       --gid value         GID owner of all inodes. (default: 0)
       --endpoint value    The non-AWS endpoint to connect to. Possible values: http://127.0.0.1:8081/
       --profile value     Use a named profile from $HOME/.aws/credentials instead of "default"
       --use-content-type  Set Content-Type according to file extension and /etc/mime.types (default: off)
       --subdomain         Enable subdomain mode of S3
    
    TUNING OPTIONS:
       --cheap                 Reduce S3 operation costs at the expense of some performance (default: off)
       --no-implicit-dir       Assume all directory objects ("dir/") exist (default: off)
       --stat-cache-ttl value  How long to cache StatObject results and inode attributes. (default: 1m0s)
       --type-cache-ttl value  How long to cache name -> file/dir mappings in directory inodes. (default: 1m0s)
       --http-timeout value    Set the timeout on HTTP requests to S3 (default: 30s)
    
    AWS S3 OPTIONS:
       --region value         The region to connect to. Usually this is auto-detected. Possible values: us-east-1, us-west-1, us-west-2, eu-west-1, eu-central-1, ap-southeast-1, ap-southeast-2, ap-northeast-1, sa-east-1, cn-north-1 (default: "us-east-1")
       --requester-pays       Whether to allow access to requester-pays buckets (default: off)
       --storage-class value  The type of storage to use when writing objects. Possible values: REDUCED_REDUNDANCY, STANDARD, STANDARD_IA. (default: "STANDARD")
       --sse                  Enable basic server-side encryption at rest (SSE-S3) in S3 for all writes (default: off)
       --sse-kms key-id       Enable KMS encryption (SSE-KMS) for all writes using this particular KMS key-id. Leave blank to Use the account's CMK - customer master key (default: off)
       --sse-c value          Enable server-side encryption using this base64-encoded key (default: off)
       --acl value            The canned ACL to apply to the object. Possible values: private, public-read, public-read-write, authenticated-read, aws-exec-read, bucket-owner-read, bucket-owner-full-control (default: off)
    
    MISC OPTIONS:
       --help, -h     Print this help text and exit successfully.
       --debug_fuse   Enable fuse-related debugging output.
       --debug_s3     Enable S3-related debugging output.
       -f             Run goofys in foreground.
       --version, -v  print the version