Beispiel #1
0
        public bool FileExists(string folderName, string fileName)
        {
            //folder ignored - packages stored on top level of S3 bucket
            if (String.IsNullOrWhiteSpace(folderName))
            {
                throw new ArgumentNullException("folderName");
            }
            if (String.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentNullException("fileName");
            }

            var request = new ListObjectsRequest();

            request.WithBucketName(clientContext.BucketName);
            request.WithPrefix(fileName);

            using (AmazonS3 client = clientContext.CreateInstance())
            {
                ListObjectsResponse response = WrapRequestInErrorHandler(() => client.ListObjects(request));
                var count = response.S3Objects.Count;
                if (count == 1)
                {
                    return(true);
                }
            }

            return(false);
        }
        public bool file_exists(string folderName, string fileName)
        {
            // It's allowed to have an empty folder name.
            // if (String.IsNullOrWhiteSpace(folderName)) throw new ArgumentNullException("folderName");
            if (String.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentNullException("fileName");
            }

            folderName = (string.IsNullOrEmpty(folderName) ? String.Empty : folderName.Substring(folderName.Length - 1, 1) == "/" ? folderName : folderName + "/");
            fileName   = string.Format("{0}{1}", folderName, fileName);

            var request = new ListObjectsRequest();

            request.WithBucketName(clientContext.BucketName);
            request.WithPrefix(fileName);

            using (AmazonS3 client = clientContext.create_instance())
            {
                ListObjectsResponse response = wrap_request_in_error_handler(() => client.ListObjects(request));
                var count = response.S3Objects.Count;
                if (count == 1)
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #3
0
        protected virtual long GetFolderSize(AmazonS3 client, string folderPath, string bucketName)
        {
            ListObjectsRequest request = new ListObjectsRequest();

            request.WithBucketName(bucketName);
            request.WithPrefix(folderPath);
            long total = 0;

            do
            {
                ListObjectsResponse response = client.ListObjects(request);

                if (response != null && response.S3Objects != null)
                {
                    total += response.S3Objects.Sum(s => s.Size);
                }

                if (response.IsTruncated)
                {
                    request.Marker = response.NextMarker;
                }
                else
                {
                    request = null;
                }
            } while (request != null);

            return(total);
        }
Beispiel #4
0
        //***
        public System.Tuple <bool, List <string> > ListObjectsInBucket(AmazonS3Client client, string bucketName)
        {
            try
            {
                ListObjectsRequest request = new ListObjectsRequest();
                request.WithBucketName(bucketName);


                ListObjectsResponse response = client.ListObjects(request);

                List <string> retList = new List <string>();
                foreach (S3Object s3Object in response.S3Objects)
                {
                    retList.Add(s3Object.Key);
                }

                return(new System.Tuple <bool, List <string> >(false, retList));
            }
            catch (Exception e)
            {
                return(new System.Tuple <bool, List <string> >(false, new List <string>()
                {
                    e.ToString()
                }));
            }
        }
Beispiel #5
0
        public static List <S3Object> s3_ObjectsRaw(this API_AmazonS3 amazonS3, string bucketName)
        {
            try
            {
                amazonS3.debugMsg("[API_AmazonS3]: retriving objects list for bucket:{0}", bucketName);

                var listObjectsRequest = new ListObjectsRequest();
                listObjectsRequest.WithBucketName(bucketName);
                return(amazonS3.S3Client.ListObjects(listObjectsRequest).S3Objects);
            }
            catch (Exception ex)
            {
                ex.log("[API_AmazonS3]: in s3_ObjectsRaw()");
            }
            return(new List <S3Object>());
        }
Beispiel #6
0
        private static void ListObjects(AmazonS3 s3Client, string bucket)
        {
            var request = new ListObjectsRequest();

            request.WithBucketName(bucket)
            .WithPrefix("key")
            .WithMaxKeys(4);
            do
            {
                ListObjectsResponse response = s3Client.ListObjects(request);

                if (response.IsTruncated)
                {
                    request.Marker = response.NextMarker;
                }
                else
                {
                    request = null;
                }
            } while (request != null);
        }
Beispiel #7
0
        /// <summary>
        /// Delete a folder
        /// </summary>
        /// <param name="prefix">prefix</param>
        public void DeleteFolder(string prefix)
        {
            // Get all object with specified prefix
            var listRequest = new ListObjectsRequest();

            listRequest.WithBucketName(_bucketName).WithPrefix(prefix);

            var deleteRequest = new DeleteObjectsRequest();

            deleteRequest.BucketName = _bucketName;

            do
            {
                ListObjectsResponse listResponse = _client.ListObjects(listRequest);

                // Add all object with specified prefix to delete request.
                foreach (S3Object entry in listResponse.S3Objects)
                {
                    deleteRequest.AddKey(entry.Key);
                }

                if (listResponse.IsTruncated)
                {
                    listRequest.Marker = listResponse.NextMarker;
                }
                else
                {
                    listRequest = null;
                }
            }while (listRequest != null);

            // Delete all the object with specified prefix.
            if (deleteRequest.Keys.Count > 0)
            {
                var deleteResponse = _client.DeleteObjects(deleteRequest);
                deleteResponse.Dispose();
            }
        }
Beispiel #8
0
        //removes all files with same prefix in S3 cloud
        public static void RemoveAllObjectsWithPrefixS3(string filePath)
        {
            AmazonS3 client;

            if (CheckS3Credentials())
            {
                NameValueCollection appConfig =
                    ConfigurationManager.AppSettings;

                string accessKeyID       = appConfig["AWSAccessKey"];
                string secretAccessKeyID = appConfig["AWSSecretKey"];

                using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(
                           accessKeyID, secretAccessKeyID, RegionEndpoint.USWest1))
                {
                    try
                    {
                        //first get all objects with the same prefix (in the same "folder")
                        ListObjectsRequest listRequest = new ListObjectsRequest();
                        listRequest.WithBucketName(Constants.AmazonS3BucketName)
                        .WithPrefix(filePath);

                        ListObjectsResponse listResponse = client.ListObjects(listRequest);

                        //delete all objects
                        DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest();
                        deleteRequest.WithBucketName(Constants.AmazonS3BucketName);

                        foreach (S3Object obj in listResponse.S3Objects)
                        {
                            deleteRequest.AddKey(obj.Key);
                        }

                        //if there are any files to delete
                        if (deleteRequest.Keys.Count > 0)
                        {
                            S3Response response = client.DeleteObjects(deleteRequest);
                            response.Dispose();
                        }
                    }
                    catch (AmazonS3Exception amazonS3Exception)
                    {
                        if (amazonS3Exception.ErrorCode != null &&
                            (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                             ||
                             amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                        {
                            Console.WriteLine("Check the provided AWS Credentials.");
                            Console.WriteLine(
                                "For service sign up go to http://aws.amazon.com/s3");
                        }
                        else
                        {
                            Console.WriteLine(
                                "Error occurred. Message:'{0}' when deleting an object"
                                , amazonS3Exception.Message);
                        }
                    }
                }
            }
        }