Say I have the following directories and files in an Amazon S3 bucket (files are in bold):
- bucketname/
- bucketname/folder1/
- bucketname/folder1/foobar.txt
- bucketname/folder1/subfolder1/
- bucketname/folder1/subfolder1/hello.txt
- bucketname/folder1/subfolder2/
- bucketname/folder1/subfolder2/world.txt
- bucketname/folder1/subfolder2/subsubfolder1/
- bucketname/folder1/subfolder2/subsubfolder1/file.txt
How can I list all objects and immediate subdirectories of a given directory with the .NET AWS S3 API, without recursively getting everything below that directory? In other words, how can I "browse" the contents of a directory at a single level?
For example, imagine I want to browse the contents of bucketname/folder1/
. What I would like to see is the following:
- bucketname/folder1/foobar.txt
- bucketname/folder1/subfolder1/
- bucketname/folder1/subfolder2/
...and nothing else. I don't want to list the files and directories in subdirectories, I just want to list the files and subdirectories at the folder1
level.
Is there a way to apply filters to a single AWS API call so that it doesn't return everything and force me to manually parse only what I need?
I've found that this code let's me get just the immediate subdirectories (as intended), but I can't figure out how to include the immediate files too:
var request = new ListObjectsRequest()
.WithBucketName("bucketname")
.WithPrefix(@"folder1/")
.WithDelimiter(@"/");
using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey))
using (var response = client.ListObjects(request))
{
foreach (var item in response.CommonPrefixes)
{
/* ... */
}
}