企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 1. 命令行api介绍 FastDFS提供了可用于运维测试的命令行api,下面进行介绍: ### 1.1 fastdfs服务管理 1. tracker进程服务管理脚本 ~~~ /etc/init.d/fdfs_trackerd [start|stop|restart] ~~~ 2. storage进程服务管理脚本 ~~~ /etc/init.d/fdfs_storaged [start|stop|restart] ~~~ ### 1.2 集群信息 用于查看集群信息,包括:tracker信息、storage信息、group信息以及集群运行状况;命令可以在任意一台storage 节点上执行 ~~~ /usr/bin/fdfs_monitor /etc/fdfs/storage.conf ~~~ 如下图,集群由两个tracker,两个group。其中group2中的storage1处于下线不可用状态 ![](https://box.kancloud.cn/dcc883be70617983f3e7d593e11991a0_1901x619.png) ![](https://box.kancloud.cn/cda7cc8af2bec8d48edc07acff4fb1e3_1540x612.png) ### 1.3 删除storage节点 ~~~ /usr/bin/fdfs_monitor /etc/fdfs/storage.conf delete group2 192.168.56.11 ~~~ ### 1.4 文件上传 ~~~ /usr/bin/fdfs_upload_file /etc/fdfs/client.conf [文件名] ~~~ 例如上传nginx-1.14.0.tar.gz文件 ~~~ /usr/bin/fdfs_upload_file /etc/fdfs/client.conf ./nginx-1.14.0.tar.gz ~~~ 返回包含文件在fastdfs存储路径(store_path0下)信息和存储文件名信息 ~~~ group1/M00/00/00/wKg4CltFU7WAAzX-AA-B0CmJB5A.tar.gz ~~~ 此时可以通过浏览器访问刚刚上传的文件 ![](https://box.kancloud.cn/a90d2f4f1162e57ff0501f52511678a3_1831x737.png) ### 1.5 文件下载 ~~~ /usr/bin/fdfs_download_file <config_file> <file_id> [local_filename] [<download_offset> <download_bytes>] ~~~ 例如,下载fastdfs中的nginx(wKg4CltFWAKADvr_AA-B0CmJB5A.tar.gz )到本地,指定下载的文件名文件名为nginxdownload.tar.gz ~~~ /usr/bin/fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKg4CltFWAKADvr_AA-B0CmJB5A.tar.gz nginxdownload.tar.gz ~~~ ### 1.6 文件删除 ~~~ /usr/bin/fdfs_delete_file <config_file> <file_id> ~~~ 例如删除上边上传的nginx ~~~ /usr/bin/fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKg4CltFU7WAAzX-AA-B0CmJB5A.tar.gz ~~~ ## 2. java api 使用 FastDFS提供了良好的java api接口,源码GitHub下载地址:https://github.com/happyfish100/fastdfs-client-java.git 。 引入api的方式有两种: ### 2.1 引入api 1. ant源码构建 ~~~ ant clean package ~~~ 2. maven源码构建 ~~~ mvn clean install ~~~ pom.xml 依赖添加 ~~~ <dependency> <groupId>org.csource</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27-SNAPSHOT</version> </dependency> ~~~ ### 2.2 配置 添加配置文件fdfs_client.conf,增加以下配置 ~~~ connect_timeout = 2 network_timeout = 30 charset = UTF-8 http.tracker_http_port = 80 http.anti_steal_token = no http.secret_key = FastDFS1234567890 tracker_server = 192.168.56.10:22122 tracker_server = 192.168.56.11:22122 ~~~ http.anti_steal_token:配置防盗链,no表示不开启(详细参考FastDFS防盗链一节) http.secret_key:防盗链密码 tracker_server:tracker连接信息,有多少tracker就配置几个 ### 2.3 api 简单使用 单例模式,无线程池;读取classpath下fdfs_client.conf配置文件 ~~~ package com.aixin.tuna.fdfs; import org.csource.fastdfs.*; import java.sql.Connection; import java.sql.SQLException; /** * Created by dailin on 2018/7/11. */ public class FdfsUtil { static class Nested { private static TrackerServer trackerServer =null; private static StorageServer storageServer = null; private static StorageClient storageClient = null; static { try { ClientGlobal.init("fdfs_client.conf"); TrackerClient tracker = new TrackerClient(); trackerServer = tracker.getConnection(); storageClient = new StorageClient(trackerServer, storageServer); }catch (Exception e) { e.printStackTrace(); } } } //获取单例 public static StorageClient getStorageClient() { return Nested.storageClient; } } ~~~ 测试: ~~~ package fdfs; import java.io.FileOutputStream; import java.io.OutputStream; import com.aixin.tuna.fdfs.FdfsUtil; import org.csource.common.NameValuePair; import org.csource.fastdfs.FileInfo; import org.csource.fastdfs.StorageClient; import org.junit.Before; import org.junit.Test; public class TestFastDfs { private StorageClient storageClient; @Before public void init() { this.storageClient = FdfsUtil.getStorageClient(); } /** * 测试文件上传,得到上传后的文件路径及文件名称 */ @Test public void testUpload() { try { String local_filename = "C:\\Users\\1\\Desktop\\1.png"; //本地文件路径 String[] file = storageClient.upload_file(local_filename, "png", null); System.out.println(file.length); System.out.println("组名:" + file[0]); System.out.println("路径: " + file[1]); } catch (Exception e) { e.printStackTrace(); } } /** * 测试文件下载,根据文件所在group和文件路径信息访问文件 */ @Test public void testDownload() { try { String groupName = "group2"; String filePath = "M00/00/00/wKg4C1tFegGAK7WZAADdeFFxlXA481.png"; byte[] bytes = storageClient.download_file(groupName, filePath); String storePath = "C:\\Users\\1\\Desktop\\download1.png"; OutputStream out = new FileOutputStream(storePath); out.write(bytes); out.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 获取文件基本信息,根据文件所在group和文件路径信息访问文件 */ @Test public void testGetFileInfo() { try { String groupName = "group2"; String filePath = "M00/00/00/wKg4C1tFegGAK7WZAADdeFFxlXA481.png"; FileInfo file = storageClient.get_file_info(groupName, filePath); System.out.println("source_ip:" + file.getSourceIpAddr()); System.out.println("file_size:" + file.getFileSize()); System.out.println("upload_time:" + file.getCreateTimestamp()); System.out.println(file.toString()); } catch (Exception e) { e.printStackTrace(); } } @Test public void testGetFileMatedata() { try { String groupName = "group2"; String filePath = "M00/00/00/wKg4C1tFmmqATh8jAA-B0CmJB5A.tar.gz"; NameValuePair nvps[] = storageClient.get_metadata(groupName, filePath); if (null != nvps && nvps.length > 0) { for (NameValuePair nvp : nvps) { System.out.println(nvp.getName() + ":" + nvp.getValue()); } } } catch (Exception e) { e.printStackTrace(); } } /** * 删除文件,根据文件所在group和文件路径信息访问文件 */ @Test public void testDelete() { try { String groupName = "group2"; String filePath = "M00/00/00/wKg4C1tFlT6ASyqKAADdeFFxlXA080.png"; int i = storageClient.delete_file(groupName, filePath); System.out.println(i == 0 ? "删除成功" : "删除失败:" + i); } catch (Exception e) { e.printStackTrace(); } } } ~~~