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 override Result Execute(ConDepSettings settings, CancellationToken token)
        {
            var dynamicAwsConfig = settings.Config.OperationsConfig.Aws;

            using (var client = new Amazon.S3.AmazonS3Client(GetAwsCredentials(dynamicAwsConfig), RegionEndpoint.GetBySystemName((string)dynamicAwsConfig.Region)))
            {
                Logger.Verbose("Trying to get object metadata from S3");
                try
                {
                    var obj = client.GetObjectMetadata(_bucket, _key);
                    if (obj.HttpStatusCode != HttpStatusCode.OK)
                    {
                        Logger.Verbose("Failed to get Amazon S3 Object metadata. Http status code was {0}", obj.HttpStatusCode);
                        Logger.Info("Could not find Amazon S3 Object {0} in bucket {1}- assuming allready deleted.", _key, _bucket);
                        return(Result.SuccessUnChanged());
                    }
                }
                catch (Exception ex)
                {
                    Logger.Verbose("Failed to get Amazon S3 Object metadata. Exception was:");
                    Logger.Verbose(ex.Message);
                    Logger.Warn("Exception during Amazon S3 Object lookup. Assuming object allready deleted.");
                    return(Result.Failed());
                }

                Logger.Info("Deleting Amazon S3 Object with key {0} in Bucket {1}", _key, _bucket);
                client.DeleteObject(_bucket, _key);
                return(Result.SuccessChanged());
            }
        }
Exemplo n.º 3
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.º 4
0
        /// <summary>
        /// Deletes a file from s3.
        /// </summary>
        /// <param name="keyName">Name of the key.</param>
        public void DeleteFromS3(string keyName)
        {
            var deleteObjectRequest =
                new Amazon.S3.Model.DeleteObjectRequest
            {
                BucketName = s3Settings.BucketName,
                Key        = keyName
            };

            s3Client.DeleteObject(deleteObjectRequest);
        }
Exemplo n.º 5
0
        //public void SetACL(string fileKey, bool anonymouseReadAccess)
        //{
        //    SetACLRequest aclRequest = new SetACLRequest();
        //    aclRequest.Key = fileKey;
        //    aclRequest.BucketName = AWAPI_File_AmazonS3_BucketName;

        //    S3AccessControlList aclList = new S3AccessControlList();

        //    Owner owner = new Owner();
        //    owner.Id = "oyesil";
        //    owner.DisplayName = "";
        //    aclList.Owner = owner;

        //    if (anonymouseReadAccess)
        //    {
        //        S3Grantee grantPublicRead = new S3Grantee();
        //        grantPublicRead.URI = " http://acs.amazonaws.com/groups/global/AllUsers";
        //        aclList.AddGrant(grantPublicRead, S3Permission.READ);
        //    }

        //    //Authenticated user read access
        //    S3Grantee grantAuthenticatedRead = new S3Grantee();
        //    grantAuthenticatedRead.URI = " http://acs.amazonaws.com/groups/global/AuthenticatedUsers";
        //    aclList.AddGrant(grantAuthenticatedRead, S3Permission.READ);

        //    aclRequest.ACL = aclList;


        //    Amazon.S3.AmazonS3Client client = new Amazon.S3.AmazonS3Client(AWAPI_File_AmazonS3_AccessKey, AWAPI_File_AmazonS3_SecretKey);
        //    SetACLResponse aclResponse = client.SetACL(aclRequest);

        //    client.Dispose();
        //}

        /// <summary>
        /// Deletes file from s3
        /// </summary>
        /// <returns></returns>
        public bool Delete(string fileUrl)
        {
            try
            {
                string key = GetKeyNameFromUrl(fileUrl);
                Amazon.S3.AmazonS3Client            client = new Amazon.S3.AmazonS3Client(AWAPI_File_AmazonS3_AccessKey, AWAPI_File_AmazonS3_SecretKey);
                Amazon.S3.Model.DeleteObjectRequest req    = new Amazon.S3.Model.DeleteObjectRequest();
                req.BucketName = AWAPI_File_AmazonS3_BucketName;
                req.Key        = key;

                client.DeleteObject(req);
                return(true);
            }
            catch (Exception)
            {
            }
            return(false);
        }
Exemplo n.º 6
0
        public void DeleteItem(string bucketName, string keyName)
        {
            //ref: http://docs.aws.amazon.com/AmazonS3/latest/dev/DeletingOneObjectUsingNetSDK.html

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

            using (var client = new Amazon.S3.AmazonS3Client(this.Credential.AcesssKey, this.Credential.SecretKey, region))
            {
                var deleteObjectRequest = new Amazon.S3.Model.DeleteObjectRequest
                {
                    BucketName = bucketName,
                    Key        = keyName
                };

                client.DeleteObject(deleteObjectRequest);

                Log(string.Format("AwsS3 -- Deleted {0}", keyName));
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Deletes file from s3
        /// </summary>
        /// <returns></returns>
        public bool Delete(string fileUrl)
        {
            try
            {
                string key        = fileUrl.Replace(BaseURL, "");
                string bucketName = GetBucketNameFromUrl(fileUrl);

                Amazon.S3.AmazonS3Client client = new Amazon.S3.AmazonS3Client(ConfigurationLibrary.Config.fileAmazonS3AccessKey,
                                                                               ConfigurationLibrary.Config.fileAmazonS3SecreyKey);
                Amazon.S3.Model.DeleteObjectRequest req = new Amazon.S3.Model.DeleteObjectRequest();
                req.BucketName = bucketName;
                req.Key        = key;

                client.DeleteObject(req);
                return(true);
            }
            catch (Exception)
            {
            }
            return(false);
        }