主機IP | 名稱 |
---|---|
192.168.1.1 | trackerd |
192.168.1.2 | storaged |
./make.sh ./make.sh install mkdir -p /usr/lib64 install -m 755 libfastcommon.so /usr/lib64 mkdir -p /usr/include/fastcommon
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_bits.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 process_ctrl.h fast_mblock.h connection_pool.h /usr/include/fastcommon
ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
cd /usr/local/fast/FastDFS ./make.sh ./make.sh install
mkdir -p /usr/bin mkdir -p /etc/fdfs cp -f fdfs_trackerd /usr/bin if [ ! -f /etc/fdfs/tracker.conf.sample ]; then cp -f ../conf/tracker.conf /etc/fdfs/tracker.conf.sample; fi mkdir -p /usr/bin mkdir -p /etc/fdfs cp -f fdfs_storaged /usr/bin if [ ! -f /etc/fdfs/storage.conf.sample ]; then cp -f ../conf/storage.conf /etc/fdfs/storage.conf.sample; fi mkdir -p /usr/bin mkdir -p /etc/fdfs mkdir -p /usr/lib64 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 /usr/bin if [ 0 -eq 1 ]; then cp -f libfdfsclient.a /usr/lib64; fi if [ 1 -eq 1 ]; then cp -f libfdfsclient.so /usr/lib64; 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 ../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.sample ]; then cp -f ../conf/client.conf /etc/fdfs/client.conf.sample; fi
ls /etc/init.d/ | grep fdfs fdfs_storaged fdfs_trackerd
ls /etc/fdfs/ client.conf.sample storage.conf.sample tracker.conf.sample
ls /usr/bin/ | grep fdfs fdfs_appender_test fdfs_appender_test1 fdfs_append_file fdfs_crc32 fdfs_delete_file fdfs_download_file fdfs_file_info fdfs_monitor fdfs_storaged fdfs_test fdfs_test1 fdfs_trackerd fdfs_upload_appender fdfs_upload_file
cd /etc/fdfs/ cp tracker.conf.sample tracker.conf vim /etc/fdfs/tracker.conf
# the base path to store data and log files base_path=/fastdfs/tracker
cd /fastdfs/tracker/ && ll total 0
cd /fastdfs/tracker/ && ll total 8 drwxr-xr-x. 2 root root 4096 Dec 14 20:48 data drwxr-xr-x. 2 root root 4096 Dec 14 20:48 logs
base_path=/fastdfs/storage store_path0=/fastdfs/storage tracker_server=192.168.1.1:22122 http.server_port=80
base_path=/fastdfs/tracker tracker_server=**192.168.1.1**:22122
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/FastDFS/libfastcommon-master.zip
cd /fastdfs/storage/data/00/00/ && ll total 1 -rw-r--r--. 1 root root 102378 Dec 14 21:00 2-YyVlhRQlaAZLlgAAGP6hUWM6I411.zip
tar -zxvf FastDFS/fastdfs-nginx-module_v1.16.tar.gz -C /usr/local/fast/ cd /usr/local/fast/fastdfs-nginx-module/src/
修改前: CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/" 修改後: CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
connect_timeout=10 tracker_server=192.168.1.1:22122 url_have_group_name = true store_path0=/fastdfs/storage
listen 80; server_name localhost; location ~/group([0-9])/M00 { ngx_fastdfs_module; }
package vip.xubin.utils;import org.apache.commons.lang3.StringUtils;import org.apache.log4j.Logger;import org.csource.common.NameValuePair;import org.csource.fastdfs.*;import java.io.*;/** * FastDFS 工具類 */public class FastDFSClientUtils { private static final String CONF_FILENAME = Thread.currentThread().getContextClassLoader().getResource("fastdfs_client.conf").getPath(); private static Logger logger = Logger.getLogger(FastDFSClientUtils.class); private static TrackerClient trackerClient; //加載文件 static { try { ClientGlobal.init(CONF_FILENAME); TrackerGroup trackerGroup = ClientGlobal.g_tracker_group; trackerClient = new TrackerClient(trackerGroup); } catch (Exception e) { logger.error(e); } } /** * 上傳 * @param file 文件 * @param path 路徑 * @return * 上傳成功返回id,失敗返回null */ public static String upload(File file, String path) { TrackerServer trackerServer = null; StorageServer storageServer = null; StorageClient1 storageClient1 = null; FileInputStream fis = null; try { NameValuePair[] meta_list = null; // new NameValuePair[0]; fis = new FileInputStream(file); byte[] file_buff = null; if (fis != null) { int len = fis.available(); file_buff = new byte[len]; fis.read(file_buff); } trackerServer = trackerClient.getConnection(); if (trackerServer == null) { logger.error("getConnection return null"); } storageServer = trackerClient.getStoreStorage(trackerServer); storageClient1 = new StorageClient1(trackerServer, storageServer); String fileid = storageClient1.upload_file1(file_buff, getFileExt(path), meta_list); return fileid; } catch (Exception ex) { logger.error(ex); return null; }finally{ if (fis != null){ try { fis.close(); } catch (IOException e) { logger.error(e); } } if (storageServer != null){ try { storageServer.close(); } catch (IOException e) { e.printStackTrace(); } } if (trackerServer != null){ try { trackerServer.close(); } catch (IOException e) { e.printStackTrace(); } } storageClient1 = null; } } /** * 上傳 * @param data 數據 * @param extName 路徑 * @return * 上傳成功返回id,失敗返回null */ public static String upload(byte[] data, String extName) { TrackerServer trackerServer = null; StorageServer storageServer = null; StorageClient1 storageClient1 = null; try { NameValuePair[] meta_list = null; // new NameValuePair[0]; trackerServer = trackerClient.getConnection(); if (trackerServer == null) { logger.error("getConnection return null"); } storageServer = trackerClient.getStoreStorage(trackerServer); storageClient1 = new StorageClient1(trackerServer, storageServer); String fileid = storageClient1.upload_file1(data, extName, meta_list); return fileid; } catch (Exception ex) { logger.error(ex); return null; }finally{ if (storageServer != null){ try { storageServer.close(); } catch (IOException e) { e.printStackTrace(); } } if (trackerServer != null){ try { trackerServer.close(); } catch (IOException e) { e.printStackTrace(); } } storageClient1 = null; } } /** * 下載 * @param fileId 文件id * @return * 返回InputStream */ public static InputStream download(String groupName, String fileId) { TrackerServer trackerServer = null; StorageServer storageServer = null; StorageClient1 storageClient1 = null; try { trackerServer = trackerClient.getConnection(); if (trackerServer == null) { logger.error("getConnection return null"); } storageServer = trackerClient.getStoreStorage(trackerServer, groupName); storageClient1 = new StorageClient1(trackerServer, storageServer); byte[] bytes = storageClient1.download_file1(fileId); InputStream inputStream = new ByteArrayInputStream(bytes); return inputStream; } catch (Exception ex) { logger.error(ex); return null; } finally { if (storageServer != null){ try { storageServer.close(); } catch (IOException e) { e.printStackTrace(); } } if (trackerServer != null){ try { trackerServer.close(); } catch (IOException e) { e.printStackTrace(); } } storageClient1 = null; } } /** * 刪除 * @param fileId 文件id * @return * 刪除成功返回0,非0則操做失敗,返回錯誤代碼 */ public static int delete(String groupName, String fileId) { TrackerServer trackerServer = null; StorageServer storageServer = null; StorageClient1 storageClient1 = null; try { trackerServer = trackerClient.getConnection(); if (trackerServer == null) { logger.error("getConnection return null"); } storageServer = trackerClient.getStoreStorage(trackerServer, groupName); storageClient1 = new StorageClient1(trackerServer, storageServer); int result = storageClient1.delete_file1(fileId); return result; } catch (Exception ex) { logger.error(ex); return 0; } finally { if (storageServer != null){ try { storageServer.close(); } catch (IOException e) { e.printStackTrace(); } } if (trackerServer != null){ try { trackerServer.close(); } catch (IOException e) { e.printStackTrace(); } } storageClient1 = null; } } /** * 更新文件 * @param oldFileId 舊文件id * @param file 新文件 * @param path 新文件路徑 * @return * 上傳成功返回id,失敗返回null */ public static String modify(String oldGroupName, String oldFileId, File file, String path) { String fileid = null; try { // 先上傳 fileid = upload(file, path); if (fileid == null) { return null; } // 再刪除 int delResult = delete(oldGroupName, oldFileId); if (delResult != 0) { return null; } } catch (Exception ex) { logger.error(ex); return null; } return fileid; } /** * 獲取文件後綴名 * @param fileName * @return * 如:"jpg"、"txt"、"zip" 等 */ private static String getFileExt(String fileName) { if (StringUtils.isBlank(fileName) || !fileName.contains(".")) { return ""; } else { return fileName.substring(fileName.lastIndexOf(".") + 1); } }}
參考