コード例 #1
0
ファイル: S3.cs プロジェクト: uqayyum/aws_suite
        public void DeleteFromS3(S3File file, AWS_Credentials credentials)
        {
            if (file is null || file.RemoteFilePath is null || file.Bucket is null)
            {
                throw new Exception("S3 File is NULL or has some NULL attributes");
            }

            if (credentials is null || credentials.AWS_AccessKey is null || credentials.AWS_SecretKey is null || credentials.Region is null)
            {
                throw new CredentialsNotProvidedException();
            }

            try
            {
                using (AmazonS3Client client = getS3Client(credentials))
                {
                    DeleteObjectRequest request = new
                                                  DeleteObjectRequest();

                    request.BucketName = file.Bucket;
                    request.Key        = file.RemoteFilePath;

                    Task <DeleteObjectResponse> task = client.DeleteObjectAsync(request);
                    task.Wait();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #2
0
ファイル: S3.cs プロジェクト: uqayyum/aws_suite
        public void DeleteBucket(string bucket_name, AWS_Credentials credentials)
        {
            if (credentials is null || credentials.AWS_AccessKey is null || credentials.AWS_SecretKey is null || credentials.Region is null)
            {
                throw new CredentialsNotProvidedException();
            }

            if (bucket_name is null)
            {
                throw new ArgumentNullException();
            }

            using (AmazonS3Client client = getS3Client(credentials))
            {
                List <string> existing_buckets = GetAllBuckets(credentials);
                try
                {
                    if (!(existing_buckets.Where(b => b.ToString() == bucket_name).FirstOrDefault() is null))
                    {
                        var request = new DeleteBucketRequest
                        {
                            BucketName      = bucket_name,
                            UseClientRegion = true
                        };

                        Task <DeleteBucketResponse> task = client.DeleteBucketAsync(request);
                        task.Wait();
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }
コード例 #3
0
ファイル: S3.cs プロジェクト: uqayyum/aws_suite
        public List <string> GetAllBuckets(AWS_Credentials credentials)
        {
            if (credentials is null || credentials.AWS_AccessKey is null || credentials.AWS_SecretKey is null || credentials.Region is null)
            {
                throw new CredentialsNotProvidedException();
            }

            List <string> buckets = new List <string>();

            using (AmazonS3Client client = getS3Client(credentials))
            {
                ListBucketsRequest bucketFetchUtility = new
                                                        ListBucketsRequest();

                Task <ListBucketsResponse> bucketsResponse = client.ListBucketsAsync(bucketFetchUtility);
                bucketsResponse.Wait();

                if (bucketsResponse.IsCompleted)
                {
                    foreach (S3Bucket bucket in bucketsResponse.Result.Buckets)
                    {
                        buckets.Add(bucket.BucketName);
                    }
                }
            }
            return(buckets);
        }
コード例 #4
0
ファイル: S3.cs プロジェクト: uqayyum/aws_suite
        public void DownloadFromS3(S3File file, AWS_Credentials credentials)
        {
            if (file is null || file.RemoteFilePath is null || file.Bucket is null || file.LocalFilePath is null)
            {
                throw new Exception("S3 File is NULL or has some NULL attributes");
            }

            if (credentials is null || credentials.AWS_AccessKey is null || credentials.AWS_SecretKey is null || credentials.Region is null)
            {
                throw new CredentialsNotProvidedException();
            }

            try
            {
                TransferUtility fileTransferUtility = new
                                                      TransferUtility(getS3Client(credentials));

                TransferUtilityDownloadRequest fileTransferUtilityRequest = new TransferUtilityDownloadRequest
                {
                    BucketName = file.Bucket,
                    FilePath   = file.LocalFilePath,
                    Key        = file.RemoteFilePath,
                };
                fileTransferUtility.Download(fileTransferUtilityRequest);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #5
0
ファイル: S3.cs プロジェクト: uqayyum/aws_suite
        public List <string> GetBucketKeys(string bucket_name, AWS_Credentials credentials, string prefix = null, long max_count = 100_000)
        {
            if (credentials is null || credentials.AWS_AccessKey is null || credentials.AWS_SecretKey is null || credentials.Region is null)
            {
                throw new CredentialsNotProvidedException();
            }

            if (bucket_name is null)
            {
                throw new ArgumentNullException();
            }

            List <string> keys = new List <string>();

            try
            {
                using (var client = new AmazonS3Client(credentials.AWS_AccessKey, credentials.AWS_SecretKey, credentials.Region))
                {
                    ListObjectsV2Request request = new ListObjectsV2Request();
                    request.BucketName = bucket_name;
                    request.Prefix     = prefix;

                    if (max_count > 0 && max_count < 1000)
                    {
                        request.MaxKeys = (int)max_count;
                    }

                    Task <ListObjectsV2Response> task;
                    ListObjectsV2Response        response;
                    long iterator = 0;
                    do
                    {
                        task = client.ListObjectsV2Async(request);
                        task.Wait();

                        response = task.Result;
                        keys.AddRange(response.S3Objects.Select(x => x.Key));
                        iterator += response.S3Objects.Count;
                        request.ContinuationToken = response.NextContinuationToken;
                    } while (response.IsTruncated == true && ((max_count == -1) ? true : iterator < max_count));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(keys);
        }
コード例 #6
0
ファイル: S3.cs プロジェクト: uqayyum/aws_suite
        public AmazonS3Client getS3Client(AWS_Credentials credentials)
        {
            if (credentials is null || credentials.AWS_AccessKey is null || credentials.AWS_SecretKey is null || credentials.Region is null)
            {
                throw new CredentialsNotProvidedException();
            }

            try
            {
                return(new AmazonS3Client(credentials.AWS_AccessKey, credentials.AWS_SecretKey, credentials.Region));
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }
コード例 #7
0
ファイル: S3.cs プロジェクト: uqayyum/aws_suite
 public S3(AWS_Credentials credentials)
 {
     Credentials = credentials ?? throw new ArgumentNullException(nameof(credentials));
 }
コード例 #8
0
ファイル: S3.cs プロジェクト: uqayyum/aws_suite
 public S3()
 {
     this.Credentials = new AWS_Credentials();
 }