Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete files, directories and buckets in amazon s3 java

Tags:

I'm wondering how to do this. I looked at the sdk documentation and have some examples, but am confused how the syntax generally goes.

If I want to delete a file, I assume I use deleteObject(path, key). However, what is the "key"?

Also how do you delete a directory? I can't seem to find a method for doing that.

like image 600
locoboy Avatar asked Oct 14 '11 05:10

locoboy


People also ask

How do I delete files from Amazon S3?

If you no longer need to store the file you've uploaded to your Amazon S3 bucket, you can delete it. Within your S3 bucket, select the file that you want to delete, choose Actions, and then choose Delete. In the confirmation message, choose OK.

How will you delete a bucket that has multiple files and folders in it?

To delete multiple files from an S3 Bucket with the AWS CLI, run the s3 rm command, passing in the exclude and include parameters to filter the files the command is applied to. Let's run the command in test mode first.

What is the fastest way to delete S3 Buckets?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, select the option next to the name of the bucket that you want to delete, and then choose Delete at the top of the page.

How do I delete multiple S3 Buckets at once?

To delete multiple S3 objects using a single HTTP request, you can use the AWS CLI, or an AWS SDK. To empty an S3 bucket of its objects, you can use the Amazon S3 console, AWS CLI, lifecycle configuration rule, or AWS SDK.


2 Answers

This snippet of code works for me. folderPath is something like "topDir/secondDir/"

void deleteObjectsInFolder(String bucketName, String folderPath) {
   for (S3ObjectSummary file : s3.listObjects(bucketName, folderPath).getObjectSummaries()){
      s3.deleteObject(bucketName, file.getKey());
    }
}
like image 51
Danger Avatar answered Sep 27 '22 18:09

Danger


A "key" in S3 is similar to a file path:

http://bucket.s3.amazonaws.com/some/path/to/use

... is in a bucket named bucket and has a key of some/path/to/use.

It's not actually a path though, because there are no folders. The S3 key is just the file name for a file in one big directory (the entire bucket). S3 keys can contain /, but it has no special meaning unless you set the delimiter argument with listing a bucket.

In other words, having an object named some/object doesn't tell you anything about the object some (it might or might not exist -- the two objects are not related).

However, you can request keys with a specific prefix, so I could say "give me all keys starting with some/path/to/ and it will return some/path/to/use. It looks like "listing a directory", but it's really just asking for files that start with a specific string of characters.

I could just as easily name things like this:

somepathtousea
somepathtouseb

And say "give me everything starting with somepathtouse" (and it would say somepathtousea and somepathtouseb).

Note: S3 URL's come in several forms:

http://s3.amazonaws.com/bucket/key
http://bucket.s3.amazonaws.com/key
http://bucket/key (where bucket is a DNS CNAME record pointing to bucket.s3.amazonaws.com)

EDIT:

I looked at the JavaDocs and this is the function signature I see (for AmazonS3Client):

public void deleteObject(java.lang.String bucketName,
                         java.lang.String key)
                  throws AmazonClientException,
                         AmazonServiceException

EDIT again:

Folders do kind-of exist now, as zero-length objects with a content-type of application/x-directory and a key ending in /:

$ AWS_PROFILE=prod aws s3api head-object --bucket example-bucket --key example-directory/
{
    "AcceptRanges": "bytes",
    "LastModified": "Mon, 29 Apr 2019 14:59:36 GMT",
    "ContentLength": 0,
    "ETag": "\"d41d8cd98f00b204e9800998ecf8427e\"",
    "ContentType": "application/x-directory",
    "ServerSideEncryption": "AES256",
    "Metadata": {}
}

This is still just convention and there's nothing stopping you from having files ending / or files inside of "folders" that don't exist.

like image 29
Brendan Long Avatar answered Sep 27 '22 19:09

Brendan Long