Пример #1
0
        static void Main(string[] args)
        {
            var files = new[]
            {
                "1.jpg",
                "2.jpg",
                "3.jpg",
            };

            ClientGlobal.InitFromFile("fdfs_client.json");
            Console.WriteLine(ClientGlobal.configInfo());

            var trackerClient = new TrackerClient();
            var trackerServer = trackerClient.getConnection();
            var storageClient = new StorageClient(trackerServer, null);

            var fileIds = new List <string[]>();

            Console.WriteLine($"Total files to be upload: {files.Length}");
            foreach (var file in files)
            {
                var metaData = new NameValueCollection
                {
                    ["filename"] = file
                };
                var result = storageClient.upload_file(file, null, metaData);
                if (result != null)
                {
                    fileIds.Add(result);

                    var fileId = string.Join("/", result);
                    Console.WriteLine($"Upload success. file: {file}, file id: {fileId}");
                }
                else
                {
                    Console.WriteLine($"Failed to upload file: {file}");
                }
            }

            foreach (var fileId in fileIds)
            {
                var fileData = storageClient.download_file(fileId[0], fileId[1]);
                using (var fs = System.IO.File.Create(System.IO.Path.GetFileName(fileId[1])))
                {
                    fs.Write(fileData, 0, fileData.Length);
                }
            }


            Console.ReadLine();
        }
Пример #2
0
        public static void Run(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Error: Must have 1 parameter: config filename");
                return;
            }

            try
            {
                ClientGlobal.init(args[0]);
                TrackerClient tc      = new TrackerClient();
                TrackerServer tracker = tc.getConnection();
                if (tracker == null)
                {
                    return;
                }

                int count = 0;
                StructGroupStat[] groupStats = tc.listGroups(tracker);
                if (groupStats == null)
                {
                    Console.WriteLine("");
                    Console.WriteLine("ERROR! list groups error, error no: " + tc.ErrorCode);
                    Console.WriteLine("");
                    return;
                }
                Console.WriteLine("group count: " + groupStats.Length);

                foreach (StructGroupStat groupStat in groupStats)
                {
                    count++;
                    Console.WriteLine("Group " + count + ":");
                    Console.WriteLine("group name = " + groupStat.GroupName);
                    Console.WriteLine("disk total space = " + groupStat.TotalMB + "MB");
                    Console.WriteLine("disk free space = " + groupStat.FreeMB + " MB");
                    Console.WriteLine("trunk free space = " + groupStat.TrunkFreeMB + " MB");
                    Console.WriteLine("storage server count = " + groupStat.StorageCount);
                    Console.WriteLine("active server count = " + groupStat.ActiveCount);
                    Console.WriteLine("storage server port = " + groupStat.StoragePort);
                    Console.WriteLine("storage HTTP port = " + groupStat.StorageHttpPort);
                    Console.WriteLine("store path count = " + groupStat.StorePathCount);
                    Console.WriteLine("subdir count per path = " + groupStat.SubdirCountPerPath);
                    Console.WriteLine("current write server index = " + groupStat.CurrentWriteServer);
                    Console.WriteLine("current trunk file id = " + groupStat.CurrentTrunkFileId);

                    StructStorageStat[] storageStats = tc.listStorages(tracker, groupStat.GroupName);
                    if (storageStats == null)
                    {
                        Console.WriteLine("");
                        Console.WriteLine("ERROR! list storage error, error no: " + tc.ErrorCode);
                        Console.WriteLine("");
                        break;
                    }

                    int stroageCount = 0;
                    foreach (StructStorageStat storageStat in storageStats)
                    {
                        stroageCount++;
                        Console.WriteLine("\tStorage " + stroageCount + ":");
                        Console.WriteLine("\t\tstorage id = " + storageStat.Id);
                        Console.WriteLine("\t\tip_addr = " + storageStat.IpAddr + "  " + ProtoCommon.getStorageStatusCaption(storageStat.Status));
                        Console.WriteLine("\t\thttp domain = " + storageStat.DomainName);
                        Console.WriteLine("\t\tversion = " + storageStat.Version);
                        Console.WriteLine("\t\tjoin time = " + storageStat.JoinTime.ToString("yyyy-MM-dd HH:mm:ss"));
                        Console.WriteLine("\t\tup time = " + storageStat.UpTime.ToString("yyyy-MM-dd HH:mm:ss"));
                        Console.WriteLine("\t\ttotal storage = " + storageStat.TotalMB + "MB");
                        Console.WriteLine("\t\tfree storage = " + storageStat.FreeMB + "MB");
                        Console.WriteLine("\t\tupload priority = " + storageStat.UploadPriority);
                        Console.WriteLine("\t\tstore_path_count = " + storageStat.StorePathCount);
                        Console.WriteLine("\t\tsubdir_count_per_path = " + storageStat.SubdirCountPerPath);
                        Console.WriteLine("\t\tstorage_port = " + storageStat.StoragePort);
                        Console.WriteLine("\t\tstorage_http_port = " + storageStat.StorageHttpPort);
                        Console.WriteLine("\t\tcurrent_write_path = " + storageStat.CurrentWritePath);
                        Console.WriteLine("\t\tsource ip_addr = " + storageStat.SrcIpAddr);
                        Console.WriteLine("\t\tif_trunk_server = " + storageStat.IfTrunkServer);
                        Console.WriteLine("\t\tconntion.alloc_count  = " + storageStat.ConnectionAllocCount);
                        Console.WriteLine("\t\tconntion.current_count  = " + storageStat.ConnectionCurrentCount);
                        Console.WriteLine("\t\tconntion.max_count  = " + storageStat.ConnectionMaxCount);
                        Console.WriteLine("\t\ttotal_upload_count = " + storageStat.TotalUploadCount);
                        Console.WriteLine("\t\tsuccess_upload_count = " + storageStat.SuccessUploadCount);
                        Console.WriteLine("\t\ttotal_append_count = " + storageStat.TotalAppendCount);
                        Console.WriteLine("\t\tsuccess_append_count = " + storageStat.SuccessAppendCount);
                        Console.WriteLine("\t\ttotal_modify_count = " + storageStat.TotalModifyCount);
                        Console.WriteLine("\t\tsuccess_modify_count = " + storageStat.SuccessModifyCount);
                        Console.WriteLine("\t\ttotal_truncate_count = " + storageStat.TotalTruncateCount);
                        Console.WriteLine("\t\tsuccess_truncate_count = " + storageStat.SuccessTruncateCount);
                        Console.WriteLine("\t\ttotal_set_meta_count = " + storageStat.TotalSetMetaCount);
                        Console.WriteLine("\t\tsuccess_set_meta_count = " + storageStat.SuccessSetMetaCount);
                        Console.WriteLine("\t\ttotal_delete_count = " + storageStat.TotalDeleteCount);
                        Console.WriteLine("\t\tsuccess_delete_count = " + storageStat.SuccessDeleteCount);
                        Console.WriteLine("\t\ttotal_download_count = " + storageStat.TotalDownloadCount);
                        Console.WriteLine("\t\tsuccess_download_count = " + storageStat.SuccessDownloadCount);
                        Console.WriteLine("\t\ttotal_get_meta_count = " + storageStat.TotalGetMetaCount);
                        Console.WriteLine("\t\tsuccess_get_meta_count = " + storageStat.SuccessGetMetaCount);
                        Console.WriteLine("\t\ttotal_create_link_count = " + storageStat.TotalCreateLinkCount);
                        Console.WriteLine("\t\tsuccess_create_link_count = " + storageStat.SuccessCreateLinkCount);
                        Console.WriteLine("\t\ttotal_delete_link_count = " + storageStat.TotalDeleteLinkCount);
                        Console.WriteLine("\t\tsuccess_delete_link_count = " + storageStat.SuccessDeleteLinkCount);
                        Console.WriteLine("\t\ttotal_upload_bytes = " + storageStat.TotalUploadBytes);
                        Console.WriteLine("\t\tsuccess_upload_bytes = " + storageStat.SuccessUploadBytes);
                        Console.WriteLine("\t\ttotal_append_bytes = " + storageStat.TotalAppendBytes);
                        Console.WriteLine("\t\tsuccess_append_bytes = " + storageStat.SuccessAppendBytes);
                        Console.WriteLine("\t\ttotal_modify_bytes = " + storageStat.TotalModifyBytes);
                        Console.WriteLine("\t\tsuccess_modify_bytes = " + storageStat.SuccessModifyBytes);
                        Console.WriteLine("\t\ttotal_download_bytes = " + storageStat.TotalDownloadloadBytes);
                        Console.WriteLine("\t\tsuccess_download_bytes = " + storageStat.SuccessDownloadloadBytes);
                        Console.WriteLine("\t\ttotal_sync_in_bytes = " + storageStat.TotalSyncInBytes);
                        Console.WriteLine("\t\tsuccess_sync_in_bytes = " + storageStat.SuccessSyncInBytes);
                        Console.WriteLine("\t\ttotal_sync_out_bytes = " + storageStat.TotalSyncOutBytes);
                        Console.WriteLine("\t\tsuccess_sync_out_bytes = " + storageStat.SuccessSyncOutBytes);
                        Console.WriteLine("\t\ttotal_file_open_count = " + storageStat.TotalFileOpenCount);
                        Console.WriteLine("\t\tsuccess_file_open_count = " + storageStat.SuccessFileOpenCount);
                        Console.WriteLine("\t\ttotal_file_read_count = " + storageStat.TotalFileReadCount);
                        Console.WriteLine("\t\tsuccess_file_read_count = " + storageStat.SuccessFileReadCount);
                        Console.WriteLine("\t\ttotal_file_write_count = " + storageStat.TotalFileWriteCount);
                        Console.WriteLine("\t\tsuccess_file_write_count = " + storageStat.SuccessFileWriteCount);
                        Console.WriteLine("\t\tlast_heart_beat_time = " + storageStat.LastHeartBeatTime.ToString("yyyy-MM-dd HH:mm:ss"));
                        Console.WriteLine("\t\tlast_source_update = " + storageStat.LastSourceUpdate.ToString("yyyy-MM-dd HH:mm:ss"));
                        Console.WriteLine("\t\tlast_sync_update = " + storageStat.LastSyncUpdate.ToString("yyyy-MM-dd HH:mm:ss"));
                        //Console.WriteLine("\t\tlast_synced_timestamp = " + storageStat.LastSyncedTimestamp.ToString("yyyy-MM-dd HH:mm:ss") + getSyncedDelayString(storageStats, storageStat));
                    }
                }
                tracker.close();

                Console.Read();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Пример #3
0
        public static void Run(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Error: Must have 2 parameters, one is config filename, "
                                  + "the other is the local filename to upload");
                return;
            }

            string conf_filename  = args[0];
            string local_filename = args[1];

            try
            {
                ClientGlobal.init(conf_filename);

                long          startTime;
                ServerInfo[]  servers;
                TrackerClient tracker       = new TrackerClient();
                TrackerServer trackerServer = tracker.getConnection();

                StorageServer storageServer = null;

                StorageClientEx client = new StorageClientEx(trackerServer, storageServer);
                byte[]          file_buff;
                NameValuePair[] meta_list;
                string          group_name;
                string          appender_file_id;
                string          file_ext_name;
                int             errno;

                meta_list    = new NameValuePair[4];
                meta_list[0] = new NameValuePair("width", "800");
                meta_list[1] = new NameValuePair("heigth", "600");
                meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF");
                meta_list[3] = new NameValuePair("author", "Mike");

                file_buff = Encoding.GetEncoding(ClientGlobal.g_charset).GetBytes("this is a test");
                Console.WriteLine("file length: " + file_buff.Length);

                group_name = null;
                StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
                if (storageServers == null)
                {
                    Console.WriteLine("get store storage servers fail, error code: " + tracker.ErrorCode);
                }
                else
                {
                    Console.WriteLine("store storage servers count: " + storageServers.Length);
                    for (int k = 0; k < storageServers.Length; k++)
                    {
                        Console.WriteLine((k + 1) + ". " + storageServers[k].InetSockAddr.Address + ":" + storageServers[k].InetSockAddr.Port);
                    }
                    Console.WriteLine("");
                }

                Stopwatch sw = new Stopwatch();
                sw.Start();
                appender_file_id = client.upload_appender_file1(file_buff, "txt", meta_list);
                sw.Stop();
                Console.WriteLine("upload_appender_file1 time used: " + sw.ElapsedMilliseconds + " ms");

                /*
                 * group_name = "";
                 * appender_file_id = client.upload_appender_file1(group_name, file_buff, "txt", meta_list);
                 */
                if (appender_file_id == null)
                {
                    Console.WriteLine("upload file fail, error code: " + client.ErrorCode);
                    return;
                }
                else
                {
                    Console.WriteLine(client.get_file_info1(appender_file_id));

                    servers = tracker.getFetchStorages1(trackerServer, appender_file_id);
                    if (servers == null)
                    {
                        Console.WriteLine("get storage servers fail, error code: " + tracker.ErrorCode);
                    }
                    else
                    {
                        Console.WriteLine("storage servers count: " + servers.Length);
                        for (int k = 0; k < servers.Length; k++)
                        {
                            Console.WriteLine((k + 1) + ". " + servers[k].Ip_Addr + ":" + servers[k].Port);
                        }
                        Console.WriteLine("");
                    }

                    meta_list    = new NameValuePair[4];
                    meta_list[0] = new NameValuePair("width", "1024");
                    meta_list[1] = new NameValuePair("heigth", "768");
                    meta_list[2] = new NameValuePair("bgcolor", "#000000");
                    meta_list[3] = new NameValuePair("title", "Untitle");

                    sw.Restart();
                    errno = client.set_metadata1(appender_file_id, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE);
                    sw.Stop();
                    Console.WriteLine("set_metadata time used: " + sw.ElapsedMilliseconds + " ms");
                    if (errno == 0)
                    {
                        Console.WriteLine("set_metadata success");
                    }
                    else
                    {
                        Console.WriteLine("set_metadata fail, error no: " + errno);
                    }

                    meta_list = client.get_metadata1(appender_file_id);
                    if (meta_list != null)
                    {
                        for (int i = 0; i < meta_list.Length; i++)
                        {
                            Console.WriteLine(meta_list[i]._Name + " " + meta_list[i]._Value);
                        }
                    }

                    sw.Restart();
                    file_buff = client.download_file1(appender_file_id);
                    sw.Stop();
                    Console.WriteLine("download_file time used: " + sw.ElapsedMilliseconds + " ms");

                    if (file_buff != null)
                    {
                        Console.WriteLine("file length:" + file_buff.Length);
                        Console.WriteLine(Encoding.GetEncoding(ClientGlobal.g_charset).GetString(file_buff));
                    }

                    file_buff     = Encoding.GetEncoding(ClientGlobal.g_charset).GetBytes("this is a slave buff");
                    file_ext_name = "txt";
                    sw.Restart();
                    errno = client.append_file1(appender_file_id, file_buff);
                    sw.Stop();
                    Console.WriteLine("append_file time used: " + sw.ElapsedMilliseconds + " ms");
                    if (errno == 0)
                    {
                        Console.WriteLine(client.get_file_info1(appender_file_id));
                    }
                    else
                    {
                        Console.WriteLine("append file fail, error no: " + errno);
                    }

                    sw.Restart();
                    errno = client.delete_file1(appender_file_id);
                    sw.Stop();
                    Console.WriteLine("delete_file time used: " + sw.ElapsedMilliseconds + " ms");
                    if (errno == 0)
                    {
                        Console.WriteLine("Delete file success");
                    }
                    else
                    {
                        Console.WriteLine("Delete file fail, error no: " + errno);
                    }
                }

                appender_file_id = client.upload_appender_file1(local_filename, null, meta_list);
                if (appender_file_id != null)
                {
                    int        ts;
                    string     token;
                    string     file_url;
                    IPEndPoint inetSockAddr;

                    inetSockAddr = trackerServer.InetSockAddr;
                    file_url     = "http://" + inetSockAddr.Address;
                    if (ClientGlobal.g_tracker_http_port != 80)
                    {
                        file_url += ":" + ClientGlobal.g_tracker_http_port;
                    }
                    file_url += "/" + appender_file_id;
                    if (ClientGlobal.g_anti_steal_token)
                    {
                        ts        = (int)ProtoCommon.DateTimeToUnixTimestamp(DateTime.Now);
                        token     = ProtoCommon.getToken(appender_file_id, ts, ClientGlobal.g_secret_key);
                        file_url += "?token=" + token + "&ts=" + ts;
                    }

                    Console.WriteLine(client.get_file_info1(appender_file_id));
                    Console.WriteLine("file url: " + file_url);

                    errno = client.download_file1(appender_file_id, 0, 0, "c:\\" + appender_file_id.Replace("/", "_"));
                    if (errno == 0)
                    {
                        Console.WriteLine("Download file success");
                    }
                    else
                    {
                        Console.WriteLine("Download file fail, error no: " + errno);
                    }

                    errno = client.download_file1(appender_file_id, 0, 0, new DownloadFileWriter("c:\\" + appender_file_id.Replace("/", "-")));
                    if (errno == 0)
                    {
                        Console.WriteLine("Download file success");
                    }
                    else
                    {
                        Console.WriteLine("Download file fail, error no: " + errno);
                    }

                    file_ext_name = null;
                    sw.Restart();
                    errno = client.append_file1(appender_file_id, local_filename);
                    sw.Stop();
                    Console.WriteLine("append_file time used: " + sw.ElapsedMilliseconds + " ms");
                    if (errno == 0)
                    {
                        Console.WriteLine(client.get_file_info1(appender_file_id));
                    }
                    else
                    {
                        Console.WriteLine("append file fail, error no: " + errno);
                    }
                }

                System.IO.FileInfo f;
                f             = new System.IO.FileInfo(local_filename);
                file_ext_name = Path.GetExtension(local_filename).Trim('.');

                appender_file_id = client.upload_appender_file1(null, f.Length,
                                                                new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
                if (appender_file_id != null)
                {
                    Console.WriteLine(client.get_file_info1(appender_file_id));

                    sw.Restart();
                    errno = client.append_file1(appender_file_id, f.Length, new UploadLocalFileSender(local_filename));
                    sw.Stop();
                    Console.WriteLine("append_file time used: " + sw.ElapsedMilliseconds + " ms");
                    if (errno == 0)
                    {
                        Console.WriteLine(client.get_file_info1(appender_file_id));
                    }
                    else
                    {
                        Console.WriteLine("append file fail, error no: " + errno);
                    }

                    sw.Restart();
                    errno = client.modify_file1(appender_file_id, 0, f.Length, new UploadLocalFileSender(local_filename));
                    sw.Stop();
                    Console.WriteLine("modify_file time used: " + sw.ElapsedMilliseconds + " ms");
                    if (errno == 0)
                    {
                        Console.WriteLine(client.get_file_info1(appender_file_id));
                    }
                    else
                    {
                        Console.WriteLine("modify file fail, error no: " + errno);
                    }

                    sw.Restart();
                    errno = client.truncate_file1(appender_file_id, 0);
                    sw.Stop();
                    Console.WriteLine("truncate_file time used: " + sw.ElapsedMilliseconds + " ms");
                    if (errno == 0)
                    {
                        Console.WriteLine(client.get_file_info1(appender_file_id));
                    }
                    else
                    {
                        Console.WriteLine("truncate file fail, error no: " + errno);
                    }
                }
                else
                {
                    Console.WriteLine("Upload file fail, error no: " + errno);
                }

                storageServer = tracker.getFetchStorage1(trackerServer, appender_file_id);
                if (storageServer == null)
                {
                    Console.WriteLine("getFetchStorage fail, errno code: " + tracker.ErrorCode);
                    return;
                }
                /* for test only */
                Console.WriteLine("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket()));

                storageServer.close();

                /* for test only */
                Console.WriteLine("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket()));

                trackerServer.close();
            }
            catch (Exception ex)
            {
            }
        }