IamCalvinBrown.com a Blog Site

Code Examples, Consulting Advice and lots of other cool things

Check to see if an S3 object exists

In Amazon S3 we sometimes need to check for the existence of an object (picture, file, etc) before taking the next actions. Here's how we do it in C#. If we find anything that matches, it brings it bk in a list.

Note: The variables App.awsKey & App.awsSecretyKey are my global variables in my Xamarin forms app. You should replace these with the text values for your key and secretKey from your AWS account.

Note: Prefix = key (key is just a string -- the name of the object you want to find)

Amazon.S3.AmazonS3Client s3Client = new Amazon.S3.AmazonS3Client(App.awsKey, App.awsSecretKey, Amazon.RegionEndpoint.USEast1);


            var request = new ListObjectsRequest

            {

                Prefix = key,

                MaxKeys = 1

            };

            ListObjectsResponse response = await s3Client.ListObjectsAsync(request);

            if (response.S3Objects.Count == 0)

            {

                return false;

            }

            return true;

Loading