fraXis fraXis - 11 months ago 129
C# Question

Determine if an object exists in a S3 bucket based on wildcard

Can someone please show me how to determine if a certain file/object exists in a S3 bucket and display a message if it exists or if it does not exist.

Basically I want it to:

1) Check a bucket on my S3 account such as testbucket

2) Inside of that bucket, look to see if there is a file with the prefix test_ (test_file.txt or test_data.txt).

3) If that file exists, then display a MessageBox (or Console message) that the file exists, or that the file does not exist.

Can someone please show me how to do this?

Answer Source

Using the AWSSDK For .Net I Currently do something along the lines of:

public bool Exists(string fileKey, string bucketName)
            response = _s3Client.GetObjectMetadata(new GetObjectMetadataRequest()

            return true;

        catch (Amazon.S3.AmazonS3Exception ex)
            if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
                return false;

            //status wasn't not found, so throw the exception

It kinda sucks, but it works for now.