Exemple #1
0
        private List <string> Get(string directoryName, FileSystemType type, string filter = "*")
        {
            filter = (string.IsNullOrEmpty(filter)) ? "*" : filter;
            List <string>   files                    = new List <string>();
            S3DirectoryInfo s3DirectoryInfo          = GetDirectoryInfo(directoryName);
            IEnumerable <IS3FileSystemInfo> elements = null;

            switch (type)
            {
            case FileSystemType.Directory:
                elements = s3DirectoryInfo.EnumerateDirectories(filter, SearchOption.TopDirectoryOnly);
                break;

            case FileSystemType.File:
                elements = s3DirectoryInfo.EnumerateFiles(filter, SearchOption.TopDirectoryOnly);
                break;

            default:
                throw new NotImplementedException(type.ToString());
            }
            foreach (IS3FileSystemInfo file in elements)
            {
                files.Add(directoryName + StorageUtils.DELIMITER + file.Name);
            }
            return(files);
        }
        private static void ScanDirectory(S3DirectoryInfo dir)
        {
            var files   = dir.EnumerateFiles();
            var subDirs = dir.EnumerateDirectories();

            if (subDirs.Any())
            {
                WriteLine($"\r\nProcessing {subDirs.Count()} directories in {dir.Name}...");

                foreach (S3DirectoryInfo sub in subDirs)
                {
                    ScanDirectory(sub);
                }
            }
            if (files.Any())
            {
                WriteLine($"\r\nScanning Files in {dir.Name}...");

                foreach (S3FileInfo file in files)
                {
                    try
                    {
                        ScanFile(file);
                    }
                    catch (Exception e)
                    {
                        LogError(e.Message);
                    }
                }
            }
        }
        static void Main(string[] args)
        {
            stopwatch.Start();

            WriteLine("\r\n\r\n***** " + DateTime.Now + " *****\r\n");

            getPrefs(args);

            while (!authorized)
            {
                GetCredentials();

                // scan S3
                using (var client = new AmazonS3Client(accessKey, secretKey, RegionEndpoint.USEast1))
                {
                    try
                    {
                        S3DirectoryInfo   root = new S3DirectoryInfo(client, bucket, key);
                        S3DirectoryInfo[] dirs = root.EnumerateDirectories().ToArray();

                        authorized = true;
                        bool run = true;

                        while (run)
                        {
                            DisplayDirectories(dirs);

                            WriteLine("\r\nEnter directory number to process directory, or enter 0 to process all.");
                            string response = Console.ReadLine();
                            bool   success  = int.TryParse(response, out dirNum);

                            while (!success || dirNum < 0 || dirNum > dirs.Length)
                            {
                                WriteLine("\r\nInvalid entry. Enter 0 or a directory number:");
                                response = Console.ReadLine();
                                success  = int.TryParse(response, out dirNum);
                            }

                            if (dirNum == 0)
                            {
                                sdd = new ScannedDirectoryData(root.FullName);

                                // process files in root directory
                                WriteLine($"Scanning files in {root.FullName}...");

                                foreach (S3FileInfo f in root.EnumerateFiles())
                                {
                                    try
                                    {
                                        ScanFile(f);
                                    }
                                    catch (Exception e)
                                    {
                                        LogError(e.Message);
                                    }
                                }
                                db.AWSS3Objects.InsertAllOnSubmit(insertionList);
                                SubmitBatch();

                                // process each sub directory recursively and submit
                                foreach (S3DirectoryInfo d in dirs)
                                {
                                    ProcessDirectory(d);
                                }
                                run = false;
                            }
                            else
                            {
                                ProcessDirectory(dirs[dirNum - 1]);
                                WriteLine($"\r\nDo you want to scan another directory? (Y/N)");
                                response = Console.ReadLine();

                                while (response.ToUpper() != "Y" && response.ToUpper() != "N")
                                {
                                    WriteLine("\r\nInvalid response. Enter Y or N:");
                                    response = Console.ReadLine();
                                }
                                run = response.ToUpper() == "Y";
                            }
                        }

                        DisplayTotals();
                        stopwatch.Stop();

                        WriteLine($"\r\nTime elapsed: {stopwatch.Elapsed}");
                    }
                    catch (Exception e)
                    {
                        LogError("UNAUTHORIZED");
                    }
                }
            }
            Console.ReadKey();
        }