示例#1
0
        private static async Task AsyncMain(string[] arg, Factory fac)
        {
            var argument   = Console.ReadLine();
            var argo       = argument.Split(' ').Where(x => x.Trim() != "").ToArray();
            var clientType = argo[0];
            var args       = argo.Skip(1).ToArray();

            try
            {
                IBucketClient client = fac.GetBucket(clientType.ToEnum());
                switch (args[0].ToLower())
                {
                case "bucket":
                    switch (args[1].ToLower())
                    {
                    case "exist":
                        var exist = await client.ExistBucket(args[2]);

                        Console.WriteLine(args[2] + " " + (exist ? "exists!" : "does not exist"));
                        break;

                    case "create":
                        var createResp = await client.CreateBucket(args[2]);

                        Console.WriteLine(JsonConvert.SerializeObject(createResp));
                        break;

                    case "del": goto case "delete";

                    case "delete":
                        var delResp = await client.DeleteBucket(args[2]);

                        Console.WriteLine(JsonConvert.SerializeObject(delResp));
                        break;

                    case "policy":
                        var policyResp = await client.SetReadPolicy(args[2], args[3] == "public"?ReadAccess.Public : ReadAccess.Private);

                        Console.WriteLine(JsonConvert.SerializeObject(policyResp));
                        break;

                    case "cors":
                        var corsResp = await client.SetGETCors(args[2], args.Length > 3?args.Skip(3).ToArray() : new string[] { });

                        Console.Write(JsonConvert.SerializeObject(corsResp));
                        break;

                    default:
                        Console.WriteLine("Unknown command!");
                        break;
                    }
                    break;

                case "blob":
                    var bucketKey = args[2];
                    var bucket    = await client.GetBucket(bucketKey);

                    var argv = args[3];
                    switch (args[1].ToLower())
                    {
                    case "exist":
                        var exist = await bucket.ExistBlob(argv);

                        Console.WriteLine(argv + " " + (exist ? "exists!" : "does not exist"));
                        break;

                    case "create":
                        byte[] file       = File.ReadAllBytes($@"{Directory.GetCurrentDirectory()}/{args[4]}");
                        var    createResp = await bucket.CreateBlob(file, argv);

                        Console.WriteLine(JsonConvert.SerializeObject(createResp));
                        break;

                    case "del": goto case "delete";

                    case "delete":
                        var delResp = await bucket.DeleteBlob(argv);

                        Console.WriteLine(JsonConvert.SerializeObject(delResp));
                        break;

                    case "update":
                        byte[] updateFile = File.ReadAllBytes($@"{Directory.GetCurrentDirectory()}/{args[4]}");
                        var    updateResp = await bucket.UpdateBlob(updateFile, argv);

                        Console.WriteLine(JsonConvert.SerializeObject(updateResp));
                        break;

                    case "put":
                        byte[] putFile = File.ReadAllBytes($@"{Directory.GetCurrentDirectory()}/{args[4]}");
                        var    putResp = await bucket.PutBlob(putFile, argv);

                        Console.WriteLine(JsonConvert.SerializeObject(putResp));
                        break;

                    default:
                        Console.WriteLine("Unknown command!");
                        break;
                    }
                    break;

                case "test":
                    byte[] putFileTest = File.ReadAllBytes($@"{Directory.GetCurrentDirectory()}/{args[3]}");
                    var    putRespTest = await client.PutBlob(putFileTest, new Uri($"https://{LoadConfiguration("secrets.json").DO.region.ToString()}.digitaloceanspaces.com/{args[1]}/{args[2]}"));

                    Console.WriteLine(JsonConvert.SerializeObject(putRespTest));
                    break;

                default:
                    Console.WriteLine("Unknown command");
                    break;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                Console.WriteLine("Invalid number of arguments");
            }
            await AsyncMain(arg, fac);
        }