public static byte[] GetResponse(FDFSRequest request) { Connection conn = null; IPEndPoint address = null; try { try { if (request.Connection == null) { address = locator.GetTrackerAddress(); conn = GetTrackerConnection(address); } else { conn = request.Connection; } //打开 conn.OpenConnection(); locator.SetActive(address); } catch (SocketException ex) { locator.SetInActive(address); throw new FdfsConnectException("connect failed", ex); } var stream = conn.GetStream(); var headerBuffer = request.Header.ToByte(); stream.Write(headerBuffer, 0, headerBuffer.Length); stream.Write(request.Body, 0, request.Body.Length); var header = new FDFSHeader(stream); if (header.Status != 0) { throw new FDFSException(string.Format("Get Response Error,Error Code:{0}", header.Status)); } var body = new byte[header.Length]; if (header.Length != 0) { stream.Read(body, 0, (int)header.Length); } return(body); } finally { //关闭 //Connection.Close(); if (conn != null) { conn.ReleaseConnection(); } } }
/// <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> /// 获取文件信息 /// </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); }
public static IDictionary <string, string> GetMetaData(StorageNode storageNode, string fileName) { FDFSRequest storageRequest = GET_METADATA.Instance.GetRequest(storageNode.EndPoint, storageNode.GroupName, fileName); GET_METADATA.Response storageResponse = new GET_METADATA.Response(); storageRequest.GetResponse(storageResponse); return(storageResponse.MetaData); }
/// <summary> /// 删除文件 /// </summary> /// <param name="groupName">组名</param> /// <param name="fileName">文件名</param> public static void RemoveFile(string groupName, string fileName) { FDFSRequest trackerRequest = QUERY_UPDATE.Instance.GetRequest(groupName, fileName); QUERY_UPDATE.Response trackerResponse = new QUERY_UPDATE.Response(trackerRequest.GetResponse()); IPEndPoint storeEndPoint = new IPEndPoint(IPAddress.Parse(trackerResponse.IPStr), trackerResponse.Port); FDFSRequest storageReqeust = DELETE_FILE.Instance.GetRequest(storeEndPoint, groupName, fileName); storageReqeust.GetResponse(); }
/// <summary> /// 附加文件 /// </summary> /// <param name="groupName">组名</param> /// <param name="fileName">文件名</param> /// <param name="contentByte">文件内容</param> public static void AppendFile(string groupName, string fileName, byte[] contentByte) { FDFSRequest trackerRequest = QUERY_UPDATE.Instance.GetRequest(groupName, fileName); QUERY_UPDATE.Response trackerResponse = new QUERY_UPDATE.Response(trackerRequest.GetResponse()); IPEndPoint storeEndPoint = new IPEndPoint(IPAddress.Parse(trackerResponse.IPStr), trackerResponse.Port); FDFSRequest storageReqeust = APPEND_FILE.Instance.GetRequest(storeEndPoint, fileName, contentByte); storageReqeust.GetResponse(); }
public static string UploadFileByName(StorageNode storageNode, string filename) { using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { string extension = Path.GetExtension(filename).Substring(1); FDFSRequest storageRequest = UPLOAD_FILE.Instance.GetRequest(storageNode.EndPoint, storageNode.StorePathIndex, fs.Length, extension, fs); UPLOAD_FILE.Response storageResponse = new UPLOAD_FILE.Response(); storageRequest.GetResponse(storageResponse); return(storageResponse.FileName); } }
/// <summary> /// 上传从文件 /// </summary> /// <param name="groupName"></param> /// <param name="contentByte">文件内容</param> /// <param name="masterFilename">主文件名</param> /// <param name="prefixName">从文件后缀</param> /// <param name="fileExt">文件扩展名(注意:不包含".")</param> /// <returns>文件名</returns> public static string UploadSlaveFile(string groupName, byte[] contentByte, string masterFilename, string prefixName, string fileExt) { FDFSRequest trackerRequest = QUERY_UPDATE.Instance.GetRequest(groupName, masterFilename); QUERY_UPDATE.Response trackerResponse = new QUERY_UPDATE.Response(trackerRequest.GetResponse()); IPEndPoint storeEndPoint = new IPEndPoint(IPAddress.Parse(trackerResponse.IPStr), trackerResponse.Port); FDFSRequest storageReqeust = UPLOAD_SLAVE_FILE.Instance.GetRequest(storeEndPoint, contentByte.Length, masterFilename, prefixName, fileExt, contentByte); UPLOAD_FILE.Response storageResponse = new UPLOAD_FILE.Response(storageReqeust.GetResponse()); return(storageResponse.FileName); }
/// <summary> /// 获取存储节点 /// </summary> /// <param name="groupName">组名</param> /// <returns>存储节点实体类</returns> public static StorageNode GetStorageNode(string groupName) { 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); StorageNode result = new StorageNode(); result.GroupName = trackerResponse.GroupName; result.EndPoint = storeEndPoint; result.StorePathIndex = trackerResponse.StorePathIndex; return(result); }
public static StorageNode QueryStorageNodeForFile(string groupName, string fileid) { FDFSRequest trackerRequest = QUERY_FETCH_ONE.Instance.GetRequest(groupName, fileid); QUERY_FETCH_ONE.Response trackerResponse = new QUERY_FETCH_ONE.Response(); trackerRequest.GetResponse(trackerResponse); IPEndPoint storeEndPoint = new IPEndPoint(IPAddress.Parse(trackerResponse.IPStr), trackerResponse.Port); StorageNode result = new StorageNode(); result.GroupName = trackerResponse.GroupName; result.EndPoint = storeEndPoint; result.StorePathIndex = 0; return(result); }
public static string DownloadFileEx(StorageNode storageNode, string filename, string destDir, string destFileName = null) { string fullPath = null; if (destFileName == null) { IDictionary <string, string> metaData = GetMetaData(storageNode, filename); destFileName = metaData["Name"] + Path.GetExtension(filename); } fullPath = Path.Combine(destDir, destFileName); FDFSRequest storageRequest = DOWNLOAD_FILE.Instance.GetRequest(storageNode.EndPoint, 0L, 0L, storageNode.GroupName, filename); DOWNLOAD_FILE.ResponseEx storageResponse = new DOWNLOAD_FILE.ResponseEx(fullPath); storageRequest.GetResponse(storageResponse); return(storageResponse.FullPath); }
public static StorageNode[] QueryStorageNodesForFile(string groupName, string fileid) { FDFSRequest trackerRequest = QUERY_FETCH_ALL.Instance.GetRequest(groupName, fileid); QUERY_FETCH_ALL.Response trackerResponse = new QUERY_FETCH_ALL.Response(); trackerRequest.GetResponse(trackerResponse); List <StorageNode> storageNodes = new List <StorageNode>(); foreach (string IPStr in trackerResponse.IPStrs) { StorageNode storage = new StorageNode(); storage.GroupName = trackerResponse.GroupName; storage.EndPoint = new IPEndPoint(IPAddress.Parse(IPStr), trackerResponse.Port); storage.StorePathIndex = 0; storageNodes.Add(storage); } return(storageNodes.ToArray()); }
public static void SetMetaData(StorageNode storageNode, string fileName, IDictionary <string, string> metadata, MetaDataOption option = MetaDataOption.Overwrite) { FDFSRequest storageRequest = SET_METADATA.Instance.GetRequest(storageNode.EndPoint, storageNode.GroupName, fileName, metadata, option); storageRequest.GetResponse(); // no response body }