简体   繁体   中英

Amazon S3 delete subfolder and it's contents C#

Suppose I have the following hierarchy in an S3 bucket.

Bucket
   DeptFolder
        Folder1
           Folder2
              -file1
              -file2
              -file3

I want to delete Folder2 and any files underneath it so it ends up looking like this...

Bucket
   DeptFolder
        Folder1

I run the following code and the delete is occurring at Folder1 not Folder2.

using (var s3Client = new AmazonS3Client(myAccessKey,mySecretAccessKey, config))
                {

                        S3DirectoryInfo directoryToDelete = new S3DirectoryInfo(s3Client, myBucket, "DeptFolder/Folder1/Folder2");
                        directoryToDelete.Delete(true); 

                }

What I end up with is this...

Bucket
    DeptFolder

Can anyone tell me where I might be going wrong? Thanks!

Amazon S3 is an object storage system, not a file system.

Folders do not actually exist!

For example, you can create an object called DeptFolder/Folder1/Folder2/foo and the object will be created in that path, even if the folders do not exist ! It will appear that the folders are there, but they are merely created as a convenience.

If the object was then deleted, the folders would disappear too because they don't actually exist.

I think this is what is happening to you... There are no objects in Folder1 , so when Folder1/Folder2 is deleted, Folder1 also disappears because there is nothing in it to make it "appear".

Bottom line: Don't think of S3 as a normal file system. Don't get worried about what directories/folders exist. Just store files where you want and it will work fine. Empty folders (generally) don't exist. Don't panic!

Some people do panic, so they create empty (zero-length) files named the same as the folders, which forces the folder to appear. You can do that, but it's best to just accept the way that S3 works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM