简体   繁体   中英

How to check if Amazon S3 bucket is empty

I'd like to check if an S3 bucket is empty using .NET. Particularly, I'm looking to check if a particular 'folder' is empty in the bucket, even though I know technically folders don't exist with S3.

For example:

BucketName1 > in > archived...with a sub-directory 'in' with a sub-directory 'archive' inside 'in'.

I'd like to check the 'in' folder to see if there are any files residing in it, and if not do something.

So my code looks something like

                // List all objects
                ListObjectsRequest listRequest = new ListObjectsRequest
                {
                    BucketName = "BucketName1",
                    Prefix = job + "/in/",
                };

                ListObjectsResponse listResponse;
                // Get a list of objects
                listResponse = client.ListObjects(listRequest);

How do I check if just the 'in' folder contains no objects?

  • IsBucketEmpty : Request for Prefix=null and delimiter=null - Check if listResponse's S3 Object Count is zero that's means Bucket is empty

  • IsFolderEmpty (Virtual Folder: S3 does not have folder) : Request for Prefix="Folder" and delimiter=null - Check if listResponse's S3 Object Count is zero thats means Folder having prefix is empty or file(s) having prefix does not exist.

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