Example #1
0
        private static bool listObjects()
        {
            try
            {
                // List Objects
                Console.WriteLine("--- List Objects: ---");

                //ObjectListing objects = ks3Client.listObjects(bucketName);

                KS3Client ks3Client = new KS3Client("ak", "sk");
                ks3Client.setEndpoint("kss.ksyun.com");

                ListObjectsRequest request = new ListObjectsRequest();
                request.setBucketName("haofenshu");
                //request.setMarker("PersistenceServiceImpl.java");
                request.setPrefix("file/s/167206/1082/ClipedRecord.xml");
                request.setDelimiter("/");
                ObjectListing objects = ks3Client.listObjects(request);


                //Console.WriteLine(objects.ToString());
                Console.WriteLine("---------------------\n");

                // Get Object Metadata
                Console.WriteLine("--- Get Object Metadata ---");

                //ObjectMetadata objMeta = ks3Client.getObjectMetadata(bucketName, objKeyNameMemoryData);
                //Console.WriteLine(objMeta.ToString());
                //Console.WriteLine();
                ObjectMetadata objMeta = ks3Client.getObjectMetadata(bucketName, objKeyNameFileData);
                Console.WriteLine(objMeta.ToString());

                Console.WriteLine("---------------------------\n");
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.ToString());
                return(false);
            }

            return(true);
        }
Example #2
0
        public void TestListObjects()
        {
            if (!init())
            {
                ObjectListing objects = ks3Client.listObjects(bucketName);
                //ListObjectsRequest request = new ListObjectsRequest();
                //request.setBucketName(bucketName);
                //request.setMarker("PersistenceServiceImpl.java");
                //ObjectListing objects = ks3Client.listObjects(request);
                //Assert.IsNotNull(objects);

                Console.WriteLine(objects.ToString());
                Console.WriteLine("---------------------\n");

                // Get Object Metadata
                Console.WriteLine("--- Get Object Metadata ---");

                ObjectMetadata objMeta = ks3Client.getObjectMetadata(bucketName, objKeyNameMemoryData);
                Console.WriteLine(objMeta.ToString());
                Console.WriteLine();
                objMeta = ks3Client.getObjectMetadata(bucketName, objKeyNameFileData);
                Assert.IsNotNull(objMeta);
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            try
            {
                #region Parse-Arguments

                if (args == null || args.Length < 2)
                {
                    Usage("No arguments specified");
                    return;
                }

                IndexFile = args[0];
                Command   = args[1];

                for (int i = 2; i < args.Length; i++)
                {
                    if (String.IsNullOrEmpty(args[i]))
                    {
                        continue;
                    }
                    if (args[i].StartsWith("--chunks=") && args[i].Length > 9)
                    {
                        ChunkDirectory = args[i].Substring(9);
                        if (!ChunkDirectory.EndsWith("\\"))
                        {
                            ChunkDirectory += "\\";
                        }
                        if (!Directory.Exists(ChunkDirectory))
                        {
                            Directory.CreateDirectory(ChunkDirectory);
                        }
                    }
                    else if (args[i].StartsWith("--key=") && args[i].Length > 6)
                    {
                        ObjectKey = args[i].Substring(6);
                    }
                    else if (String.Compare(args[i], "--debug") == 0)
                    {
                        DebugDedupe = true;
                    }
                    else if (String.Compare(args[i], "--debugsql") == 0)
                    {
                        DebugSql = true;
                    }
                    else if (args[i].StartsWith("--params=") && args[i].Length > 9)
                    {
                        CreateParams = args[i].Substring(9);
                        if (new Regex(@"^\d+,\d+,\d+,\d+$").IsMatch(CreateParams))
                        {
                            string[] currParams = CreateParams.Split(',');
                            if (currParams.Length != 4)
                            {
                                Usage("Value for 'params' is invalid");
                                return;
                            }

                            if (!Int32.TryParse(currParams[0], out MinChunkSize) ||
                                !Int32.TryParse(currParams[1], out MaxChunkSize) ||
                                !Int32.TryParse(currParams[2], out ShiftCount) ||
                                !Int32.TryParse(currParams[3], out BoundaryCheckBytes)
                                )
                            {
                                Usage("Value for 'params' is not of the form int,int,int,int");
                                return;
                            }
                        }
                        else
                        {
                            Usage("Value for 'params' is not of the form int,int,int,int");
                            return;
                        }
                    }
                    else
                    {
                        Usage("Unknown argument: " + args[i]);
                        return;
                    }
                }

                #endregion

                #region Verify-Values

                List <string> validCommands = new List <string>()
                {
                    "create", "stats", "store", "retrieve", "delete", "list", "exists", "metadata"
                };
                if (!validCommands.Contains(Command))
                {
                    Usage("Invalid command: " + Command);
                    return;
                }

                #endregion

                #region Enumerate

                if (DebugDedupe)
                {
                    Console.WriteLine("Command         : " + Command);
                    Console.WriteLine("Index File      : " + IndexFile);
                    Console.WriteLine("Chunk Directory : " + ChunkDirectory);
                    Console.WriteLine("Object Key      : " + ObjectKey);
                    if (MinChunkSize > 0)
                    {
                        Console.WriteLine("Min Chunk Size  : " + MinChunkSize);
                    }
                    if (MaxChunkSize > 0)
                    {
                        Console.WriteLine("Max Chunk Size  : " + MaxChunkSize);
                    }
                    if (ShiftCount > 0)
                    {
                        Console.WriteLine("Shift Count     : " + ShiftCount);
                    }
                    if (BoundaryCheckBytes > 0)
                    {
                        Console.WriteLine("Boundary Bytes  : " + BoundaryCheckBytes);
                    }
                }

                #endregion

                #region Create

                if (String.Compare(Command, "create") == 0)
                {
                    Dedupe = new DedupeLibrary(IndexFile, MinChunkSize, MaxChunkSize, ShiftCount, BoundaryCheckBytes, WriteChunk, ReadChunk, DeleteChunk, DebugDedupe, DebugSql);
                    if (DebugDedupe)
                    {
                        Console.WriteLine("Successfully wrote new index: " + IndexFile);
                    }
                    return;
                }

                #endregion

                #region Initialize-Index

                if (!File.Exists(IndexFile))
                {
                    Console.WriteLine("*** Index file " + IndexFile + " not found");
                }

                Dedupe = new DedupeLibrary(IndexFile, WriteChunk, ReadChunk, DeleteChunk, DebugDedupe, DebugSql);

                #endregion

                #region Process-by-Command

                switch (Command)
                {
                case "stats":
                    if (Dedupe.IndexStats(out NumObjects, out NumChunks, out LogicalBytes, out PhysicalBytes, out DedupeRatioX, out DedupeRatioPercent))
                    {
                        Console.WriteLine("Statistics:");
                        Console.WriteLine("  Number of objects : " + NumObjects);
                        Console.WriteLine("  Number of chunks  : " + NumChunks);
                        Console.WriteLine("  Logical bytes     : " + LogicalBytes + " bytes");
                        Console.WriteLine("  Physical bytes    : " + PhysicalBytes + " bytes");
                        Console.WriteLine("  Dedupe ratio      : " + DecimalToString(DedupeRatioX) + "X, " + DecimalToString(DedupeRatioPercent) + "%");
                        return;
                    }
                    else
                    {
                        Console.WriteLine("Failed");
                    }
                    return;

                case "retrieve":
                    if (String.IsNullOrEmpty(ObjectKey))
                    {
                        Usage("Object key must be supplied");
                    }
                    else
                    {
                        if (!Dedupe.RetrieveObject(ObjectKey, out ResponseData))
                        {
                            Console.WriteLine("Failed");
                        }
                        else
                        {
                            WriteConsoleData(ResponseData);
                        }
                    }
                    return;

                case "store":
                    if (String.IsNullOrEmpty(ObjectKey))
                    {
                        Usage("Object key must be supplied");
                    }
                    else
                    {
                        if (Dedupe.ObjectExists(ObjectKey))
                        {
                            Console.WriteLine("Already exists");
                        }
                        else
                        {
                            ReadConsoleData();
                            if (!Dedupe.StoreObject(ObjectKey, RequestData, out Chunks))
                            {
                                Console.WriteLine("Failed");
                            }
                            else
                            {
                                Console.WriteLine("Success");
                            }
                        }
                    }
                    return;

                case "delete":
                    if (String.IsNullOrEmpty(ObjectKey))
                    {
                        Usage("Object key must be supplied");
                    }
                    else
                    {
                        if (!Dedupe.DeleteObject(ObjectKey))
                        {
                            Console.WriteLine("Failed");
                        }
                        else
                        {
                            Console.WriteLine("Success");
                        }
                    }
                    return;

                case "metadata":
                    if (String.IsNullOrEmpty(ObjectKey))
                    {
                        Usage("Object key must be supplied");
                    }
                    else
                    {
                        if (!Dedupe.RetrieveObjectMetadata(ObjectKey, out Metadata))
                        {
                            Console.WriteLine("Failed");
                        }
                        else
                        {
                            Console.WriteLine(Metadata.ToString());
                        }
                    }
                    return;

                case "list":
                    Dedupe.ListObjects(out Keys);
                    if (Keys == null || Keys.Count < 1)
                    {
                        Console.WriteLine("No objects");
                    }
                    else
                    {
                        Console.WriteLine("Objects:");
                        foreach (string curr in Keys)
                        {
                            Console.WriteLine("  " + curr);
                        }
                        Console.WriteLine(Keys.Count + " objects in index");
                    }
                    return;

                case "exists":
                    if (String.IsNullOrEmpty(ObjectKey))
                    {
                        Usage("Object key must be supplied");
                        return;
                    }
                    else
                    {
                        Console.WriteLine(Dedupe.ObjectExists(ObjectKey));
                    }
                    return;

                default:
                    Usage("Unknown command: " + Command);
                    return;
                }

                #endregion
            }
            catch (Exception e)
            {
                ExceptionConsole("Dedupe", "Outer exception", e);
            }
        }