Say I have the following directories and files in an Amazon S3 bucket (files are in bold):
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:
...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)
{
/* ... */
}
}
Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that contains the object. In the Objects list, choose the name of the object for which you want an overview.
An S3 bucket can be accessed through its URL. The URL format of a bucket is either of two options: http://s3.amazonaws.com/[bucket_name]/ http://[bucket_name].s3.amazonaws.com/
Restrict access to your S3 resources. By default, all S3 buckets are private and can be accessed only by users who are explicitly granted access. Restrict access to your S3 buckets or objects by doing the following: Writing IAM user policies that specify the users that can access specific buckets and objects.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With