Created
November 17, 2013 05:11
-
-
Save saintc0d3r/7509545 to your computer and use it in GitHub Desktop.
[C#][AWS][S3][MsTest] A sample of AWS S3's File Repository.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using Amazon.S3.Model; | |
using Amazon.S3.Transfer; | |
namespace GuestBook.Repository.Aws.S3 | |
{ | |
class GuestBookImageRepository : S3RepositoryBase, IGuestBookImageRepository | |
{ | |
// Adjust the BUCKET_NAME to match yours | |
public const string BUCKET_NAME = "guestbookdemo-bucket-aws"; | |
public GuestBookImageRepository() | |
: base(CommonHelpers.GetRegionEndpointFromConfig(), BUCKET_NAME) { } | |
public string UploadImageFile(string imageType, Stream imageStream) | |
{ | |
var uniqueKey = Guid.NewGuid().ToString(); | |
//var transferUtiliyConfig = new TransferUtilityConfig { DefaultTimeout = 10 * 60 * 1000 }; | |
//var transferUtility = new TransferUtility(S3, transferUtiliyConfig); | |
var transferUtility = new TransferUtility(S3); | |
var uploadRequest = new TransferUtilityUploadRequest | |
{ | |
BucketName = BUCKET_NAME, | |
Key = uniqueKey, | |
ContentType = imageType, | |
InputStream = imageStream | |
}.WithAutoCloseStream(false); | |
transferUtility.Upload(uploadRequest); | |
return uniqueKey; | |
} | |
public void Remove(string objectKey) | |
{ | |
S3.DeleteObject(new DeleteObjectRequest { Key = objectKey, BucketName = BUCKET_NAME }); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Drawing.Imaging; | |
using System.IO; | |
using System.Net.Mime; | |
using GuestBook.Repository; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using Xtremecode.Infrastructure.Library.Di; | |
namespace Demo.S3.Test | |
{ | |
[TestClass] | |
public class GuestBookImageRepositoryTest | |
{ | |
private readonly IGuestBookImageRepository _guestBookImageRepository = DependencyInjector.Get<IGuestBookImageRepository>(); | |
private Stream _uploadedImageFileStream; | |
private string _uniqueBlobName; | |
[TestMethod] | |
public void Can_store_uploaded_image_file() | |
{ | |
// Arrange | |
_uploadedImageFileStream = new MemoryStream(); | |
Resource.Huang5.Save(_uploadedImageFileStream, ImageFormat.Jpeg); | |
// This is an IMPORTANT ONE TO DO! Or else, calling the S3's Upload Utility method would return IOException later. | |
_uploadedImageFileStream.Seek(0, SeekOrigin.Begin); | |
// Act | |
_uniqueBlobName = _guestBookImageRepository.UploadImageFile(MediaTypeNames.Image.Jpeg, _uploadedImageFileStream); | |
// Assert | |
Assert.IsFalse(string.IsNullOrWhiteSpace(_uniqueBlobName)); | |
} | |
[TestCleanup] | |
public void Teardown() | |
{ | |
// Wipe out the stored image | |
if (_uploadedImageFileStream != null) | |
{ | |
_uploadedImageFileStream.Dispose(); | |
_uploadedImageFileStream = null; | |
} | |
_guestBookImageRepository.Remove(_uniqueBlobName); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Amazon; | |
using Amazon.S3; | |
namespace GuestBook.Repository.Aws.S3 | |
{ | |
static class S3Helpers | |
{ | |
public static S3Region AsS3Region(this RegionEndpoint regionEndpoint) | |
{ | |
if (regionEndpoint == RegionEndpoint.APNortheast1) | |
{ | |
return S3Region.APN1; | |
} | |
if (regionEndpoint == RegionEndpoint.APSoutheast1) | |
{ | |
return S3Region.APS1; | |
} | |
if (regionEndpoint == RegionEndpoint.APSoutheast2) | |
{ | |
return S3Region.APS2; | |
} | |
if (regionEndpoint == RegionEndpoint.EUWest1) | |
{ | |
return S3Region.EU; | |
} | |
if (regionEndpoint == RegionEndpoint.SAEast1) | |
{ | |
return S3Region.SAE1; | |
} | |
if (regionEndpoint == RegionEndpoint.USEast1) | |
{ | |
return S3Region.US; | |
} | |
if (regionEndpoint == RegionEndpoint.USWest1) | |
{ | |
return S3Region.USW1; | |
} | |
if (regionEndpoint == RegionEndpoint.USWest2) | |
{ | |
return S3Region.USW2; | |
} | |
return S3Region.SFO; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using Amazon; | |
using Amazon.S3; | |
using Amazon.S3.Model; | |
namespace GuestBook.Repository.Aws.S3 | |
{ | |
abstract class S3RepositoryBase | |
{ | |
private readonly string _bucketName; | |
private readonly S3Region _region; | |
protected S3RepositoryBase(RegionEndpoint regionEndpoint, string bucketName) | |
{ | |
_bucketName = bucketName; | |
S3 = new AmazonS3Client(regionEndpoint); | |
_region = regionEndpoint.AsS3Region(); | |
initialiseBucket(); | |
} | |
private void initialiseBucket() | |
{ | |
var response = S3.ListBuckets(); | |
if (!response.Buckets.Any(bucket => bucket.BucketName.Equals(_bucketName))) | |
{ | |
S3.PutBucket(new PutBucketRequest { BucketName = _bucketName, BucketRegion = _region }); | |
} | |
} | |
protected AmazonS3Client S3 { private set; get; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment