Exemple #1
0
        public override void Store(Stream stream, FileSystemObject fso)
        {
            var bki = new S3BucketKeyInfo(fso);

            var attemptedBucketCreate = false;

            do
            {
                try
                {
                    var request = new PutObjectRequest
                    {
                        BucketName   = bki.BucketName,
                        Key          = bki.Key,
                        InputStream  = stream,
                        StorageClass = S3StorageClass.Standard
                    };

                    S3Client.PutObject(request);

                    break;
                }
                catch (AmazonS3Exception s3x)
                {
                    if (!attemptedBucketCreate && IsMissingObjectException(s3x))
                    {
                        CreateFolder(fso.FolderName);
                        attemptedBucketCreate = true;
                        continue;
                    }

                    throw;
                }
            } while (true);
        }
Exemple #2
0
        private ListObjectsResponse ListFolderResponse(S3BucketKeyInfo bki, string nextMarker = null)
        {
            try
            {
                var listRequest = new ListObjectsRequest
                {
                    BucketName = bki.BucketName
                };

                if (!string.IsNullOrEmpty(nextMarker))
                {
                    listRequest.Marker = nextMarker;
                }

                if (bki.HasPrefix)
                {
                    listRequest.Prefix = bki.Prefix;
                }

                var listResponse = S3Client.ListObjects(listRequest);

                return(listResponse);
            }
            catch (System.Xml.XmlException) { }
            catch (AmazonS3Exception s3x)
            {
                if (!IsMissingObjectException(s3x))
                {
                    throw;
                }
            }

            return(null);
        }
Exemple #3
0
        private List <S3Object> ListFolder(S3BucketKeyInfo bki)
        {
            var listResponse = ListFolderResponse(bki);

            return(listResponse == null
                ? new List <S3Object>()
                : listResponse.S3Objects ?? new List <S3Object>());
        }
Exemple #4
0
        private void DeleteFolderInternal(string path, bool recursive)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            var bki = new S3BucketKeyInfo(path, terminateWithPathDelimiter: true);

            if (recursive)
            {
                while (true)
                {
                    var objects = ListFolder(bki);

                    if (!objects.Any())
                    {
                        break;
                    }

                    var keys = objects.Select(o => new KeyVersion
                    {
                        Key = o.Key
                    })
                               .ToList();

                    var deleteObjectsRequest = new DeleteObjectsRequest
                    {
                        BucketName = bki.BucketName,
                        Quiet      = true,
                        Objects    = keys
                    };

                    S3Client.DeleteObjects(deleteObjectsRequest);
                }
            }
            else if (!bki.IsBucketObject)
            {
                var deleteObjectRequest = new DeleteObjectRequest
                {
                    BucketName = bki.BucketName,
                    Key        = bki.Key
                };

                S3Client.DeleteObject(deleteObjectRequest);
            }

            if (bki.IsBucketObject)
            {
                var request = new DeleteBucketRequest
                {
                    BucketName      = bki.BucketName,
                    UseClientRegion = true
                };

                S3Client.DeleteBucket(request);
            }
        }
Exemple #5
0
        public override bool FolderExists(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            var bki = new S3BucketKeyInfo(path);

            // Folders aren't a thing in S3, buckets are the only things that actually matter more or less. Return true
            // if this is a bucket object and the bucket exists, otherwise false since it actually doesn't exist
            return(bki.IsBucketObject && BucketExists(bki));
        }
Exemple #6
0
        public override void CreateFolder(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            var bki = new S3BucketKeyInfo(path);

            var request = new PutBucketRequest
            {
                BucketName      = bki.BucketName,
                UseClientRegion = true
            };

            S3Client.PutBucket(request);
        }
Exemple #7
0
        public override IEnumerable <string> ListFolder(string folderName, bool recursive = false, bool fileNamesOnly = false)
        {
            if (string.IsNullOrEmpty(folderName))
            {
                yield break;
            }

            string nextMarker = null;

            var bki = new S3BucketKeyInfo(folderName, terminateWithPathDelimiter: true);

            do
            {
                var listResponse = ListFolderResponse(bki, nextMarker);

                if (listResponse == null || listResponse.S3Objects == null)
                {
                    break;
                }

                var filesOnly = listResponse.S3Objects
                                .Select(o => new S3BucketKeyInfo(bki.BucketName, o))
                                .Where(b => !string.IsNullOrEmpty(b.FileName))
                                .Where(b => recursive
                        ? b.Prefix.StartsWith(bki.Prefix, StringComparison.Ordinal)
                        : b.Prefix.Equals(bki.Prefix, StringComparison.Ordinal))
                                .Select(b => fileNamesOnly
                        ? b.FileName
                        : b.ToString()
                                        );

                foreach (var file in filesOnly)
                {
                    yield return(file);
                }

                if (listResponse.IsTruncated)
                {
                    nextMarker = listResponse.NextMarker;
                }
                else
                {
                    break;
                }
            } while (true);
        }
Exemple #8
0
        private void GetToLocalFile(FileSystemObject fso, FileSystemObject downloadToFso)
        {
            var bki = new S3BucketKeyInfo(fso.FullName);

            var request = new GetObjectRequest
            {
                BucketName = bki.BucketName,
                Key        = bki.Key
            };

            localFs.CreateFolder(downloadToFso.FolderName);

            localFs.Delete(downloadToFso);

            using (var response = S3Client.GetObject(request))
            {
                response.WriteResponseStreamToFile(downloadToFso.FullName, append: false);
            }
        }
Exemple #9
0
        private void CopyInS3(FileSystemObject sourceFso, FileSystemObject targetFso)
        {
            if (sourceFso.Equals(targetFso))
            {
                return;
            }

            var sourceBki = new S3BucketKeyInfo(sourceFso);
            var targetBki = new S3BucketKeyInfo(targetFso);

            var request = new CopyObjectRequest
            {
                SourceBucket      = sourceBki.BucketName,
                SourceKey         = sourceBki.Key,
                DestinationBucket = targetBki.BucketName,
                DestinationKey    = targetBki.Key
            };

            S3Client.CopyObject(request);
        }
Exemple #10
0
        private void Delete(S3BucketKeyInfo bki)
        {
            var request = new DeleteObjectRequest
            {
                BucketName = bki.BucketName,
                Key        = bki.Key
            };

            try
            {
                S3Client.DeleteObject(request);
            }
            catch (AmazonS3Exception s3x)
            {
                if (IsMissingObjectException(s3x))
                {
                    return;
                }

                throw;
            }
        }
Exemple #11
0
        public override Stream GetStream(FileSystemObject fso)
        {
            var bki = new S3BucketKeyInfo(fso.FullName);

            var request = new GetObjectRequest
            {
                BucketName = bki.BucketName,
                Key        = bki.Key
            };

            try
            {
                return(S3Client.GetObject(request).ResponseStream);
            }
            catch (AmazonS3Exception s3x)
            {
                if (IsMissingObjectException(s3x))
                {
                    return(null);
                }

                throw;
            }
        }
Exemple #12
0
        private bool BucketExists(S3BucketKeyInfo bki)
        {
            if (string.IsNullOrEmpty(bki.BucketName))
            {
                return(false);
            }

            try
            {
                var bucketLocation = S3Client.GetBucketLocation(new GetBucketLocationRequest {
                    BucketName = bki.BucketName
                });
                return(true);
            }
            catch (AmazonS3Exception s3x)
            {
                if (IsMissingObjectException(s3x))
                {
                    return(false);
                }

                throw;
            }
        }
Exemple #13
0
        public override bool Exists(FileSystemObject fso)
        {
            var bki = new S3BucketKeyInfo(fso.FullName);

            try
            {
                var response = S3Client.GetObjectMetadata(new GetObjectMetadataRequest
                {
                    BucketName = bki.BucketName,
                    Key        = bki.Key
                });
                return(true);
            }
            catch (AmazonS3Exception ex)
            {
                if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    return(false);
                }

                //status wasn't not found, so throw the exception
                throw;
            }
        }
Exemple #14
0
        public override void Delete(FileSystemObject fso)
        {
            var bki = new S3BucketKeyInfo(fso);

            Delete(bki);
        }