一、FastDFS安装
按照前两步安装好libfatscommon和FastDFS后
配置文件准备
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf #客户端文件,测试用
cp /usr/local/src/fastdfs/conf/http.conf /etc/fdfs/ #供nginx访问使用
cp /usr/local/src/fastdfs/conf/mime.types /etc/fdfs/ #供nginx访问使用
tracker配置
# 在家目录创建tracker文件夹存放相关数据
mkdir /home/i6000/fast/trackervi
/etc/fdfs/tracker.conf
# 设置tracker数据文件存放路径
base_path=/home/i6000/fast/tracker
storage配置
# 在家目录插件storage文件夹存放相关数据
mkdir /home/i6000/fast/storage
vi /etc/fdfs/storage.conf
# 设置storage相关文件存放路径
base_path=/home/i6000/fast/storage
## 修改tracker的ip
tracker_server=192.168.229.130:22122
# 设置数据存储路径
store_path0=/home/i6000/fast/storage
client 配置
# 设置tracker相关文件存放路径
base_path= /home/i6000/fast/tracker
# 当前机器ip
tracker_server=192.168.52.1:22122
启动
cd /etc/fdfs
fdfs_trackerd ./tracker.conf
# storage配置文件要指定绝对路径,启动后会在storage下文件两个文件夹
fdfs_storaged /etc/fdfs/storage.conf
测试上传文件
image.png你可以在storage_path/00/00下找到这这张图片
image.png
二、nginx安装
1、安装fastdfs-nginx-module
2、安装nginx
# --prefix指定安装文件夹, --add-module安装一个子模块
./configure --prefix=/usr/local/nginx/ --add-module=/home/i6000/fast/fastdfs-nginx-module-master/src
make && make install #编译安装
删掉objs/Makefilel里面的-Werror
image.png
编译
make
make install
# 拷贝配置文件
cp fastdfs-nginx-module-master/src/mod_fastdfs.conf /etc/fdfs/
# 修改以下配置
connect_timeout=10 # 超时时间
base_path=/home/i6000/fast/tracker
tracker_server=192.168.229.130:22122
store_path0=/home/i6000/fast/storage
url_have_group_name = true
## 拷贝配置文件
cp fast/fastdfs-master/conf/http.conf /etc/fdfs/
cp fast/fastdfs-master/conf/mime.types /etc/fdfs/
修改nginx配置
# 到上面安装nginx是指定的目录
cd /usr/local/nginx/conf
vi nginx.conf
在keepalive_timeout 65;下面添加如下配置:
server {
listen 8888;
server_name localhost;
location ~/group[0-9]/ {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
启动nginx
cd /usr/local/nginx/sbin
./nginx
测试
打开网址:ip/nginx_port/上传图片时返回的那串代码
http://192.168.229.130:8888/group1/M00/00/00/wKjlglvjxKaAASJKAAN6eCD4Z_c124.png
三、Go上传及读取图片
安装依赖库
- 下载
go get github.com/weilaihui/fdfs_client
- 如果有报错先执行下面命令再执行上面的
cd ~/workspace/go/src
mkdir -p golang.org/x
cd golang.org/x
git clone https://github.com/golang/crypto.git
git clone https://github.com/golang/sys.git
上传图片
1、添加配置文件
我这里是windows连虚拟机
新建一个client.conf文件,修改相关配置
# connect timeout in seconds
# default value is 30s
connect_timeout=30
# network timeout in seconds
# default value is 30s
network_timeout=60
# the base path to store log files
base_path=/home/i6000/fast/tracker
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
# 虚拟机的ip和端口
tracker_server=192.168.229.130:22122
#standard log level as syslog, case insensitive, value list:
### emerg for emergency
### alert
### crit for critical
### error
### warn for warning
### notice
### info
### debug
log_level=info
#HTTP settings
http.tracker_server_port=80
#use "#include" directive to include HTTP other settiongs
##include http.conf
2、上传测试
// 新建fdfs客户端
client, err := fdfs_client.NewFdfsClient("conf/client.conf")
if err != nil{
beego.Info("fdfs连接错误:", err)
return ""
}
// 获取一个字节数组,大小和文件相等
fileBuffer := make([]byte, head.Size)
// 写入图片信息
file.Read(fileBuffer)
// 上传文件到fdfs
res, err := client.UploadByBuffer(fileBuffer, ext[1:])
if err != nil{
beego.Info("上传fdfs失败:", err)
return ""
}
beego.Info("上传fdfs成功:", res)
return "1"
3、页面前台读取
<!-- 配置nginx的ip:端口/上传图片时的返回码 -->
<a href=".Url"><img src="http://192.168.229.130:8888/group1/M00/00/00/wKjlglvjxKaAASJKAAN6eCD4Z_c124.png“
网友评论