Alex Rivera | Logout

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

Asked 2010-08-19T21:53:00.693
57

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?

Edit
Report

1 Answer

74

Use the S3FileInfo.Exists method:

using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey))
{
    S3FileInfo s3FileInfo = new Amazon.S3.IO.S3FileInfo(client, "your-bucket-name", "your-file-name");
    if (s3FileInfo.Exists)
    {
         // file exists
    }
    else
    {
        // file does not exist
    }   
}
answered 2013-04-19T10:31:24.177

Your Answer