Exemplo n.º 1
0
 static void WipeBucket(ControllerConfiguration context)
 {
     using (var client = new Amazon.S3.AmazonS3Client(context.AwsAccessKeyId, context.AwsSecretAccessKey))
     {
         int batchSize = 100;
         int count = batchSize;
         while (count == batchSize)
         {
             using (var listResponse = client.ListObjects(new Amazon.S3.Model.ListObjectsRequest()
             {
                 BucketName = context.BucketName,
                 MaxKeys = batchSize,
             }))
             {
                 count = listResponse.S3Objects.Count;
                 Parallel.ForEach(listResponse.S3Objects, s3obj =>
                     {
                         using (var delResponse = client.DeleteObject(new Amazon.S3.Model.DeleteObjectRequest()
                         {
                             BucketName = context.BucketName,
                             Key = s3obj.Key,
                         })) { }
                     });
             }
         }
     }
 }
Exemplo n.º 2
0
        public static void CleanBucket(string accessKey, string secretKey, string bucket, TimeSpan t)
        {
            using (var client = new Amazon.S3.AmazonS3Client(accessKey, secretKey, RegionEndpoint.USEast1)) {
                string marker = null;
                while (true)
                {
                    var loReq = new ListObjectsRequest()
                    {
                        Marker = marker, BucketName = bucket
                    };
                    var loRes = client.ListObjects(loReq);

                    foreach (var o in loRes.S3Objects)
                    {
                        if (o.LastModified <= DateTime.Now.Subtract(t))
                        {
                            client.DeleteObject(bucket, o.Key);
                            Console.WriteLine("Deleted " + o.Key);
                        }
                    }

                    if (loRes.IsTruncated)
                    {
                        marker = loRes.NextMarker;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
Exemplo n.º 3
0
        public List <string> ListItems(string bucketName, string serverFolder, int?maxItems = null)
        {
            //ref: http://docs.aws.amazon.com/AmazonS3/latest/dev/RetrievingObjectUsingNetSDK.html

            List <string> listRest = new List <string>();
            int           count    = 0;

            var region = Amazon.RegionEndpoint.GetBySystemName(this.Credential.Region);

            using (var client = new Amazon.S3.AmazonS3Client(this.Credential.AcesssKey, this.Credential.SecretKey, region))
            {
                var request = new Amazon.S3.Model.ListObjectsRequest
                {
                    BucketName = bucketName,
                    MaxKeys    = 10,
                    Prefix     = serverFolder
                };

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

                    // Process response
                    foreach (Amazon.S3.Model.S3Object entry in response.S3Objects)
                    {
                        if (entry.Key == serverFolder || entry.Key == string.Format("{0}/", serverFolder) || entry.Key == string.Format("/{0}", serverFolder))
                        {
                            continue; //Folder
                        }
                        count++;

                        System.Diagnostics.Debug.WriteLine("AwsS3 -- key = {0} size = {1} / {2} items read", entry.Key, entry.Size.ToString("#,##0"), count.ToString("#,##0"));
                        listRest.Add(entry.Key);
                    }

                    // If response is truncated, set the marker to get the next
                    // set of keys.
                    if (response.IsTruncated)
                    {
                        request.Marker = response.NextMarker;
                    }
                    else
                    {
                        request = null;
                    }

                    if (maxItems.HasValue && count >= maxItems.Value)
                    {
                        break;
                    }
                } while (request != null);
            }

            return(listRest);
        }