Beispiel #1
0
        public static async Task UpdateBucketAsync(IAccount account, Ess3Bucket bucket, CancellationToken token)
        {
            ListObjectsV2Response?response = await List.ObjectsAsync(account, bucket, token).ConfigureAwait(true);  // ConfigAwait must remain true

            if (response is null)
            {
                return;
            }
            if (response.HttpStatusCode != HttpStatusCode.OK)
            {
                return;
            }

            var ess3Objects = response.S3Objects.Select(o => Ess3Factory.Create(o)).ToList();

            var files       = ess3Objects.Where(o => o is Ess3File).Cast <Ess3File>();
            var directories = ess3Objects.Where(o => o is Ess3Directory).Cast <Ess3Directory>();

            PutEveryDirectoryIntoParentDirectory(directories);
            PutEveryFileIntoParentDirectory(files, directories);

            // adding dirs then files in this way "sorts" the dirs first
            // this means we don't need to sort in the GUI
            foreach (Ess3Directory each in directories.Where(d => Ess3Factory.IsBucketLevel(d)))
            {
                bucket.Add(each);
            }

            foreach (Ess3File each in files.Where(f => Ess3Factory.IsBucketLevel(f)))
            {
                bucket.Add(each);
            }
        }
Beispiel #2
0
        private static DeleteObjectsRequest CreateDirectoryDeletionRequest(Ess3Bucket bucket, Ess3Directory directory)
        {
            DeleteObjectsRequest request = new DeleteObjectsRequest
            {
                BucketName = bucket.BucketName
            };

            // delete the directory key itself
            request.AddKey(directory.Key);

            // delete every subdirectory key
            //foreach (Ess3Directory eachDirectory in bucket.Directories)
            //{
            //    request.AddKey(eachDirectory.Key);
            //}

            // delete every file key on the directory
            //foreach (Ess3File eachFile in bucket.Files)
            //{
            //    request.AddKey(eachFile.Key);
            //}

            foreach (Ess3Object each in bucket.Ess3Objects)
            {
                request.AddKey(each.Key);
            }

            return(request);
        }
Beispiel #3
0
        public void BucketSize_Calculation()
        {
            Random random = new Random(Environment.TickCount % 17);

            Int64 fileSize1 = (Int64)random.Next(10, Int32.MaxValue - 1);
            Int64 fileSize2 = (Int64)random.Next(10, Int32.MaxValue - 1);
            Int64 fileSize3 = (Int64)random.Next(10, Int32.MaxValue - 1);
            Int64 fileSize4 = (Int64)random.Next(10, Int32.MaxValue - 1);

            var file1 = new Ess3File(new S3Object {
                Size = fileSize1, Key = "file1"
            });
            var file2 = new Ess3File(new S3Object {
                Size = fileSize2, Key = "file2"
            });
            var file3 = new Ess3File(new S3Object {
                Size = fileSize3, Key = "file3"
            });
            var file4 = new Ess3File(new S3Object {
                Size = fileSize4, Key = "file4"
            });

            var directory1 = new Ess3Directory {
                Key = "dir1"
            };
            var directory2 = new Ess3Directory {
                Key = "dir2"
            };
            var directory3 = new Ess3Directory {
                Key = "dir3"
            };

            directory1.Add(file1);
            directory2.Add(file2);
            directory3.Add(file3);

            directory2.Add(directory3);
            directory1.Add(directory2);

            var bucket = new Ess3Bucket("bucket", DateTime.Now);

            bucket.Add(directory1);
            bucket.Add(file4);

            Int64 expected = fileSize1 + fileSize2 + fileSize3 + fileSize4;
            Int64 actual   = bucket.Size;

            Assert.AreEqual(expected, actual, "bucket has {0} objects ({1}, {2}, {3}, {4})", bucket.Ess3Objects.Count, fileSize1, fileSize2, fileSize3, fileSize4);
        }
Beispiel #4
0
        public static async Task <bool> FileAsync(IAccount account, Ess3Bucket bucket, Ess3File file, CancellationToken token)
        {
            DeleteObjectRequest request = new DeleteObjectRequest
            {
                BucketName = bucket.BucketName,
                Key        = file.Key
            };

            using IAmazonS3 client = new AmazonS3Client(account.GetCredentials(), bucket.RegionEndpoint);

            DeleteObjectResponse?response = await Helpers
                                            .RunWithCatchAsync <DeleteObjectRequest, DeleteObjectResponse>(client.DeleteObjectAsync, request, token)
                                            .ConfigureAwait(false);

            return((response?.HttpStatusCode ?? HttpStatusCode.BadRequest) == HttpStatusCode.OK);
            // I don't know what status code translates to success
        }
Beispiel #5
0
        public static async Task <bool> DirectoryAsync(IAccount account, Ess3Bucket bucket, Ess3Directory directory, bool failOnHasItems, CancellationToken token)
        {
            if (BucketHasItems(bucket) && failOnHasItems)
            {
                return(false);
            }

            DeleteObjectsRequest request = CreateDirectoryDeletionRequest(bucket, directory);

            using IAmazonS3 client = new AmazonS3Client(account.GetCredentials(), bucket.RegionEndpoint);

            DeleteObjectsResponse?response = await Helpers
                                             .RunWithCatchAsync <DeleteObjectsRequest, DeleteObjectsResponse>(client.DeleteObjectsAsync, request, token)
                                             .ConfigureAwait(false);

            return((response?.HttpStatusCode ?? HttpStatusCode.BadRequest) == HttpStatusCode.OK);
            // I don't know which status code translates to success
        }
Beispiel #6
0
 public static Task UpdateBucketAsync(IAccount account, Ess3Bucket bucket)
 => UpdateBucketAsync(account, bucket, CancellationToken.None);
Beispiel #7
0
        private static bool BucketHasItems(Ess3Bucket bucket)
        {
            //return bucket.Directories.Count + bucket.Files.Count > 0;

            return(bucket.Ess3Objects.Count > 0);
        }
Beispiel #8
0
 public static Task <bool> DirectoryAsync(IAccount account, Ess3Bucket bucket, Ess3Directory directory, bool failOnHasItems)
 => DirectoryAsync(account, bucket, directory, failOnHasItems, CancellationToken.None);
Beispiel #9
0
 public static Task <bool> FileAsync(IAccount account, Ess3Bucket bucket, Ess3File file)
 => FileAsync(account, bucket, file, CancellationToken.None);
Beispiel #10
0
        public static async Task <ListObjectsV2Response?> ObjectsAsync(IAccount account, Ess3Bucket bucket, CancellationToken token)
        {
            ListObjectsV2Request request = new ListObjectsV2Request
            {
                BucketName = bucket.BucketName,
                FetchOwner = true
            };

            using (IAmazonS3 client = new AmazonS3Client(account.GetCredentials(), bucket.RegionEndpoint))
            {
                ListObjectsV2Response?response = await Helpers
                                                 .RunWithCatchAsync <ListObjectsV2Request, ListObjectsV2Response>(client.ListObjectsV2Async, request, token)
                                                 .ConfigureAwait(false);

                return(response);
            }
        }
Beispiel #11
0
 public static Task <ListObjectsV2Response?> ObjectsAsync(IAccount account, Ess3Bucket bucket)
 => ObjectsAsync(account, bucket, CancellationToken.None);