Skip to content

Instantly share code, notes, and snippets.

@nimacks
Created October 31, 2013 17:04
Show Gist options
  • Save nimacks/7253276 to your computer and use it in GitHub Desktop.
Save nimacks/7253276 to your computer and use it in GitHub Desktop.
AWS S3 Service Wrapper
using System.Collections.Generic;
using System.Diagnostics;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
namespace S3Navigator.AWS
{
public class AwsService
{
private readonly AmazonS3 _client;
public AwsService() { _client = AWSClientFactory.CreateAmazonS3Client(); }
/// <summary>
/// AWS S3 Service call to get a list of all Buckets
/// </summary>
/// <returns></returns>
public List<S3Bucket> GetBuckets()
{
try
{
var response = _client.ListBuckets();
if (response != null)
return response.Buckets;
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
Debug.WriteLine("Please check the provided AWS Credentials.");
}
else
{
Debug.WriteLine("An Error, number {0}, occurred when creating a bucket with the message '{1}", amazonS3Exception.ErrorCode, amazonS3Exception.Message);
}
}
return new List<S3Bucket>();
}
/// <summary>
/// AWS S3 Service call to retrieve the contents of a Bucket
/// </summary>
/// <param name="bucketName"></param>
/// <returns></returns>
public List<S3Object> GetBucketContents(string bucketName)
{
try
{
var request = new ListObjectsRequest { BucketName = bucketName };
var response = _client.ListObjects(request);
if (response != null)
return response.S3Objects;
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
Debug.WriteLine("Please check the provided AWS Credentials.");
}
else
{
Debug.WriteLine("An Error, number {0}, occurred when creating a bucket with the message '{1}", amazonS3Exception.ErrorCode, amazonS3Exception.Message);
}
}
return new List<S3Object>();
}
/// <summary>
/// AWS S3 Service call to create a Bucket
/// </summary>
/// <param name="bucketName"></param>
public void CreateBucket(string bucketName)
{
try
{
var request = new PutBucketRequest { BucketName = bucketName };
_client.PutBucket(request);
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
Debug.WriteLine("Please check the provided AWS Credentials.");
}
else
{
Debug.WriteLine("An Error, number {0}, occurred when creating a bucket with the message '{1}", amazonS3Exception.ErrorCode, amazonS3Exception.Message);
}
}
}
/// <summary>
/// AWS S3 Service call to Delete an object
/// </summary>
/// <param name="bucketName"></param>
/// <returns></returns>
public void DeleteBucket(string bucketName)
{
try
{
var request = new DeleteBucketRequest {BucketName = bucketName};
_client.DeleteBucket(request);
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
Debug.WriteLine("Please check the provided AWS Credentials.");
}
else
{
Debug.WriteLine("An Error, number {0}, occurred when creating a bucket with the message '{1}", amazonS3Exception.ErrorCode, amazonS3Exception.Message);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment