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” 🙂
Categories: Uncategorized
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);