C# List all Files from an AWS S3 Bucket

C# List all Files from an AWS S3 Bucket

Were going to continue on from the previous post that allowed us to upload a file to an S3 Bucket, and now get a list of all files within the S3 Bucket.

Amazon Contents Class

Within the Visual Studio Solution add an additional class to the UploadToS3Demo project folder, called ‘AmazonContents’.

Amazon Contents Class

Next well add the following code, the class will be constructed by passing in the bucket name, and then the list of S3 Bucket objects can be obtained via the read only property.

//AmazonContents.cs
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace TestProgram.UploadToS3Demo
{
    public class AmazonContents
    {
        string _BucketName = string.Empty;
        public IEnumerable<S3Object> BucketContents
        {
            get
            {
                if (this._BucketName != string.Empty)
                {
                    return this.listMyFilesS3Async(this._BucketName).Result;
                }
                return null;
            }
        }
        public AmazonContents(string BucketName)
        {
            this._BucketName = BucketName;
        }
        private  async Task<IList<S3Object>> listMyFilesS3Async(string bucketName)
        {
            SecretConsumer secretConsumer = new SecretConsumer();
            IAmazonS3 client = new AmazonS3Client(secretConsumer.AWSAccessKey, secretConsumer.AWSSecretKey, RegionEndpoint.EUCentral1);
            string token = null;
            var result = new List<S3Object>();
            do
            {
                ListObjectsRequest request = new ListObjectsRequest()
                {
                    BucketName = bucketName
                };
                ListObjectsResponse response = await client.ListObjectsAsync(request).ConfigureAwait(false);
                result.AddRange(response.S3Objects);
                token = response.NextMarker;
            } while (token != null);
            return result;
        }
    }
}

Testing the Application

For now we will comment out the uploading of the text file, and then add the last two lines shown below:

//Program.cs
using Amazon.S3.Model;
using System.Collections.Generic;
using TestProgram.UploadToS3Demo;
namespace TestProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            // preparing our file and directory names
            string fileToBackup = @"C:\Users\ma_je\Downloads\TestS3uploadFile.txt"; // test file
            string myBucketName = "catiawidgetsbkup"; //your s3 bucket name goes here
            string s3DirectoryName = "";
            string s3FileName = @"TestS3uploadFile.txt";
            AmazonUploader myUploader = new AmazonUploader();
            //myUploader.sendMyFileToS3(fileToBackup, myBucketName, s3DirectoryName, s3FileName);
            AmazonContents myContents = new AmazonContents(myBucketName);
            IEnumerable<S3Object> Objects = myContents.BucketContents;
        }
    }
}

When the test program is executed the IEnumerable contains all of the S3 Objects within the S3 bucket.

S3 bucket Objects

The list of S3 Objects also includes the subfolders as well as the files.

S3 Bucket Sub Folders