Home > Uncategorized > Check if file exists on AWS S3 Bucket C#

Check if file exists on AWS S3 Bucket C#

You can check if a file exists on an S3 Bucket by trying to download it, but there’s a faster way, by just requesting the metadata;

public bool S3Exists(string fileKey)
{
Amazon.Util.ProfileManager.RegisterProfile(“s3”, “XXX”,”XXXX”);
AWSCredentials credentials = new StoredProfileAWSCredentials(“s3”);
IAmazonS3 s3Client = new AmazonS3Client(credentials, RegionEndpoint.EUWest1);
var s3FileInfo = new Amazon.S3.IO.S3FileInfo(s3Client, “XXXX”,fileKey);
return s3FileInfo.Exists;
}

I’ve replaced the passwords with “XXXX” 🙂

Advertisements
Categories: Uncategorized
  1. pavelaws
    June 22, 2015 at 7:27 pm

    Good blog post. Unfortunately, your example uses Amazon.Util.ProfileManager.RegisterProfile, which is unnecessary and potentially can cause runtime problems. This blog post provides some more detail: http://blogs.aws.amazon.com/net/post/Tx3VJIHQF8Q0YSX/RegisterProfile

    Given that the application already has access to AWS access and secret keys, you can simplify your code as follows:
    var credentials = new BasicAWSCredentials(“access_key_id”, “secret_key”);
    var client = new AmazonS3Client(credentials, RegionEndpoint.EUWest1);

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: