首页 技术 正文
技术 2022年11月10日
0 收藏 491 点赞 4,709 浏览 4719 个字

1. 命令行api介绍

FastDFS提供了可用于运维测试的命令行api,下面进行介绍:

1.1 fastdfs服务管理

  1. tracker进程服务管理脚本
 /etc/init.d/fdfs_trackerd [start|stop|restart]
  1. 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处于下线不可用状态
FastDFS api介绍
FastDFS api介绍

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

此时可以通过浏览器访问刚刚上传的文件

FastDFS api介绍

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
  1. 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 = FastDFS1234567890tracker_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();
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898