Let's make it clear, simple, and with a little description.
As we all know, in S3 there is no concept of directories (folders). Ah, what?
So everything inside S3 is nothing but objects.
Let's consider the below example s3 bucket - the bucket name is testBucket, the directory name is testDirectory and the directory contains two files testImage.jpg and testUserData.txt.
- testBucket
- testDirectory
- testImage.jpg
- testUserData.txt
AmazonS3Client s3Client = new AmazonS3Client();
string bucketName = "testBucket";
// to list out all the elements inside of a director [To know more][1]
string prefix = "testDirectory/";
ListObjectsRequest request = new ListObjectsRequest
{
BucketName = bucketName,
Prefix = prefix,
// Use this to exclude your directory name
StartAfter = prefix,
};
ListObjectsResponse response = s3Client.ListObjects(request);
foreach (S3Object itemsInsideDirectory in response .S3Objects)
{
Console.WriteLine(itemsInsideDirectory.Key);
}
output:
If you use 'StartAfter', then output will be
testDirectory/testImage.jpg
testDirectory/testUserData.txt*
If you donot use 'StartAfter', then output will be
testDirectory/
testDirectory/testImage.jpg
testDirectory/testUserData.txt