fastdfs安装
1.编译安装 libevent-1.4.14b-stable
$./configure
$ make
# make install
2.FastDFS v3.05
$./make.sh
#./make.sh install
FastDFS安装使用实战二(配置篇)
1.
cd /etc/fdfs/
修改tracker.conf文件,修改如下
2.
base_path=/home/liguojun/fastdfs_tracker
3.
http.server_port=8080 -> http.server_port=8090
(http端口)
4.
##include http.conf -> #include http.conf
(http支持)
5.
reserved_storage_space = 4GB -> reserved_storage_space = 1GB
6.
port=22122
(tracker server对storage server供服务的端口)
7.
cd /usr/local/bin/
8.
fdfs_trackerd /etc/fdfs/tracker.conf
(启动tracker服务器)
9.
cat /home/liguojun/fastdfs_tracker/logs/trackerd.log
查看日志
配置及启动Storage Server
1.
cd /etc/fdfs
修改storage.conf文件,修改如下
2.
base_path=/home/yuqing/fastdfs -> /home/liguojun/fastdfs_storge
#文件的存储位置,在一台storage server上可以指定多个存储位置
#
store_path0=/home/yuqing/fastdfs -> store_path0=/home/liguojun/fastdfs_storge
3.
group_name=group1
4.
tracker_server=192.168.209.121:22122 -> tracker_server=10.0.2.15:22122
5.
##include http.conf ->#include http.conf
6.
cd /usr/local/bin/
7.
fdfs_storaged /etc/fdfs/storage.conf
8.
cat /home/liguojun/fastdfs_storge/logs/storaged.log
查看日志
FastDFS安装使用实战三(使用篇)
1.
cd /etc/fdfs
修改client.conf文件,修改如下
2.
base_path=/home/yuqing/fastdfs-> base_path=/home/soar/fastdfs_tracker
3.
tracker_server=192.168.209.121:22122 -> tracker_server=10.0.2.15:22122
4.
http.tracker_server_port=8080 ->http.tracker_server_port=8090
5.
##include http.conf ->#include http.conf
6.
cd /usr/local/bin/
7.
fdfs_test /etc/fdfs/conf/client.conf upload /home/liguojun/a.txt
上传:
package com.jd.fastdfs.test;
import java.io.File;
import java.io.FileInputStream;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.ServerInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
/**
* 测试上传
* @author gary
*
*/
public class TestUpload {
public static void main(String[] args) throws Exception{
String classPath = new File(TestUpload.class.getResource("/").getFile()).getCanonicalPath();
String configFilePath = classPath + File.separator + "fdfs_client.conf";
System.out.println("配置文件:" + configFilePath);
ClientGlobal.init(configFilePath);
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
NameValuePair[] meta_list = new NameValuePair[3];
meta_list[0] = new NameValuePair("width", "120");
meta_list[1] = new NameValuePair("heigth", "120");
meta_list[2] = new NameValuePair("author", "gary");
// byte[] file_buff = "F:\\pic.jpg".getBytes(ClientGlobal.g_charset);
File file = new File("/home/liguojun/图片/image/head.jpg");
FileInputStream fis = new FileInputStream(file);
byte[] file_buff = null;
if(fis != null){
int len = fis.available();
file_buff = new byte[len];
fis.read(file_buff);
}
System.out.println("file length: " + file_buff.length);
String group_name = null;
StorageServer[] storageServers = trackerClient.getStoreStorages(trackerServer, group_name);
if (storageServers == null) {
System.err.println("get store storage servers fail, error code: " + storageClient.getErrorCode());
}else{
System.err.println("store storage servers count: " + storageServers.length);
for (int k = 0; k < storageServers.length; k++){
System.err.println(k + 1 + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());
}
System.err.println("");
}
long startTime = System.currentTimeMillis();
String[] results = storageClient.upload_file(file_buff, "jpg", meta_list);
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (results == null){
System.err.println("upload file fail, error code: " + storageClient.getErrorCode());
return;
}
group_name = results[0];
String remote_filename = results[1];
System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
System.err.println(storageClient.get_file_info(group_name, remote_filename));
ServerInfo[] servers = trackerClient.getFetchStorages(trackerServer, group_name, remote_filename);
if (servers == null){
System.err.println("get storage servers fail, error code: " + trackerClient.getErrorCode());
} else {
System.err.println("storage servers count: " + servers.length);
for (int k = 0; k < servers.length; k++){
System.err.println(k + 1 + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
}
System.err.println("");
}
}
}
下载
package com.jd.fastdfs.test;
import java.io.File;
import java.io.FileOutputStream;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
/**
* 获取文件信息
* @author gary
*
*/
public class TestGet {
public static void main(String[] args) throws Exception {
String classPath = new File(TestGet.class.getResource("/").getFile()).getCanonicalPath();
String configFilePath = classPath + File.separator + "fdfs_client.conf";
ClientGlobal.init(configFilePath);
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String group_name = "group1";
String remote_filename = "M00/00/00/Cgr5w09wUsmIsSZvAABvCbsGaKM946.jpg";
FileInfo fi = storageClient.get_file_info(group_name, remote_filename);
byte[] b = storageClient.download_file(group_name, remote_filename);
FileOutputStream os = new FileOutputStream( "/home/liguojun/filename");
os.write(b);
os.close();
System.out.println("--->"+fi.getCreateTimestamp());
String sourceIpAddr = fi.getSourceIpAddr();
long size = fi.getFileSize();
System.out.println("ip:" + sourceIpAddr + ",size:" + size);
}
}
删除
package com.jd.fastdfs.test;
import java.io.File;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
/**
* 删除文件
* @author gary
*
*/
public class TestDelete {
public static void main(String[] args) throws Exception {
String classPath = new File(TestDelete.class.getResource("/").getFile()).getCanonicalPath();
String configFilePath = classPath + File.separator + "fdfs_client.conf";
ClientGlobal.init(configFilePath);
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String group_name = "group1";
String remote_filename = "M00/00/00/Cgr5w09wUMvgS6TXAABvCbsGaKM547.jpg";
storageClient.delete_file(group_name, remote_filename);
System.out.println("删除成功");
}
}