Say I have the following directories and files in an Amazon S3 bucket (files are in bold):
I had the opposite problem (I knew how to get the files in the specified folder, but not the subdirectories).
The answer is that Amazon lists files differently than it does sub-folders.
Sub-folders are listed, as your example shows, in the ListObjectsResponse.CommonPrefixes collection.
Files are listed in the ListObjectsResponse.S3Objects collection.
So your code should look like this:
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 subFolder in response.CommonPrefixes)
{
/* list the sub-folders */
}
foreach (var file in response.S3Objects) {
/* list the files */
}
}
my google search turned up this post on the burningmonk blog with this in the comment section:
When you make the ListObjects request, to list the top level folders, don’t set the prefix but set the delimiter to ‘/’, then inspect the ‘CommonPrefixes’ property on the response for the folders that are in the top folder.
To list the contents of a ‘rootfolder’, make the request with prefix set to the name of the folder plus the backslash, e.g. ‘rootfolder/’ and set the delimiter to ‘/’. In the response you’ll always have the folder itself as an element with the same key as the prefix you used in the request, plus any subfolders in the ‘CommonPrefixes’ property.