Tech Blog

Coding

Fetch more than 1000 objects from S3 using C#

By Vidhathri Avadhany1 min read
← Back to blog

Hello!
Have you worked with S3? If you have, you know that we can’t get more than 1000 objects in a response from S3. So, the next question is, what do we do if we have more objects in S3?
Go ahead and read to know!

I have used ListObjectsAsync() here. See the following code.
‘bucket’ here refers to S3 bucket, and ‘key’ is the folder path inside the bucket(excluding the bucket name)

public static async Task<List<S3Object>> FetchS3ObjectsAsync(string bucket, string key)
{
    ListObjectsResponse response = null;
    List<S3Object> s3Objects = new List<S3Object>();
    using (AmazonS3Client client = new AmazonS3Client(<AwsAccessKey>, <AwsSecretAccessKey>, Amazon.RegionEndpoint.USEast1))
    {
        ListObjectsRequest request = new ListObjectsRequest
        {
            BucketName = bucket,
            Prefix = key
        };

        do
        {
            try
            {
                response = await client.ListObjectsAsync(request);

                foreach (S3Object obj in response.S3Objects)
                {
                    s3Objects.Add(obj);
                }
                if (response.IsTruncated)
                {
                    request.Marker = response.NextMarker;
                }
                else
                {
                    request = null;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        } while (request != null);
    }
    return s3Objects;
}

Keep reading

You may be interested in

Browse all articles →
Active Directory Federation Service
July 5, 20235 min read

Active Directory Federation Service

Active Directory Federation Service (ADFS) uses claims-based identity model to maintain application security and to implement federated identity. ADFS is a concept of Windows Server operating system, that provides users with single sign-on access to the application, those are located across organizational boundaries. For Example: We have 2 organizations – Organization 1 and Organization 2. […]

By Abinash MishraRead More →
Remote Access to Microsoft SQL Database Server
April 5, 20232 min read

Remote Access to Microsoft SQL Database Server

Remote access to a SQL server is important because it allows users to access the server from anywhere, at any time. This enables remote workers to access the data they need when they need it, even if they are not physically in the same location as the server. It also allows IT admins to access […]

By Malkeet SinghRead More →
Types of Load Balancer in AWS
February 6, 20235 min read

Types of Load Balancer in AWS

What is Load Balancing ? Load balancing is a critical component of any scalable and highly available Cloud or On-premise architecture. It helps distribute incoming traffic across multiple servers, improving the availability and scalability of a website or application. In the cloud, load balancing is offered as a service by various cloud providers such as […]

By Keerthik ShenoyRead More →