FastDFS NET示例

風靈使發表於2018-04-03

Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using FastDFS.Client;
using System.Drawing;

namespace FastDFS
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            //===========================初始化========================================
            var trackerIPs = new List<IPEndPoint>();
            var endPoint = new IPEndPoint(IPAddress.Parse("193.112.***.225"), 22122);
            trackerIPs.Add(endPoint);
            ConnectionManager.Initialize(trackerIPs);
            var node = FastDFSClient.GetStorageNode("group1");
            //===========================上傳檔案=====================================
            byte[] content = null;
            if (File.Exists(@"E:\JSON.pdf"))
            {
                FileStream streamUpload = new FileStream(@"E:\JSON.pdf", FileMode.Open);
                using (BinaryReader reader = new BinaryReader(streamUpload))
                {
                    content = reader.ReadBytes((int)streamUpload.Length);
                }
            }
            //主檔案
            string fileName = FastDFSClient.UploadFile(node, content, "pdf");

            //UploadFileByName
            //string fileName1 = FastDFSClient.UploadFileByName(node, @"D:\計算機基礎.doc");

            //從檔案
            string slavefileName = FastDFSClient.UploadSlaveFile("group1", content, fileName, "-part1", "pdf");

            //===========================批量上傳檔案=====================================
            string[] _FileEntries = Directory.GetFiles(@"E:\圖片\png", "*.jpg");
            DateTime start = DateTime.Now;
            foreach (string file in _FileEntries)
            {
                string name = Path.GetFileName(file);
                content = null;
                FileStream streamUpload = new FileStream(file, FileMode.Open);
                using (BinaryReader reader = new BinaryReader(streamUpload))
                {
                    content = reader.ReadBytes((int)streamUpload.Length);
                }
                //string fileName = FastDFSClient.UploadAppenderFile(node, content, "mdb");
                fileName = FastDFSClient.UploadFile(node, content, "jpg");
            }
            DateTime end = DateTime.Now;
            TimeSpan consume = ((TimeSpan)(end - start));
            double consumeSeconds = Math.Ceiling(consume.TotalSeconds);
            //===========================查詢檔案=======================================
            fileName = "M00/00/00/rBAAC1rA21SAULJwAAoTzEG_OEk868.pdf";

            //http://193.112.***.225/group1/M00/00/00/rBAAC1rA21SAULJwAAoTzEG_OEk868.pdf
            //rBAAC1rA21SAULJwAAoTzEG_OEk868 - part1.pdf
            //rBAAC1rA26iAWFB7AACXK3PhcOQ326.jpg
            //rBAAC1rA27CAHqf4AAAUBnpdd - Q710.jpg
            //rBAAC1rA28WAUmITAABKJd12G1A489.jpg

            var fileInfo = FastDFSClient.GetFileInfo(node, fileName);
            Console.WriteLine("FileName:{0}", fileName);
            Console.WriteLine("FileSize:{0}", fileInfo.FileSize);
            Console.WriteLine("CreateTime:{0}", fileInfo.CreateTime);
            Console.WriteLine("Crc32:{0}", fileInfo.Crc32);
            //==========================追加檔案=======================================
            FastDFSClient.AppendFile("group1", fileName, content);
            FastDFSClient.AppendFile("group1", fileName, content);

            //===========================下載檔案====================================

            //===========================RemoveFile=======================================
            FastDFSClient.RemoveFile("group1", fileName);

            //===========================Http測試,流讀取=======================================
            string url = "http://img13.360buyimg.com/da/g5/M02/0D/16/rBEDik_nOJ0IAAAAAAA_cbJCY-UAACrRgMhVLEAAD-J352.jpg";
            System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
            System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)req.GetResponse();
            Image myImage = Image.FromStream(res.GetResponseStream());
            myImage.Save("c:\\fast.jpg");//儲存 
            //===========================Http測試,直接下載=======================================
            using (WebClient web = new WebClient())
            {
                web.DownloadFile("http://193.112.***.225/group1/M00/00/00/rBAAC1rA21SAULJwAAoTzEG_OEk868.pdf", "E:\\abc.pdf");
                web.DownloadFile("http://193.112.***.225/group1/M00/00/00/rBAAC1rA26iAWFB7AACXK3PhcOQ326.jpg", "E:\\abc.jpg");
            }
            //===========================防盜鏈請求=======================================
            start = new DateTime(1970, 1, 1);
            end = DateTime.Now;
            consume = (TimeSpan)(end - start);
            int ts = (int)(consume.TotalSeconds);
            string pwd = FastDFS.Client.Util.GetToken("M00/03/81/wKhR6VAh0sfyH0AxAABYAMjfFsM301-part1.doc", ts, "FastDFS1qaz2wsxsipsd");
            string anti_steel_url = "http://192.168.81.233/M00/03/81/wKhR6VAh0sfyH0AxAABYAMjfFsM301-part1.doc?token=" + pwd + "&ts=" + ts;
            string url1 = "http://192.168.81.233/M00/01/E0/wKhR6VANJBiInHb5AAClVeZnxGg341.pdf";
            using (WebClient web = new WebClient())
            {
                web.DownloadFile(anti_steel_url, "C:\\salve.doc");
            }

            Console.WriteLine("完成");
            Console.Read();
        }
    }
}

相關文章