Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a folder from Amazon S3 using API

I am trying to delete all the files inside a folder which is basically the date.

Suppose, if there are 100 files under folder "08-10-2015", instead of sending all those 100 file names, i want to send the folder name.

I am trying below code and it is not working for me.

        DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest();

        multiObjectDeleteRequest.BucketName = bucketName;

        multiObjectDeleteRequest.AddKey(keyName + "/" + folderName + "/");


        AmazonS3Config S3Config = new AmazonS3Config()
        {
            ServiceURL = string.Format(servicehost)
        };

        using (IAmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accesskey, secretkey, S3Config))
        {
            try
            {
                DeleteObjectsResponse response = client.DeleteObjects(multiObjectDeleteRequest);
                Console.WriteLine("Successfully deleted all the {0} items", response.DeletedObjects.Count);

            }
            catch (DeleteObjectsException e)
            {
                // Process exception.
            }

I am using the above code and it is not working.

like image 225
Viki888 Avatar asked Oct 08 '15 13:10

Viki888


2 Answers

I think you can delete the entire folder using the following code:

 AmazonS3Config cfg = new AmazonS3Config();
 cfg.RegionEndpoint = Amazon.RegionEndpoint.EUCentral1;
 string bucketName = "your bucket name";
 AmazonS3Client s3Client = new AmazonS3Client("your access key", "your secret key", cfg);
 S3DirectoryInfo directoryToDelete = new S3DirectoryInfo(s3Client, bucketName, "your folder name or full folder key");
 directoryToDelete.Delete(true); // true will delete recursively in folder inside

I am using amazon AWSSDK.Core and AWSSDK.S3 version 3.1.0.0 for .net 3.5. I hope it can help you

like image 52
Luis Fernando Camacho Camacho Avatar answered Oct 06 '22 18:10

Luis Fernando Camacho Camacho


You have to:

  1. List all objects in the folder
  2. Retrieve key for each object
  3. Add this key to a multiple Delete Object Request
  4. Make the request to delete all objects

     AmazonS3Config S3Config = new AmazonS3Config()
        {
            ServiceURL = "s3.amazonaws.com",
            CommunicationProtocol = Amazon.S3.Model.Protocol.HTTP,
        };
    
     const string AWS_ACCESS_KEY = "xxxxxxxxxxxxxxxx";
     const string AWS_SECRET_KEY = "yyyyyyyyyyyyyyyy";            
     AmazonS3Client client = new AmazonS3Client(AWS_ACCESS_KEY, AWS_SECRET_KEY, S3Config);
    
        DeleteObjectsRequest request2 = new DeleteObjectsRequest();
        ListObjectsRequest request = new ListObjectsRequest
        {
            BucketName = "yourbucketname",
            Prefix = "yourprefix"
    
        };
    
        ListObjectsResponse response = await client.ListObjectsAsync(request);
                // Process response.
                foreach (S3Object entry in response.S3Objects)
                {
    
                    request2.AddKey(entry.Key);
                }
        request2.BucketName = "yourbucketname";
        DeleteObjectsResponse response2 = await client.DeleteObjectsAsync(request2);
    
like image 42
fxtrade Avatar answered Oct 06 '22 17:10

fxtrade