Nutritioustim
Nutritioustim

Reputation: 2738

AWS S3: Howto maintain empty directory structure after deleting last file

For the AWS SDK for Java library (used by cognitect-labs/aws-api), if I delete the last object in a "directory", I'd expect to be left with an empty directory.

https://aws.amazon.com/sdk-for-java/ https://github.com/cognitect-labs/aws-api

Instead, DeleteObject deletes the directory as well (not what I want). Is there a way to avoid this? https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObject.html

(require '[cognitect.aws.client.api :as aws])

;; Copying works fine
(aws/invoke aws/client
            {:op :CopyObject
             :request {:Bucket "bucket"
                       :CopySource "bucket/dirA/f1"
                       :Key "dirB/f1"}})

;; However, deleting removes f1 and dirA (if dirA is empty)
(aws/invoke aws/client
            {:op :DeleteObject
             :request {:Bucket "bucket"
                       :Key "dirA/f1"}})

Upvotes: 1

Views: 1076

Answers (2)

wilmol
wilmol

Reputation: 1900

S3 is smart enough to know when a folder was created with an object. So when the object is deleted, the folder is deleted too. Otherwise you'd have empty folders dangling in the bucket.

However - when I was testing this, I found that if you created the folder separately before creating the objects in the folder, DeleteObject preserves the empty folder. This wasn't the case for DeleteObjects.

Upvotes: 0

Arun Kamalanathan
Arun Kamalanathan

Reputation: 8583

It is not possible, because the folders you see in S3 are not really folders. It is organised as folders for easy. But it's not really folders.

Upvotes: 2

Related Questions