Skip to content

Instantly share code, notes, and snippets.

@ChrisMcKee
Created January 16, 2014 16:43
Show Gist options
  • Save ChrisMcKee/8458355 to your computer and use it in GitHub Desktop.
Save ChrisMcKee/8458355 to your computer and use it in GitHub Desktop.
S3 Client Extensions (IAmazonS3) Adds fluent extensions to check if an object exists, and if an object is the same as one in S3.
using System;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
public static class AmazonS3Extensions
{
public static bool ObjectExists(this IAmazonS3 client, string bucket, string key)
{
var request = new GetObjectRequest {BucketName = bucket, Key = key};
try
{
var response = client.GetObject(request);
if (response.ResponseStream != null)
{
return true;
}
}
catch (AmazonS3Exception)
{
return false;
}
catch (WebException)
{
return false;
}
catch (Exception)
{
return false;
}
return false;
}
public static bool ObjectExistsAndIsTheSame(this IAmazonS3 client, GetObjectMetadataRequest objectMetadataRequest, string metaDataFileHashKey, string fileMD5Hash)
{
try
{
var metadata = client.GetObjectMetadata(objectMetadataRequest);
if (metadata != null)
{
if (metadata.ETag != null && metadata.ETag == fileMD5Hash) return true;
}
}
catch (AmazonS3Exception)
{
return false;
}
catch (WebException)
{
return false;
}
catch (Exception)
{
return false;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment