前置知识:
AWS S3,对象存储(阿里叫oss,腾讯叫cos),mount挂载
一、安装cosfs工具
COSFS 工具支持将 COS 存储桶挂载到本地,像使用本地文件系统一样直接操作腾讯云对象存储中的对象, COSFS 提供的主要功能包括:
- 支持 POSIX 文件系统的大部分功能,如:文件读写、目录操作、链接操作、权限管理、uid/gid 管理等功能。
- 大文件分块传输功能。
- MD5 数据校验功能。
- 将本机数据上传至 COS,建议使用 COS Migration 工具 或 COSCMD 工具。
官网地址:
https://cloud.tencent.com/document/product/436/6883
1. 确认系统版本(这里以centos7为例)
cat /etc/redhat-release # 有的发行版是centos-release
2. yum安装cosfs工具。(下文摘自官网,重点说注意点)
注意点: 如果是腾讯云服务器环境,使用如下安装,可能会没有加入系统变量,需要去安装路径如 /usr/local/sbin中找到并且加入环境变量或者加入软链接。
如果yum源头找不到该工具,可以自行添加。安装报错,多半libxml2以及fuse问题。
- 安装依赖
sudo yum install libxml2-devel libcurl-devel -y
- 根据系统版本选择对应的安装包,目前支持的 CentOS 发行版包括 CentOS6.5、CentOS7.0。
#CentOS6.5
wget https://github.com/tencentyun/cosfs/releases/download/v1.0.19/cosfs-1.0.19-centos6.5.x86_64.rpm
#CentOS7.0
wget https://github.com/tencentyun/cosfs/releases/download/v1.0.19/cosfs-1.0.19-centos7.0.x86_64.rpm
- 安装。以 CentOS7.0为例:
sudo rpm -ivh cosfs-1.0.19-centos7.0.x86_64.rpm
3. 本地方式安装cosfs工具
1. 腾讯官方Cosfs的地址 https://github.com/tencentyun/cosfs/
2. Wget https://github.com/tencentyun/cosfs/releases/download/v1.0.14/cosfs-1.0.14-centos7.0.x86_64.rpm (自行确定最新版本)
4. 编译方式安装cosfs工具
1. 安装依赖软件
COSFS 的编译安装依赖于 automake、git、libcurl-devel、libxml2-devel、fuse-devel、make、openssl-devel 等软件包,Ubuntu 、CentOS、SUSE 和 macOS 的依赖软件安装过程如下:
- Ubuntu 系统下安装依赖软件:
sudo apt-get install automake autotools-dev g++ git libcurl4-gnutls-dev libfuse-dev libssl-dev libxml2-dev make pkg-config fuse
- CentOS 系统下安装依赖软件:
sudo yum install automake gcc-c++ git libcurl-devel libxml2-devel fuse-devel make openssl-devel fuse
- SUSE 系统下安装依赖软件:
sudo zypper install gcc-c++ automake make libcurl-devel libxml2-devel openssl-devel pkg-config
- macOS 系统下安装依赖软件:
brew install automake git curl libxml2 make pkg-config openssl brew cask install osxfuse
2. 获取源码
您需要从 GitHub 上将 COSFS 源码 下载到指定目录,下面以目录
/usr/cosfs
为例(实际操作下,建议您根据具体操作环境选择目录):git clone https://github.com/tencentyun/cosfs /usr/cosfs
3. 编译和安装 COSFS
进入安装目录,执行如下命令进行编译和安装:
cd /usr/cosfs ./autogen.sh ./configure make sudo make install cosfs --version #查看 cosfs 版本号
4. Configure 操作问题处理
根据操作系统的不同,进行 configure 操作时会出现不同的提示,在 fuse 版本低于 2.8.4 的操作系统上,进行 configure 操作时会出现以下报错提示:
checking for common_lib_checking... configure: error: Package requirements (fuse >= 2.8.4 libcurl >= 7.0 libxml-2.0 >= 2.6) were not met:
Requested 'fuse >= 2.8.4' but version of fuse is 2.8.3
此时,您需要手动安装 fuse 2.8.4及以上版本,安装命令示例如下:
yum -y remove fuse-devel
wget https://github.com/libfuse/libfuse/releases/download/fuse_2_9_4/fuse-2.9.4.tar.gz
tar -zxvf fuse-2.9.4.tar.gz
cd fuse-2.9.4
./configure
make
make install
export PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/lib64/pkgconfig/:/usr/local/lib/pkgconfig
modprobe fuse #挂载 fuse 内核模块
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig #更新动态链接库
pkg-config --modversion fuse #查看 fuse 版本号,当看到 “2.9.4” 时,表示 fuse 2.9.4 安装成功
- SUSE 系统下手动安装 fuse 2.8.4及以上版本,安装命令示例如下:
注意:
安装时,需要注释掉
example/fusexmp.c
文件下第222行内容,否则 make 将报错。注释方法为/*content*/
。zypper remove fuse libfuse2 wget https://github.com/libfuse/libfuse/releases/download/fuse_2_9_4/fuse-2.9.4.tar.gz tar -zxvf fuse-2.9.4.tar.gz cd fuse-2.9.4 ./configure make make install export PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/lib64/pkgconfig/:/usr/local/lib/pkgconfig modprobe fuse #挂载 fuse 内核模块 echo "/usr/local/lib" >> /etc/ld.so.conf ldconfig #更新动态链接库 pkg-config --modversion fuse #查看 fuse 版本号,当看到 “2.9.4” 时,表示 fuse2.9.4 安装成功
- 在 macOS 进行 configure 操作时,可能会出现如下提示:
configure: error: Package requirements (fuse >= 2.7.3 libcurl >= 7.0 libxml-2.0 >2.6 libcrypto >= 0.9) were not met
No package 'libcrypto' found
此时,您需要设置 PKG_CONFIG_PATH 变量,以使得 pkg-config 工具能找到 openssl,命令如下:
brew info openssl
export PKG_CONFIG_PATH=/usr/local/opt/openssl/lib/pkgconfig #您可能需要根据上一条命令的提示信息
三、 挂载cosfs存储
1. 配置密钥文件
sudo vim /etc/passwd-cosfs
输入你的对象信息,第一位是对象桶,第二位是accessId,第三位是accessKey,中间使用半角冒号隔开,多个对象存储信息,使用换行符隔开(回车)
examplebucket-1250000000:AKIDHTVVaVR6e3****:PdkhT9e2rZCfy6***
修改权限
sudo chmod 640 /etc/passwd-cosfs
运行工具:
cosfs examplebucket-1250000000 /mnt/cosfs -ourl=http://cos.ap-guangzhou.myqcloud.com -odbglevel=info -onoxattr -oallow_other
<MountPoint> 为本地挂载目录(例如/mnt)。
<Region> 为地域简称, 例如 ap-guangzhou 、 eu-frankfurt 等。更多地域简称信息,请参见 可用地域。
-odbglevel 指定日志级别,默认为crit,可选值为crit、error、warn、info、debug。
-oallow_other 允许非挂载用户访问挂载文件夹。
注意点: 需要确保本地目录存在且为空。地域要选对,后边参数可选
卸载挂载比较简单,使用工具,或者使用原生mount命令均可。
方式1:fusermount -u /mnt, fusermount 命令专用于卸载 FUSE 文件系统
方式2:umount -l /mnt, 当有程序引用文件系统中文件时,进行卸载不会报错,并在没程序引用时完成卸载
方式3:umount /mnt, 当有程序引用文件系统中的文件时,进行卸载会报错