C# List all files with filename under an amazon S3 folder

37,287

Solution 1

Also you can use the following c# code to retrieve the files information.

var bucketName = "your bucket";
var s3Client = new AmazonS3Client("your access key", "your secret key");
var dir = new S3DirectoryInfo(s3Client, bucketName, "your AmazonS3 folder name");
foreach (IS3FileSystemInfo file in dir.GetFileSystemInfos())
{
    Console.WriteLine(file.Name);
    Console.WriteLine(file.Extension);
    Console.WriteLine(file.LastWriteTime);
}

I am using amazon AWSSDK.Core and AWSSDK.S3 version 3.1.0.0 for .net 3.5. I hope it can help you

Solution 2

AmazonS3Client s3Client = new AmazonS3Client(S3_ACCESS_KEY, S3_SECRET_ACCESS_KEY, S3_REGIAO);
var lista = s3Client.ListObjectsAsync(S3_BUCKET, $"{S3_SUBDIRETORIO}/{produto}/{produto}.").Result;
var files = lista.S3Objects.Select(x => x.Key);
var arquivos = files.Select(x => Path.GetFileName(x)).ToList();

Solution 3

    AmazonS3Client client = new AmazonS3Client();
    ListObjectsRequest listRequest = new ListObjectsRequest
    {
        BucketName = "your-bucket-name",
        Prefix = "example/path"
    };

    ListObjectsResponse listResponse;
    do
    {
        listResponse = client.ListObjects(listRequest);
        foreach (S3Object obj in listResponse.S3Objects)
        {
            Console.WriteLine(obj.Key);
            Console.WriteLine(" Size - " + obj.Size);
        }

        listRequest.Marker = listResponse.NextMarker;
    } while (listResponse.IsTruncated);

https://docs.aws.amazon.com/sdkfornet1/latest/apidocs/html/M_Amazon_S3_AmazonS3_ListObjects.htm

Solution 4

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

Share:
37,287
Jameel
Author by

Jameel

Updated on August 24, 2021

Comments

  • Jameel
    Jameel almost 3 years

    Using C# and amazon .Net SDK, able to list all the files with in a amazon S3 folder as below:

    ListObjectsRequest request = new ListObjectsRequest();           
    request.BucketName = _bucketName; //Amazon Bucket Name
    request.Prefix = _sourceKey; //Amazon S3 Folder path           
    do
    {
        ListObjectsResponse response = _client.ListObjects(request);//_client - AmazonS3Client
    

    Output

    Folder
    Folder/file1.pdf
    Folder/file2.pdf
    Folder/file3.pdf
    

    But i wanted to achieve something like this:

    Desired Output

    file1.pdf
    file2.pdf
    file3.pdf
    

    Thanks in advance