示例服务器ip: 192.168.19.124
FastDFS版本: 6.12.2
FastDFS客户端版本:
Nginx版本: 1.24.0
FastDFS服务端
1. 安装环境准备
yum -y install zlib zlib-devel pcre pcre-devel gcc gcc-c++ openssl openssl-devel libevent libevent-devel perl unzip net-tools wget gd-devel epel-release2. 下载并解压fastDFS依赖包
仓库地址 https://github.com/happyfish100/libfastcommon/tags
这里选择的是libfastcommon-1.0.75版本
[root@k8s-nfs opt]# tar -zxvf libfastcommon-1.0.75.tar.gz -C /usr/local/
[root@k8s-nfs opt]# cd /usr/local/libfastcommon-1.0.75/
[root@k8s-nfs libfastcommon-1.0.75]# pwd
/usr/local/libfastcommon-1.0.75
[root@k8s-nfs libfastcommon-1.0.75]# ls -lhra
总用量 56K
drwxrwxr-x. 3 root root 4.0K 9月 22 12:19 src
-rw-rw-r--. 1 root root 2.8K 9月 22 12:19 README
drwxrwxr-x. 2 root root 191 9月 22 12:19 php-fastcommon
-rwxrwxr-x. 1 root root 5.6K 9月 22 12:19 make.sh
-rw-rw-r--. 1 root root 7.5K 9月 22 12:19 LICENSE
-rw-rw-r--. 1 root root 1.5K 9月 22 12:19 libfastcommon.spec
-rw-rw-r--. 1 root root 673 9月 22 12:19 INSTALL
-rw-rw-r--. 1 root root 19K 9月 22 12:19 HISTORY
-rw-rw-r--. 1 root root 1.9K 9月 22 12:19 .gitignore
drwxrwxr-x. 2 root root 114 9月 22 12:19 doc
drwxrwxr-x. 3 root root 192 9月 22 12:19 debian
drwxr-xr-x. 14 root root 181 2月 28 09:51 ..
drwxrwxr-x. 6 root root 182 9月 22 12:19 .
[root@k8s-nfs libfastcommon-1.0.75]#3. 编译安装fastDFS依赖
[root@k8s-nfs libfastcommon-1.0.75]# ./make.sh
gcc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o hash.o hash.c
gcc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o chain.o chain.c
gcc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o shared_func.o shared_func.c
...
[root@k8s-nfs libfastcommon-1.0.75]# ./make.sh install
mkdir -p /usr/lib64
mkdir -p /usr/lib
mkdir -p /usr/include/fastcommon
install -m 755 libfastcommon.so /usr/lib64
install -m 644 common_define.h hash.h chain.h logger.h base64.h shared_func.h pthread_func.h ini_file_reader.h _os_define.h sockopt.h sched_thread.h http_func.h md5.h local_ip_func.h avl_tree.h ioevent.h ioevent_loop.h fast_task_queue.h fast_timer.h locked_timer.h process_ctrl.h fast_mblock.h connection_pool.h fast_mpool.h fast_allocator.h fast_buffer.h skiplist.h multi_skiplist.h flat_skiplist.h skiplist_common.h system_info.h fast_blocked_queue.h php7_ext_wrapper.h id_generator.h char_converter.h char_convert_loader.h common_blocked_queue.h multi_socket_client.h skiplist_set.h uniq_skiplist.h fc_list.h locked_list.h json_parser.h buffered_file_writer.h server_id_func.h fc_queue.h sorted_queue.h fc_memory.h shared_buffer.h thread_pool.h fc_atomic.h array_allocator.h sorted_array.h /usr/include/fastcommon
[root@k8s-nfs libfastcommon-1.0.75]#4. 下载并解压fastDFS的网络框架
仓库地址 https://github.com/happyfish100/libserverframe/tags
这里选择的是libserverframe-1.2.5版本
[root@k8s-nfs opt]# tar -zxvf libserverframe-1.2.5.tar.gz -C /usr/local/
[root@k8s-nfs opt]# cd /usr/local/libserverframe-1.2.5/
[root@k8s-nfs libserverframe-1.2.5]# pwd
/usr/local/libserverframe-1.2.5
[root@k8s-nfs libserverframe-1.2.5]# ls -lhra
总用量 60K
drwxrwxr-x. 4 root root 4.0K 9月 22 12:21 src
-rw-rw-r--. 1 root root 1.6K 9月 22 12:21 sample.conf
-rw-rw-r--. 1 root root 61 9月 22 12:21 README.md
-rwxrwxr-x. 1 root root 4.0K 9月 22 12:21 make.sh
-rw-rw-r--. 1 root root 34K 9月 22 12:21 LICENSE
-rw-rw-r--. 1 root root 1.2K 9月 22 12:21 libserverframe.spec
-rw-rw-r--. 1 root root 277 9月 22 12:21 .gitignore
drwxrwxr-x. 3 root root 211 9月 22 12:21 debian
drwxr-xr-x. 15 root root 209 2月 28 10:06 ..
drwxrwxr-x. 4 root root 142 9月 22 12:21 .
[root@k8s-nfs libserverframe-1.2.5]#5. 编译安装fastDFS的网络框架
[root@k8s-nfs libserverframe-1.2.5]# ./make.sh
gcc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -fPIC -c -o sf_nio.lo sf_nio.c -Iinclude -I/usr/local/include
gcc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -fPIC -c -o sf_iov.lo sf_iov.c -Iinclude -I/usr/local/include
...
[root@k8s-nfs libserverframe-1.2.5]# ./make.sh install
mkdir -p /usr/lib64
mkdir -p /usr/lib
mkdir -p /usr/include/sf/idempotency/common
mkdir -p /usr/include/sf/idempotency/server
mkdir -p /usr/include/sf/idempotency/client
install -m 755 libserverframe.so /usr/lib64
cp -f sf_types.h sf_global.h sf_define.h sf_nio.h sf_service.h sf_func.h sf_util.h sf_configs.h sf_proto.h sf_cluster_cfg.h sf_sharding_htable.h sf_connection_manager.h sf_serializer.h sf_binlog_index.h sf_file_writer.h sf_binlog_writer.h sf_ordered_writer.h sf_buffered_writer.h sf_iov.h sf_shared_mbuffer.h /usr/include/sf
cp -f idempotency/common/idempotency_types.h /usr/include/sf/idempotency/common
cp -f idempotency/server/server_types.h idempotency/server/server_channel.h idempotency/server/request_htable.h idempotency/server/channel_htable.h idempotency/server/server_handler.h idempotency/server/request_metadata.h /usr/include/sf/idempotency/server
cp -f idempotency/client/client_types.h idempotency/client/receipt_handler.h idempotency/client/client_channel.h idempotency/client/rpc_wrapper.h /usr/include/sf/idempotency/client
[root@k8s-nfs libserverframe-1.2.5]# 6. 下载并解压fastDFS
仓库地址 https://github.com/happyfish100/fastdfs/tags
这里选择的是fastdfs-6.12.2版本
[root@k8s-nfs opt]# tar -zxvf fastdfs-6.12.2.tar.gz -C /usr/local/
[root@k8s-nfs opt]# cd /usr/local/fastdfs-6.12.2/
[root@k8s-nfs fastdfs-6.12.2]# pwd
/usr/local/fastdfs-6.12.2
[root@k8s-nfs fastdfs-6.12.2]# ls -lhra
总用量 144K
drwxrwxr-x. 2 root root 4.0K 9月 17 11:50 tracker
drwxrwxr-x. 2 root root 4.0K 9月 17 11:50 test
drwxrwxr-x. 2 root root 64 9月 17 11:50 systemd
drwxrwxr-x. 4 root root 4.0K 9月 17 11:50 storage
-rwxrwxr-x. 1 root root 763 9月 17 11:50 setup.sh
-rw-rw-r--. 1 root root 2.1K 9月 17 11:50 README_zh.md
-rw-rw-r--. 1 root root 2.8K 9月 17 11:50 README.md
drwxrwxr-x. 2 root root 4.0K 9月 17 11:50 php_client
-rwxrwxr-x. 1 root root 6.1K 9月 17 11:50 make.sh
-rw-rw-r--. 1 root root 8.6K 9月 17 11:50 INSTALL
drwxrwxr-x. 2 root root 48 9月 17 11:50 init.d
drwxrwxr-x. 2 root root 27 9月 17 11:50 images
-rw-rw-r--. 1 root root 38K 9月 17 11:50 HISTORY
-rw-rw-r--. 1 root root 1.7K 9月 17 11:50 .gitignore
-rw-rw-r--. 1 root root 3.1K 9月 17 11:50 fastdfs.spec
drwxrwxr-x. 5 root root 87 9月 17 11:50 docker
drwxrwxr-x. 3 root root 4.0K 9月 17 11:50 debian
-rw-rw-r--. 1 root root 35K 9月 17 11:50 COPYING-3_0.txt
drwxrwxr-x. 2 root root 146 9月 17 11:50 conf
drwxrwxr-x. 2 root root 189 9月 17 11:50 common
drwxrwxr-x. 3 root root 4.0K 9月 17 11:50 client
drwxr-xr-x. 14 root root 181 2月 28 09:51 ..
drwxrwxr-x. 14 root root 4.0K 9月 17 11:50 .
[root@k8s-nfs fastdfs-6.12.2]#7. 编译安装fastDFS
[root@k8s-nfs fastdfs-6.12.2]# ./make.sh
gcc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o ../common/fdfs_global.o ../common/fdfs_global.c -I../common -I/usr/local/include
gcc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o tracker_proto.o tracker_proto.c -I../common -I/usr/local/include
gcc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o tracker_mem.o tracker_mem.c -I../common -I/usr/local/include
...
[root@k8s-nfs fastdfs-6.12.2]# ./make.sh install
mkdir -p /usr/bin
mkdir -p /etc/fdfs
cp -f fdfs_trackerd /usr/bin
if [ ! -f /etc/fdfs/tracker.conf ]; then cp -f ../conf/tracker.conf /etc/fdfs/tracker.conf; fi
if [ ! -f /etc/fdfs/storage_ids.conf ]; then cp -f ../conf/storage_ids.conf /etc/fdfs/storage_ids.conf; fi
mkdir -p /usr/bin
mkdir -p /etc/fdfs
cp -f fdfs_storaged /usr/bin
if [ ! -f /etc/fdfs/storage.conf ]; then cp -f ../conf/storage.conf /etc/fdfs/storage.conf; fi
mkdir -p /usr/bin
mkdir -p /etc/fdfs
mkdir -p /usr/lib64
mkdir -p /usr/lib
cp -f fdfs_monitor fdfs_test fdfs_test1 fdfs_crc32 fdfs_upload_file fdfs_download_file fdfs_delete_file fdfs_file_info fdfs_appender_test fdfs_appender_test1 fdfs_append_file fdfs_upload_appender fdfs_regenerate_filename /usr/bin
if [ 0 -eq 1 ]; then cp -f libfdfsclient.a /usr/lib64; cp -f libfdfsclient.a /usr/lib/; fi
if [ 1 -eq 1 ]; then cp -f libfdfsclient.so /usr/lib64; cp -f libfdfsclient.so /usr/lib/; fi
mkdir -p /usr/include/fastdfs
cp -f ../common/fdfs_define.h ../common/fdfs_global.h ../common/mime_file_parser.h ../common/fdfs_http_shared.h ../tracker/tracker_types.h ../tracker/tracker_proto.h ../tracker/fdfs_shared_func.h ../tracker/fdfs_server_id_func.h ../storage/trunk_mgr/trunk_shared.h tracker_client.h storage_client.h storage_client1.h client_func.h client_global.h fdfs_client.h /usr/include/fastdfs
if [ ! -f /etc/fdfs/client.conf ]; then cp -f ../conf/client.conf /etc/fdfs/client.conf; fi
[root@k8s-nfs fastdfs-6.12.2]# 8. 补全fastdfs的配置文件, 供后续nginx使用
将fastDFS安装包下的http.conf和mime.types拷贝到/etc/fdfs下
[root@k8s-nfs opt]# cd /usr/local/fastdfs-6.12.2/conf/
[root@k8s-nfs conf]# pwd
/usr/local/fastdfs-6.12.2/conf
[root@k8s-nfs conf]# ls -lhra
总用量 96K
-rw-rw-r--. 1 root root 9.9K 9月 17 11:50 tracker.conf
-rw-rw-r--. 1 root root 874 9月 17 11:50 storage_ids.conf
-rw-rw-r--. 1 root root 11K 9月 17 11:50 storage.conf
-rw-rw-r--. 1 root root 31K 9月 17 11:50 mime.types
-rw-rw-r--. 1 root root 965 9月 17 11:50 http.conf
-rw-rw-r--. 1 root root 2.3K 9月 17 11:50 client.conf
-rw-rw-r--. 1 root root 24K 9月 17 11:50 anti-steal.jpg
drwxrwxr-x. 14 root root 4.0K 9月 17 11:50 ..
drwxrwxr-x. 2 root root 146 9月 17 11:50 .
[root@k8s-nfs conf]# cp ./http.conf /etc/fdfs/
[root@k8s-nfs conf]# cp ./mime.types /etc/fdfs/
[root@k8s-nfs conf]# cd /etc/fdfs/
[root@k8s-nfs fdfs]# ls -lhra
总用量 80K
-rw-r--r--. 1 root root 9.9K 2月 28 10:10 tracker.conf
-rw-r--r--. 1 root root 874 2月 28 10:10 storage_ids.conf
-rw-r--r--. 1 root root 11K 2月 28 10:10 storage.conf
-rw-r--r--. 1 root root 31K 2月 28 10:19 mime.types
-rw-r--r--. 1 root root 965 2月 28 10:18 http.conf
-rw-r--r--. 1 root root 2.3K 2月 28 10:10 client.conf
drwxr-xr-x. 80 root root 8.0K 2月 28 10:10 ..
drwxr-xr-x. 2 root root 124 2月 28 10:19 .
[root@k8s-nfs fdfs]#9. 准备fastDFS使用的目录
[root@k8s-nfs storage]# cd /opt/
[root@k8s-nfs opt]# mkdir -p /opt/fastdfs/client
[root@k8s-nfs opt]# mkdir -p /opt/fastdfs/tracker
[root@k8s-nfs opt]# mkdir -p /opt/fastdfs/storage/files
[root@k8s-nfs opt]# cd /opt/fastdfs/
[root@k8s-nfs fastdfs]# echo 403 > ./403.txt10. 修改tracker配置
[root@k8s-nfs opt]# cd /etc/fdfs/
[root@k8s-nfs fdfs]# pwd
/etc/fdfs
[root@k8s-nfs fdfs]# ls -lhra
总用量 80K
-rw-r--r--. 1 root root 9.9K 2月 28 10:10 tracker.conf
-rw-r--r--. 1 root root 874 2月 28 10:10 storage_ids.conf
-rw-r--r--. 1 root root 11K 2月 28 10:10 storage.conf
-rw-r--r--. 1 root root 31K 2月 28 10:19 mime.types
-rw-r--r--. 1 root root 965 2月 28 10:18 http.conf
-rw-r--r--. 1 root root 2.3K 2月 28 10:10 client.conf
drwxr-xr-x. 80 root root 8.0K 2月 28 10:10 ..
drwxr-xr-x. 2 root root 124 2月 28 10:22 .
[root@k8s-nfs fdfs]# vi tracker.conf
# 需要修改的内容如下:
base_path = /opt/fastdfs/tracker11. 修改storage配置
[root@k8s-nfs opt]# cd /etc/fdfs/
[root@k8s-nfs fdfs]# pwd
/etc/fdfs
[root@k8s-nfs fdfs]# ls -lhra
总用量 80K
-rw-r--r--. 1 root root 9.9K 2月 28 10:10 tracker.conf
-rw-r--r--. 1 root root 874 2月 28 10:10 storage_ids.conf
-rw-r--r--. 1 root root 11K 2月 28 10:10 storage.conf
-rw-r--r--. 1 root root 31K 2月 28 10:19 mime.types
-rw-r--r--. 1 root root 965 2月 28 10:18 http.conf
-rw-r--r--. 1 root root 2.3K 2月 28 10:10 client.conf
drwxr-xr-x. 80 root root 8.0K 2月 28 10:10 ..
drwxr-xr-x. 2 root root 124 2月 28 10:22 .
[root@k8s-nfs fdfs]# vi storage.conf
# 需要修改的内容如下:
base_path=/opt/fastdfs/storage
store_path0=/opt/fastdfs/storage/files
tracker_server=192.168.19.124:22122 #tracker_server的地址
http.server_port = 80 # 与后续nginx监听端口保持一致
# 添加这一行配置:
http.supported.mime.types=/etc/fdfs/mime.types12. 启动tracker和storage
修改/usr/lib/systemd/system/fdfs_trackerd.service文件, 内容修改为:
[Unit]
Description=FastDFS Trackerd Service (CompileBy=zhangyl07)
After=network-online.target
[Service]
Type=forking
PIDFile=/opt/fastdfs/tracker/data/fdfs_trackerd.pid
ExecStart=/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
ExecStartPost=/bin/sleep 0.1
ExecStop=/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf stop
# No artificial start/stop timeout
TimeoutSec=0
# Disable OOM kill by Linux kernel
OOMScoreAdjust=-1000
[Install]
WantedBy=multi-user.target修改/usr/lib/systemd/system/fdfs_storaged.service文件, 内容修改为:
[Unit]
Description=FastDFS Storaged Service (CompileBy=zhangyl07)
After=network-online.target
[Service]
Type=forking
PIDFile=/opt/fastdfs/storage/data/fdfs_storaged.pid
ExecStart=/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
ExecStartPost=/bin/sleep 0.1
ExecStop=/usr/bin/fdfs_storaged /etc/fdfs/storage.conf stop
# No artificial start/stop timeout
TimeoutSec=0
# Disable OOM kill by Linux kernel
OOMScoreAdjust=-1000
[Install]
WantedBy=multi-user.target[root@k8s-nfs opt]# cd /etc/fdfs/
[root@k8s-nfs fdfs]# pwd
/etc/fdfs
[root@k8s-nfs fdfs]# systemctl daemon-reload
[root@k8s-nfs fdfs]# systemctl enable fdfs_storaged.service
[root@k8s-nfs fdfs]# systemctl enable fdfs_trackerd.service
[root@k8s-nfs fdfs]# systemctl start fdfs_storaged.service
[root@k8s-nfs fdfs]# systemctl start fdfs_trackerd.service
[root@k8s-nfs fdfs]# netstat -ntlp | grep fdfs
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 20098/fdfs_trackerd
tcp 0 0 0.0.0.0:23000 0.0.0.0:* LISTEN 20087/fdfs_storaged
[root@k8s-nfs fdfs]#13. 使用fdfs_client进行测试
配置fdfs_client
[root@k8s-nfs opt]# cd /etc/fdfs/
[root@k8s-nfs fdfs]# pwd
/etc/fdfs
[root@k8s-nfs fdfs]# vi client.conf
# 需要修改的内容如下:
base_path=/opt/fastdfs/client
tracker_server=192.168.19.124:22122 #tracker_server的地址写入一段内容到文本中, 测试上传到fastdfs
[root@k8s-nfs fdfs]# echo hello > ./test.txt
[root@k8s-nfs fdfs]# ls -lhra
总用量 84K
-rw-r--r--. 1 root root 9.9K 2月 28 10:25 tracker.conf
-rw-r--r--. 1 root root 6 2月 28 13:57 test.txt
-rw-r--r--. 1 root root 874 2月 28 10:10 storage_ids.conf
-rw-r--r--. 1 root root 11K 2月 28 13:53 storage.conf
-rw-r--r--. 1 root root 31K 2月 28 10:19 mime.types
-rw-r--r--. 1 root root 965 2月 28 10:18 http.conf
-rw-r--r--. 1 root root 2.3K 2月 28 13:56 client.conf
drwxr-xr-x. 80 root root 8.0K 2月 28 10:10 ..
drwxr-xr-x. 2 root root 140 2月 28 13:57 .
[root@k8s-nfs fdfs]# fdfs_test /etc/fdfs/client.conf upload ./test.txt
This is FastDFS client test program v6.12.2
Copyright (C) 2008, Happy Fish / YuQing
FastDFS may be copied only under the terms of the GNU General
Public License V3, which may be found in the FastDFS source kit.
Please visit the FastDFS Home Page http://www.fastken.com/
for more detail.
tracker_query_storage_store_list_without_group:
server 1. group_name=, ip_addr=192.168.19.124, port=23000
group_name=group1, ip_addr=192.168.19.124, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/wKgTfGfBUICAGC2pAAAABjY6MCA564.txt
source ip address: 192.168.19.124
file timestamp=2025-02-28 13:58:24
file size=6
file crc32=909783072
example file url: http://192.168.19.124/group1/M00/00/00/wKgTfGfBUICAGC2pAAAABjY6MCA564.txt
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKgTfGfBUICAGC2pAAAABjY6MCA564_big.txt
source ip address: 192.168.19.124
file timestamp=2025-02-28 13:58:24
file size=6
file crc32=909783072
example file url: http://192.168.19.124/group1/M00/00/00/wKgTfGfBUICAGC2pAAAABjY6MCA564_big.txt
[root@k8s-nfs fdfs]#
[root@k8s-nfs 00]# cd /etc/fdfs/
[root@k8s-nfs fdfs]# cd /opt/fastdfs/storage/files/data/00/00
[root@k8s-nfs 00]# pwd
/opt/fastdfs/storage/files/data/00/00
[root@k8s-nfs 00]# ls -lhra
总用量 28K
-rw-r--r--. 1 root root 49 2月 28 13:58 wKgTfGfBUICAGC2pAAAABjY6MCA564.txt-m
-rw-r--r--. 1 root root 6 2月 28 13:58 wKgTfGfBUICAGC2pAAAABjY6MCA564.txt
-rw-r--r--. 1 root root 49 2月 28 13:58 wKgTfGfBUICAGC2pAAAABjY6MCA564_big.txt-m
-rw-r--r--. 1 root root 6 2月 28 13:58 wKgTfGfBUICAGC2pAAAABjY6MCA564_big.txt
drwxr-xr-x. 258 root root 8.0K 2月 28 13:53 ..
drwxr-xr-x. 2 root root 186 2月 28 13:58 .
[root@k8s-nfs 00]# cat wKgTfGfBUICAGC2pAAAABjY6MCA564.txt
hello
[root@k8s-nfs 00]#保留控制台打印的信息:
group_name=group1, remote_filename=M00/00/00/wKgTfGfBUICAGC2pAAAABjY6MCA564.txt
14. 下载并安装cmake
下载libarchive-3.1.2-14.el7_7.rpm和cmake-2.8.12.2-2.el7.rpm并上传到opt目录下
[root@dc-60 opt]# rpm -Uvh *.rpm --nodeps --force
Preparing... ################################# [100%]
Updating / installing...
1:libarchive-3.1.2-14.el7_7 ################################# [ 50%]
2:cmake-2.8.12.2-2.el7 ################################# [100%]
[root@dc-60 opt]#
[root@dc-60 opt]#
[root@dc-60 opt]# cmake --version
cmake version 2.8.12.2
[root@dc-60 opt]#15. 编译安装libpng
[root@dc-60 opt]# wget http://downloads.sourceforge.net/libpng/libpng-1.6.37.tar.gz
[root@dc-60 opt]# tar -zxvf libpng-1.6.37.tar.gz -C /usr/local/
[root@dc-60 opt]# cd /usr/local/libpng-1.6.37/
[root@dc-60 libpng-1.6.37]# pwd
/usr/local/libpng-1.6.37
[root@dc-60 libpng-1.6.37]# ./configure --prefix=/usr/local/libpng-1.6.37
checking for a BSD-compatible install... /bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
...
[root@dc-60 libpng-1.6.37]# make
rm -f pnglibconf.c pnglibconf.tf[45]
gawk -f ./scripts/options.awk out=pnglibconf.tf4 version=search\
./pngconf.h ./scripts/pnglibconf.dfa\
./pngusr.dfa 1>&2
gawk -f ./scripts/options.awk out=pnglibconf.tf5 pnglibconf.tf4 1>&2
rm pnglibconf.tf4
...
[root@dc-60 libpng-1.6.37]# make install
make install-am
make[1]: Entering directory `/usr/local/libpng-1.6.37'
make[2]: Entering directory `/usr/local/libpng-1.6.37'
/bin/mkdir -p '/usr/local/libpng-1.6.37/lib'
/bin/sh ./libtool --mode=install /bin/install -c libpng16.la '/usr/local/libpng-1.6.37/lib'
libtool: install: /bin/install -c .libs/libpng16.so.16.37.0 /usr/local/libpng-1.6.37/lib/libpng16.so.16.37.0
...
[root@dc-60 libpng-1.6.37]# pwd
/usr/local/libpng-1.6.37
# 更新动态库链接
[root@dc-60 libpng-1.6.37]# echo "/usr/local/libpng-1.6.37/lib" >> /etc/ld.so.conf
[root@dc-60 libpng-1.6.37]# ldconfig16. 编译安装libjpeg
[root@dc-60 libpng-1.6.37]# cd /opt/
[root@dc-60 opt]# wget https://download.sourceforge.net/libjpeg-turbo/libjpeg-turbo-2.1.4.tar.gz
[root@dc-60 opt]# tar -xzvf libjpeg-turbo-2.1.4.tar.gz -C /usr/local/
[root@dc-60 opt]# cd /usr/local/libjpeg-turbo-2.1.4/
[root@dc-60 libjpeg-turbo-2.1.4]# pwd
/usr/local/libjpeg-turbo-2.1.4
[root@dc-60 libjpeg-turbo-2.1.4]# mkdir build
[root@dc-60 libjpeg-turbo-2.1.4]# cd build/
[root@dc-60 build]# ls
[root@dc-60 build]#
[root@dc-60 build]# cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/libjpeg-turbo-2.1.4
-- The C compiler identification is GNU 4.8.5
-- Check for working C compiler: /bin/cc
-- Check for working C compiler: /bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
...
[root@dc-60 build]# make
Scanning dependencies of target simd
[ 1%] Building C object CMakeFiles/simd.dir/jsimd_none.c.o
[ 1%] Built target simd
Scanning dependencies of target jpeg-static
[ 1%] Building C object CMakeFiles/jpeg-static.dir/jcapimin.c.o
[ 2%] Building C object CMakeFiles/jpeg-static.dir/jcapistd.c.o
[ 2%] Building C object CMakeFiles/jpeg-static.dir/jccoefct.c.o
...
[root@dc-60 build]# make install
[ 1%] Built target simd
[ 19%] Built target jpeg-static
[ 22%] Built target cjpeg-static
[ 25%] Built target djpeg-static
[ 26%] Built target jpegtran-static
...
[root@dc-60 build]# ls /usr/local/libjpeg-turbo-2.1.4/lib64/libjpeg*
/usr/local/libjpeg-turbo-2.1.4/lib64/libjpeg.a /usr/local/libjpeg-turbo-2.1.4/lib64/libjpeg.so.62
/usr/local/libjpeg-turbo-2.1.4/lib64/libjpeg.so /usr/local/libjpeg-turbo-2.1.4/lib64/libjpeg.so.62.3.0
[root@dc-60 build]#
# 更新动态库链接
[root@dc-60 build]# echo "/usr/local/libjpeg-turbo-2.1.4/lib64" >> /etc/ld.so.conf
[root@dc-60 build]# ldconfig17. 编译安装gd
[root@dc-60 build]# cd /opt/
[root@dc-60 opt]# wget https://github.com/libgd/libgd/releases/download/gd-2.3.3/libgd-2.3.3.tar.gz
[root@dc-60 opt]# tar -zxvf libgd-2.3.3.tar.gz -C /usr/local/
[root@dc-60 opt]# cd /usr/local/libgd-2.3.3/
[root@dc-60 libgd-2.3.3]# ./configure --prefix=/usr/local/libgd-2.3.3 --with-png=/usr/local/libpng-1.6.37 --with-jpeg=/usr/local/libjpeg-turbo-2.1.4
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for a BSD-compatible install... /bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
...
[root@dc-60 libgd-2.3.3]# make
Making all in src
make[1]: Entering directory `/usr/local/libgd-2.3.3/src'
make all-am
make[2]: Entering directory `/usr/local/libgd-2.3.3/src'
depbase=`echo gdcmpgif.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
gcc -std=gnu99 -DHAVE_CONFIG_H -I. -I/usr/local/libpng-1.6.37/include -I/usr/local/libpng-1.6.37/include/libpng16 -g -O2 -fvisibility=hidden -Wall -MT gdcmpgif.o -MD -MP -MF $depbase.Tpo -c -o gdcmpgif.o gdcmpgif.c &&\
mv -f $depbase.Tpo $depbase.Po
depbase=`echo gd.lo | sed 's|[^/]*$|.deps/&|;s|\.lo$||'`;\
...
[root@dc-60 libgd-2.3.3]# make install
Making install in src
make[1]: Entering directory `/usr/local/libgd-2.3.3/src'
make[2]: Entering directory `/usr/local/libgd-2.3.3/src'
/bin/mkdir -p '/usr/local/libgd-2.3.3/lib'
/bin/sh ../libtool --mode=install /bin/install -c libgd.la '/usr/local/libgd-2.3.3/lib'
libtool: install: /bin/install -c .libs/libgd.so.3.0.11 /usr/local/libgd-2.3.3/lib/libgd.so.3.0.11
libtool: install: (cd /usr/local/libgd-2.3.3/lib && { ln -s -f libgd.so.3.0.11 libgd.so.3 || { rm -f libgd.so.3 && ln -s libgd.so.3.0.11 libgd.so.3; }; })
libtool: install: (cd /usr/local/libgd-2.3.3/lib && { ln -s -f libgd.so.3.0.11 libgd.so || { rm -f libgd.so && ln -s libgd.so.3.0.11 libgd.so; }; })
libtool: install: /bin/install -c .libs/libgd.lai /usr/local/libgd-2.3.3/lib/libgd.la
libtool: install: /bin/install -c .libs/libgd.a /usr/local/libgd-2.3.3/lib/libgd.a
libtool: install: chmod 644 /usr/local/libgd-2.3.3/lib/libgd.a
libtool: install: ranlib /usr/local/libgd-2.3.3/lib/libgd.a
libtool: finish: PATH="/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/sbin" ldconfig -n /usr/local/libgd-2.3.3/lib
----------------------------------------------------------------------
Libraries have been installed in:
/usr/local/libgd-2.3.3/lib
...
# 更新动态库链接
[root@dc-60 libgd-2.3.3]# echo "/usr/local/libgd-2.3.3/lib" >> /etc/ld.so.conf
[root@dc-60 libgd-2.3.3]# ldconfig
[root@dc-60 libgd-2.3.3]#
# 检查gd是否安装成功
[root@dc-60 libgd-2.3.3]# ls /usr/local/libgd-2.3.3/include/gd.h
/usr/local/libgd-2.3.3/include/gd.h
[root@dc-60 libgd-2.3.3]#
[root@dc-60 libgd-2.3.3]# ldconfig -p | grep libgd
libgdbm_compat.so.4 (libc6,x86-64) => /lib64/libgdbm_compat.so.4
libgdbm.so.4 (libc6,x86-64) => /lib64/libgdbm.so.4
libgd.so.3 (libc6,x86-64) => /usr/local/libgd-2.3.3/lib/libgd.so.3
libgd.so (libc6,x86-64) => /usr/local/libgd-2.3.3/lib/libgd.so
[root@dc-60 libgd-2.3.3]# 18. 下载并解压fastdfs-nginx-module插件
仓库地址 https://github.com/happyfish100/fastdfs-nginx-module/tags
这里选择的是fastdfs-nginx-module-1.24版本
[root@k8s-nfs opt]# tar -zxvf fastdfs-nginx-module-1.24.tar.gz -C /usr/local/
fastdfs-nginx-module-1.24/
fastdfs-nginx-module-1.24/.gitignore
fastdfs-nginx-module-1.24/HISTORY
fastdfs-nginx-module-1.24/INSTALL
fastdfs-nginx-module-1.24/LICENSE
fastdfs-nginx-module-1.24/src/
fastdfs-nginx-module-1.24/src/common.c
fastdfs-nginx-module-1.24/src/common.h
fastdfs-nginx-module-1.24/src/config
fastdfs-nginx-module-1.24/src/mod_fastdfs.conf
fastdfs-nginx-module-1.24/src/ngx_http_fastdfs_module.c
[root@k8s-nfs opt]# 19. 下载并解压nginx
nginx版本与fastdfs-nginx-module插件版本保持一致, 这里选择nginx-1.24.0版本
[root@k8s-nfs opt]# tar -zxvf nginx-1.24.0.tar.gz -C /usr/local/20. 编译安装nginx
[root@k8s-nfs opt]# cd /usr/local/nginx-1.24.0/
[root@k8s-nfs nginx-1.24.0]# pwd
/usr/local/nginx-1.24.0
[root@k8s-nfs nginx-1.24.0]# ls -lhra
总用量 816K
drwxr-xr-x. 9 1001 1001 91 2月 28 14:36 src
-rw-r--r--. 1 1001 1001 49 4月 11 2023 README
drwxr-xr-x. 2 1001 1001 21 2月 28 14:36 man
-rw-r--r--. 1 1001 1001 1.4K 4月 11 2023 LICENSE
drwxr-xr-x. 2 1001 1001 40 2月 28 14:36 html
drwxr-xr-x. 4 1001 1001 72 2月 28 14:36 contrib
-rwxr-xr-x. 1 1001 1001 2.6K 4月 11 2023 configure
drwxr-xr-x. 2 1001 1001 168 2月 28 14:36 conf
-rw-r--r--. 1 1001 1001 483K 4月 11 2023 CHANGES.ru
-rw-r--r--. 1 1001 1001 316K 4月 11 2023 CHANGES
drwxr-xr-x. 6 1001 1001 4.0K 2月 28 14:36 auto
drwxr-xr-x. 17 root root 262 2月 28 14:36 ..
drwxr-xr-x. 8 1001 1001 158 4月 11 2023 .
[root@k8s-nfs nginx-1.24.0]# ls /usr/local/libgd-2.3.3/include/gd.h
/usr/local/libgd-2.3.3/include/gd.h
[root@k8s-nfs nginx-1.24.0]# ls /usr/local/libgd-2.3.3/lib/libgd.so*
/usr/local/libgd-2.3.3/lib/libgd.so /usr/local/libgd-2.3.3/lib/libgd.so.3 /usr/local/libgd-2.3.3/lib/libgd.so.3.0.11
[root@k8s-nfs nginx-1.24.0]# export C_INCLUDE_PATH=/usr/local/libgd-2.3.3/include:$C_INCLUDE_PATH
[root@k8s-nfs nginx-1.24.0]# export LIBRARY_PATH=/usr/local/libgd-2.3.3/lib:$LIBRARY_PATH
[root@k8s-nfs nginx-1.24.0]# export LD_LIBRARY_PATH=/usr/local/libgd-2.3.3/lib:$LD_LIBRARY_PATH
[root@k8s-nfs nginx-1.24.0]# ./configure \
--prefix=/usr/local/nginx \
--with-http_image_filter_module \
--add-module=/usr/local/fastdfs-nginx-module-1.24/src/ \
--with-cc-opt="-I/usr/local/libgd-2.3.3/include" \
--with-ld-opt="-L/usr/local/libgd-2.3.3/lib"
checking for OS
+ Linux 3.10.0-1160.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
checking for gcc -pipe switch ... found
checking for --with-ld-opt="-L/usr/local/libgd-2.3.3/lib" ... found
...
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@k8s-nfs nginx-1.24.0]# make
make -f objs/Makefile
make[1]: 进入目录“/usr/local/nginx-1.24.0”
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='"/etc/fdfs/mod_fastdfs.conf"' -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/nginx.o \
src/core/nginx.c
...
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
-e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
-e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
-e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
< man/nginx.8 > objs/nginx.8
make[1]: 离开目录“/usr/local/nginx-1.24.0”
[root@k8s-nfs nginx-1.24.0]# make install
make -f objs/Makefile install
make[1]: 进入目录“/usr/local/nginx-1.24.0”
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' \
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
make[1]: 离开目录“/usr/local/nginx-1.24.0”
[root@k8s-nfs nginx-1.24.0]# /usr/local/nginx/sbin/nginx -V 2>&1 | grep with-http_image_filter_module
configure arguments: --with-http_image_filter_module --add-module=/usr/local/fastdfs-nginx-module-1.24/src --with-cc-opt=-I/usr/local/libgd-2.3.3/include --with-ld-opt=-L/usr/local/libgd-2.3.3/lib
[root@k8s-nfs nginx-1.24.0]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
[root@k8s-nfs nginx-1.24.0]# nginx -V
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --with-http_image_filter_module --add-module=/usr/local/fastdfs-nginx-module-1.24/src --with-cc-opt=-I/usr/local/libgd-2.3.3/include --with-ld-opt=-L/usr/local/libgd-2.3.3/lib
[root@k8s-nfs nginx-1.24.0]#21. 配置fastdfs-nginx-module插件
[root@k8s-nfs nginx-1.24.0]# cd /usr/local/fastdfs-nginx-module-1.24/src/
[root@k8s-nfs src]# pwd
/usr/local/fastdfs-nginx-module-1.24/src
[root@k8s-nfs src]# ls -lhra
总用量 84K
-rw-rw-r--. 1 root root 28K 12月 5 2023 ngx_http_fastdfs_module.c
-rw-rw-r--. 1 root root 3.7K 12月 5 2023 mod_fastdfs.conf
-rw-rw-r--. 1 root root 1.1K 12月 5 2023 config
-rw-rw-r--. 1 root root 4.0K 12月 5 2023 common.h
-rw-rw-r--. 1 root root 43K 12月 5 2023 common.c
drwxrwxr-x. 3 root root 80 12月 5 2023 ..
drwxrwxr-x. 2 root root 109 12月 5 2023 .
[root@k8s-nfs src]#
[root@k8s-nfs src]# cp ./mod_fastdfs.conf /etc/fdfs/
[root@k8s-nfs src]# cd /etc/fdfs/
[root@k8s-nfs fdfs]# ls -lhra
总用量 88K
-rw-r--r--. 1 root root 9.9K 2月 28 10:25 tracker.conf
-rw-r--r--. 1 root root 6 2月 28 13:57 test.txt
-rw-r--r--. 1 root root 874 2月 28 10:10 storage_ids.conf
-rw-r--r--. 1 root root 11K 2月 28 13:53 storage.conf
-rw-r--r--. 1 root root 3.7K 2月 28 14:49 mod_fastdfs.conf
-rw-r--r--. 1 root root 31K 2月 28 10:19 mime.types
-rw-r--r--. 1 root root 965 2月 28 10:18 http.conf
-rw-r--r--. 1 root root 2.3K 2月 28 13:56 client.conf
drwxr-xr-x. 80 root root 8.0K 2月 28 10:10 ..
drwxr-xr-x. 2 root root 164 2月 28 14:49 .
[root@k8s-nfs fdfs]# vi mod_fastdfs.conf
# 需要修改的内容如下:
base_path=/opt/fastdfs/storage
tracker_server=192.168.19.124:22122 #tracker_server地址
url_have_group_name = true
store_path0=/opt/fastdfs/storage/files # 与storage的store_path0值保持一致22. 配置nginx
这里nginx监听80端口
[root@k8s-nfs fdfs]# cd /usr/local/nginx
[root@k8s-nfs nginx]# pwd
/usr/local/nginx
[root@k8s-nfs nginx]# ls -lhra
总用量 4.0K
drwxr-xr-x. 2 root root 19 2月 28 14:43 sbin
drwxr-xr-x. 2 root root 6 2月 28 14:43 logs
drwxr-xr-x. 2 root root 40 2月 28 14:43 html
drwxr-xr-x. 2 root root 4.0K 2月 28 14:43 conf
drwxr-xr-x. 18 root root 275 2月 28 14:43 ..
drwxr-xr-x. 6 root root 54 2月 28 14:43 .
[root@k8s-nfs nginx]# vi ./conf/nginx.conf
# 修改location内容如下:
location ~ /group[1-3]/M00 {
root /opt/fastdfs/storage/files/data;
if ($arg_attname) {
add_header Content-Disposition "attachment; filename=\"$arg_attname\"";
}
ngx_fastdfs_module;
}
[root@k8s-nfs nginx]# ./sbin/nginx -c ./conf/nginx.conf
[root@k8s-nfs nginx]# netstat -nelp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 806556 22886/nginx: master
[root@k8s-nfs nginx]#23. 将nginx添加为系统服务
创建nginx系统服务: /etc/systemd/system/nginx.service 内容如下:
[Unit]
Description=Nginx Application (CompileBy=zhangyl07)
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target启用nginx服务, trackerd服务, storaged服务
systemctl daemon-reload
systemctl enable nginx.service
systemctl start nginx.service
systemctl restart fdfs_trackerd.service
systemctl restart fdfs_storaged.service24. 使用nginx下载fastdfs存储的文件
# 使用第13步保留的信息
group_name=group1, remote_filename=M00/00/00/wKgTfGfBUICAGC2pAAAABjY6MCA564.txt
# 结合nginx的运行端口和服务器ip, 拼接为实际下载地址:
http://192.168.19.124/group1/M00/00/00/wKgTfGfBUICAGC2pAAAABjY6MCA564.txt
# 使用浏览器访问此地址可以获取到原文件内容: hello25. 开启防盗链
[root@k8s-nfs nginx]# cd /etc/fdfs/
[root@k8s-nfs fdfs]# pwd
/etc/fdfs
[root@k8s-nfs fdfs]# ls -lhra
总用量 88K
-rw-r--r--. 1 root root 9.9K 2月 28 10:25 tracker.conf
-rw-r--r--. 1 root root 6 2月 28 13:57 test.txt
-rw-r--r--. 1 root root 874 2月 28 10:10 storage_ids.conf
-rw-r--r--. 1 root root 11K 2月 28 13:53 storage.conf
-rw-r--r--. 1 root root 3.7K 2月 28 14:57 mod_fastdfs.conf
-rw-r--r--. 1 root root 31K 2月 28 10:19 mime.types
-rw-r--r--. 1 root root 965 2月 28 10:18 http.conf
-rw-r--r--. 1 root root 2.3K 2月 28 13:56 client.conf
drwxr-xr-x. 80 root root 8.0K 2月 28 10:10 ..
drwxr-xr-x. 2 root root 164 2月 28 14:57 .
[root@k8s-nfs fdfs]# vi http.conf
# 需要修改的内容如下:
http.anti_steal.check_token = true #启用token校验
http.anti_steal.token_ttl = 600 #token有效期,单位秒
http.anti_steal.secret_key = smartsmartsmart #token加解密秘钥,与客户端保持一致
http.anti_steal.token_check_fail = /opt/fastdfs/403.txt #当token过期时重定向到哪里重启tracker,storage和nginx
[root@k8s-nfs fdfs]# systemctl restart fdfs_trackerd.service
[root@k8s-nfs fdfs]# systemctl restart fdfs_storaged.service
[root@k8s-nfs fdfs]# systemctl restart nginx.service26. 配置缩略图
示例:
原始图片:
http://192.168.234.61/group1/M00/00/01/wKjqPWj3SCyAE_FHAAB9W2_n6AQ148.png?token=2057d860673607303f1dcbdad20bb464&ts=1761092547
缩略图:
http://192.168.234.61/group1/M00/00/01/wKjqPWj3SCyAE_FHAAB9W2_n6AQ148.png_100x100?token=2057d860673607303f1dcbdad20bb464&ts=1761092547
# 编辑nginx配置文件
[root@k8s-nfs fdfs]# cd /usr/local/nginx/conf
[root@k8s-nfs fdfs]# vi nginx.conf
# 修改location内容为:
# FastDFS缩略图处理
location ~* ^(/group1/M00/.+)\.(jpg|jpeg|png|gif)_(\d+)x(\d+)$ {
# 捕获组:
# $1 -> /group1/M00/.../file_name_without_ext
# $2 -> ext (jpg/png/...)
# $3 -> width
# $4 -> height
set $orig_file $1.$2;
set $width $3;
set $height $4;
# 取原始二进制图片(禁用上游gzip, 避免压缩流导致 image_filter 识别失败)
proxy_set_header Accept "image/*";
proxy_set_header Accept-Encoding "";
proxy_set_header Host $proxy_host;
# 保留查询参数(token, ts等)
proxy_pass http://127.0.0.1$orig_file$is_args$args;
# 可调的 proxy 缓冲,防止小文件被分片收取影响处理
proxy_buffering on;
proxy_buffers 8 16k;
proxy_busy_buffers_size 32k;
# 代理到上游后的响应由 image_filter 处理
image_filter resize $width $height;
image_filter_jpeg_quality 95;
image_filter_buffer 50M;
}
# FastDFS 原始图片处理
location /group1/M00 {
root /opt/fastdfs/storage/data;
if ($arg_attname) {
add_header Content-Disposition "attachment; filename=\"$arg_attname\"";
}
ngx_fastdfs_module;
}FastDFS客户端
1. 编译客户端源码
仓库地址 https://github.com/happyfish100/fastdfs-client-java
注意: 作者余庆不太愿意上传到maven仓库, 无法直接拉去, 只能源码编译
# 修改pom文件中的jdk版本为目前正在使用的版本, 然后编译打包
mvn clean install2. 在项目中引入
具体版本以客户端编译时pom文件中的版本为准, 这里为1.32-SNAPSHOT
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.32-SNAPSHOT</version>
</dependency>3. 配置客户端
在application.yml配置文件中添加配置如下
# fastdfs配置
fastdfs:
connectTimeout: 30
networkTimeout: 60
charset: UTF-8
trackerHttpPort: 8080
antiStealToken: true
secretKey: smartsmartsmart
trackerServer: 192.168.19.124:221224. 编写FastDFS配置类和工具类
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
/**
* fastdfs配置类
* 从nacos配置中心动态刷新
*
* @author zhangyl07
*/
@Data
@Component
@RefreshScope
@ConfigurationProperties(prefix = "fastdfs")
public class FastDFSConfig {
/**
* 连接超时时间(秒)
* eg: 30
*/
private int connectTimeout;
/**
* 网络超时时间(秒)
* eg: 60
*/
private int networkTimeout;
/**
* 字符集
* eg: UTF-8
*/
private String charset;
/**
* tracker的HTTP端口
* eg: 8080
*/
private int trackerHttpPort;
/**
* 是否启用防盗链
* eg: true
*/
private boolean antiStealToken;
/**
* fastdfs的token加密秘钥, 与fastdfs服务端保持一致
* eg: smartsmartsmart
*/
private String secretKey;
/**
* tracker 服务器地址
* eg: 192.168.19.124:22122
*/
private String trackerServer;
}import com.ctsi.config.FastDFSConfig;
import com.ctsi.enums.ErrorCodeEnum;
import com.ctsi.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.csource.common.NameValuePair;
import org.apache.commons.io.FilenameUtils;
import org.csource.fastdfs.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
/**
* fastDFS客户端工具类
*
* @author zhangyl07
* @date 2025-03-07
*/
@Slf4j
@Component
@ConditionalOnBean(FastDFSConfig.class)
public class FastDFSClientUtil {
private final FastDFSConfig fastDFSConfig;
private final StorageClient storageClient;
/**
* 构造方法注入fastDFS配置, 并初始化fastDFS客户端
*/
@Autowired
public FastDFSClientUtil(FastDFSConfig fastDFSConfig) {
this.fastDFSConfig = fastDFSConfig;
try {
// 初始化客户端配置
// 设置连接超时(单位:毫秒)
ClientGlobal.g_connect_timeout = fastDFSConfig.getConnectTimeout() * 1000;
// 设置网络超时(单位:毫秒)
ClientGlobal.g_network_timeout = fastDFSConfig.getNetworkTimeout() * 1000;
// 设置字符集
ClientGlobal.g_charset = fastDFSConfig.getCharset();
// http相关配置
System.setProperty("fastdfs.http.tracker_http_port", String.valueOf(fastDFSConfig.getTrackerHttpPort()));
System.setProperty("fastdfs.http.anti_steal_token", String.valueOf(fastDFSConfig.isAntiStealToken()));
System.setProperty("fastdfs.http.secret_key", fastDFSConfig.getSecretKey());
// 使用 tracker 服务器地址进行初始化
ClientGlobal.initByTrackers(fastDFSConfig.getTrackerServer());
// 创建 TrackerClient 和 TrackerServer
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getTrackerServer();
// 创建 StorageClient
storageClient = new StorageClient(trackerServer);
} catch (Exception e) {
log.info("fastdfs初始化失败...");
throw new RuntimeException(e);
}
}
/**
* 上传文件到fastdfs服务器
*/
public String uploadToFastDFS(MultipartFile multipartFile) {
try {
// 取文件扩展名
String extraFileName = FilenameUtils.getExtension(multipartFile.getOriginalFilename());
NameValuePair[] meta = new NameValuePair[]{new NameValuePair("orig_filename", multipartFile.getOriginalFilename())};
String[] uploadResults = storageClient.upload_file(multipartFile.getBytes(), extraFileName, meta);
return uploadResults[0] + "/" + uploadResults[1];
} catch (Exception e) {
log.info("上传文件到fastdfs失败, 异常信息: {}, 详细信息:", e.getMessage(), e);
throw new BusinessException(ErrorCodeEnum.UPLOAD_FAST_DFS_ERROR);
}
}
/**
* 生成带Token的文件下载地址
*
* @param filePath 文件保存路径
* @return 带Token的文件下载地址
*/
public String getDownloadUrl(String filePath, String origFilename) {
String groupName = filePath.substring(0, filePath.indexOf("/"));
String remoteFileName = filePath.substring(filePath.indexOf("/") + 1);
try {
int lts = (int) (System.currentTimeMillis() / 1000);
String token = ProtoCommon.getToken(remoteFileName, lts, fastDFSConfig.getSecretKey());
// 拼接下载地址
return groupName + "/" + remoteFileName + "?token=" + token + "&ts=" + lts + "&attname=" + origFilename;
} catch (Exception e) {
log.error("生成文件下载地址失败, 异常信息: {}, 详细信息: ", e.getMessage(), e);
throw new BusinessException(ErrorCodeEnum.CREATE_FILE_DOWNLOAD_URL_ERROR);
}
}
/**
* 删除fastdfs存储的文件
*
* @param filePath 文件保存路径
*/
public void deleteFileOnFastDFS(String filePath) {
try {
String groupName = filePath.substring(0, filePath.indexOf("/"));
String remoteFileName = filePath.substring(filePath.indexOf("/"));
// 删除文件
storageClient.delete_file(groupName, remoteFileName);
} catch (Exception e) {
log.info("删除fastdfs文件失败, 异常信息: {}, 详细信息:", e.getMessage(), e);
throw new BusinessException(ErrorCodeEnum.DELETE_FAST_DFS_ERROR);
}
}
}5. 调用工具类, 上传文件后获取下载地址如下
Group: group1
Remote File Name: M00/00/00/wKgTfGfBaySADZyPAAAACoRc_8w705.txt
Download URL: http://192.168.19.124/group1/M00/00/00/wKgTfGfBaySADZyPAAAACoRc_8w705.txt?token=a8f91f4dcaf4edd138919b853d4ff4de&ts=1740729124&attname=hello.txt完整路径可以访问到文件内容 不携带token无法访问文件 当token过期时, 会响应配置的403