Tech Blog

Coding

Check if a folder exists in S3 through C#

By Vidhathri Avadhany2 min read
← Back to blog

To check if a folder exists in a bucket is one of the basic requirements in any application which uses S3 buckets. I have demonstrated a small function in this blog, which checks if a given folder exists in S3.

This function takes ‘Key’ as input. ‘Key’ here is the folder path in string format, excluding the bucket name. This function returns a bool value.

You can just pass the key(For example if ‘Test’ is an S3 bucket and there is a folder ‘InTest’ in the bucket, if you have to check if ‘InTest’ exists, the key is just ‘InTest’) to the function and it returns true if the key exists else returns false. Specify the ‘AwsAccessKey’, ‘AwsSecretAccessKey’ and the ‘BucketName’ in the appropriate places in the function below.

To implement this function, you have to install a NuGet package called AWSSDK.S3

using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Threading.Tasks;

namespace CheckS3
{
    public static class S3Utility
    {
        public static async Task<bool> DoesFolderexists(string Key)
        {
            ListObjectsResponse response = null;
            try
            {
                using (AmazonS3Client client = new AmazonS3Client("<AwsAccessKey>", "<AwsSecretAccessKey>", Amazon.RegionEndpoint.USEast1))
                {
                    ListObjectsRequest request = new ListObjectsRequest
                    {
                        Bucket Name = "<bucket-name>",
                        Prefix = Key
                    };
                    response = await client.ListObjectsAsync(request);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return (response != null && response.S3Objects != null && response.S3Objects.Count > 0);
        }
    }
}

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 →