/// <summary> /// 获取文件信息 /// </summary> /// <param name="storageNode">GetStorageNode方法返回的存储节点</param> /// <param name="fileName">文件名</param> /// <returns></returns> public static FDFSFileInfo GetFileInfo(StorageNode storageNode, string fileName) { FDFSRequest storageReqeust = QUERY_FILE_INFO.Instance.GetRequest(storageNode.EndPoint, storageNode.GroupName, fileName); FDFSFileInfo result = new FDFSFileInfo(storageReqeust.GetResponse()); return(result); }
/// <summary> /// 增量下载文件 /// </summary> /// <param name="storageNode">GetStorageNode方法返回的存储节点</param> /// <param name="fileName">文件名</param> /// <param name="offset">从文件起始点的偏移量</param> /// <param name="length">要读取的字节数</param> /// <returns>文件内容</returns> public static byte[] DownloadFile(StorageNode storageNode, string fileName, long offset, long length) { FDFSRequest storageReqeust = DOWNLOAD_FILE.Instance.GetRequest(storageNode.EndPoint, offset, length, storageNode.GroupName, fileName); DOWNLOAD_FILE.Response storageResponse = new DOWNLOAD_FILE.Response(storageReqeust.GetResponse()); return(storageResponse.Content); }
/// <summary> /// 上传可以Append的文件 /// </summary> /// <param name="storageNode">GetStorageNode方法返回的存储节点</param> /// <param name="contentByte">文件内容</param> /// <param name="fileExt">文件扩展名(注意:不包含".")</param> /// <returns>文件名</returns> public static string UploadAppenderFile(StorageNode storageNode, byte[] contentByte, string fileExt) { FDFSRequest storageReqeust = UPLOAD_APPEND_FILE.Instance.GetRequest(storageNode.EndPoint, storageNode.StorePathIndex, contentByte.Length, fileExt, contentByte); UPLOAD_APPEND_FILE.Response storageResponse = new UPLOAD_APPEND_FILE.Response(storageReqeust.GetResponse()); return(storageResponse.FileName); }
/// <summary> /// 上传文件到FastDFS /// </summary> /// <param name="storageNode"></param> /// <param name="contentByte"></param> /// <param name="fileExt"></param> /// <param name="beginDelegate">上传前回调</param> /// <param name="afterDelegate">上传后回调,参数为URL文件名</param> /// <returns>返回短文件名</returns> public static string UploadFile(StorageNode storageNode, byte[] contentByte, string fileExt, System.Action <string> beginDelegate, System.Action <string> afterDelegate) { if (null != beginDelegate) { beginDelegate(fileExt); } FDFSRequest storageReqeust = UPLOAD_FILE.Instance.GetRequest(storageNode.EndPoint, storageNode.StorePathIndex, contentByte.Length, fileExt, contentByte); UPLOAD_FILE.Response storageResponse = new UPLOAD_FILE.Response(storageReqeust.GetResponse()); if (null != afterDelegate) { afterDelegate(GetFormatUrl(storageNode.EndPoint.Address.ToString(), storageNode.GroupName, storageResponse.FileName)); } return(storageResponse.FileName); }
/// <summary> /// 获取存储节点,组名为空返回默认组 /// </summary> /// <param name="groupName">组名</param> /// <returns>存储节点实体类</returns> public static StorageNode GetStorageNode(string groupName = null) { FDFSRequest trackerRequest = QUERY_STORE_WITH_GROUP_ONE.Instance.GetRequest(groupName); QUERY_STORE_WITH_GROUP_ONE.Response trackerResponse = new QUERY_STORE_WITH_GROUP_ONE.Response(trackerRequest.GetResponse()); IPEndPoint storeEndPoint = new IPEndPoint(IPAddress.Parse(trackerResponse.IPStr), trackerResponse.Port); if (groupName == null) { return(DefaultGroup); } StorageNode result = new StorageNode(); result.GroupName = trackerResponse.GroupName; result.EndPoint = storeEndPoint; result.StorePathIndex = trackerResponse.StorePathIndex; return(result); }
/// <summary> /// 当前默认的组,节,卷名称 /// 开发人员可以通过FastDFSClient.GetStorageNode("groupname")去指定自己的组 /// </summary> static FastDFSClient() { List <IPEndPoint> trackerIPs = new List <IPEndPoint>(); TrackerSection trackersSection = ConfigurationManager.GetSection("TrackerSection") as TrackerSection; foreach (tracker item in trackersSection.Trackers) { trackerIPs.Add(new IPEndPoint(IPAddress.Parse(item.Host), item.Port)); } ConnectionManager.Initialize(trackerIPs); try { DefaultGroup = GetStorageNode("tsingda"); } catch (Exception ex) { Logger.LoggerFactory.Instance.Logger_Error(ex); } }
/// <summary> /// 上传文件到FastDFS /// </summary> /// <param name="storageNode"></param> /// <param name="contentByte"></param> /// <param name="fileExt"></param> /// <returns></returns> public static string UploadFile(StorageNode storageNode, byte[] contentByte, string fileExt) { return(UploadFile(storageNode, contentByte, fileExt, null, null)); }
static void Main(string[] args) { //===========================Initial======================================== List <IPEndPoint> trackerIPs = new List <IPEndPoint>(); IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("192.168.2.50"), 22122); trackerIPs.Add(endPoint); ConnectionManager.Initialize(trackerIPs); StorageNode node = FastDFSClient.GetStorageNode("fdfsgroup1"); //===========================DownloadFile==================================== var fileName = "M00/00/00/wKgCMlGkFyGASOCUAAMJ-PUDjH8009.mp4"; byte[] buffer = FastDFSClient.DownloadFile(node, fileName, 0L, 0L); FileStream stream = new FileStream(@"D:\PUDjH8009.mp4", FileMode.Create); using (BinaryWriter write = new BinaryWriter(stream, Encoding.BigEndianUnicode)) { write.Write(buffer); write.Close(); } stream.Close(); //===========================UploadFile===================================== byte[] content = null; if (File.Exists(@"c:\resource\video.mp4")) { FileStream streamUpload = new FileStream(@"c:\resource\video.mp4", 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, "mp4"); //===========================BatchUploadFile===================================== string[] _FileEntries = Directory.GetFiles(@"E:\fastimage\三维", "*.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); //===========================QueryFile======================================= fileName = "M00/00/00/wKhR6U__-BnxYu0eAxRgAJZBq9Q180.mdb"; FDFSFileInfo fileInfo = FastDFSClient.GetFileInfo(node, fileName); Console.WriteLine(string.Format("FileName:{0}", fileName)); Console.WriteLine(string.Format("FileSize:{0}", fileInfo.FileSize)); Console.WriteLine(string.Format("CreateTime:{0}", fileInfo.CreateTime)); Console.WriteLine(string.Format("Crc32:{0}", fileInfo.Crc32)); //==========================AppendFile======================================= 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测试,直接下载======================================= WebClient web = new WebClient(); web.DownloadFile("http://img13.360buyimg.com/da/g5/M02/0D/16/rBEDik_nOJ0IAAAAAAA_cbJCY-UAACrRgMhVLEAAD-J352.jpg", "C:\\abc.jpg"); web.DownloadFile("http://192.168.81.233/gruop1/M00/00/00/wKhR6VADbNr5s7ODAAIOGO1_YmA574.jpg", "C:\\abc.jpg"); Console.WriteLine("Complete"); Console.Read(); }