Skip to content

Instantly share code, notes, and snippets.

@n3rd
Created October 18, 2015 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n3rd/ca14d4917dff46d31ffa to your computer and use it in GitHub Desktop.
Save n3rd/ca14d4917dff46d31ffa to your computer and use it in GitHub Desktop.
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.IO;
using System.Net.Mime;
using System.Threading.Tasks;
namespace N3rd.Storage
{
public interface IStorageProvider
{
Task<IStorageFile> SaveAsync(string folder, string fileName, byte[] file, ContentType contentType);
}
public interface IStorageFile
{
Uri Uri { get; }
}
public class AzureBlobStorageProvider : IStorageProvider
{
readonly CloudBlobClient _blobClient;
public AzureBlobStorageProvider(string storageConnectionString)
{
if (string.IsNullOrEmpty(storageConnectionString))
throw new ArgumentException("string null or empty", nameof(storageConnectionString));
var storageAccount = CloudStorageAccount.Parse(storageConnectionString);
_blobClient = new CloudBlobClient(storageAccount.BlobEndpoint, storageAccount.Credentials);
}
public async Task<IStorageFile> SaveAsync(string folder, string fileName, byte[] file, ContentType contentType)
{
var container = _blobClient.GetContainerReference(folder);
var blockBlob = container.GetBlockBlobReference(fileName);
if (contentType != null)
blockBlob.Properties.ContentType = contentType.MediaType;
await blockBlob.UploadFromByteArrayAsync(file, 0, file.Length);
return new AzureBlobStorageFile(blockBlob);
}
}
public class AzureBlobStorageFile : IStorageFile
{
public Uri Uri { get; private set; }
internal AzureBlobStorageFile(ICloudBlob blob)
{
Uri = blob.Uri;
}
}
public class FileSystemStorageProvider : IStorageProvider
{
readonly DirectoryInfo _rootDir;
readonly Uri _endPoint;
public FileSystemStorageProvider(DirectoryInfo rootDir, Uri endPoint)
{
if (rootDir == null)
throw new ArgumentNullException(nameof(rootDir));
if (endPoint == null)
throw new ArgumentNullException(nameof(endPoint));
if (!rootDir.Exists)
throw new ArgumentException("Directory must exist", nameof(rootDir));
_rootDir = rootDir;
_endPoint = endPoint;
}
public async Task<IStorageFile> SaveAsync(string folder, string fileName, byte[] file, ContentType contentType)
{
var fullFileName = Path.Combine(Path.Combine(_rootDir.FullName, folder), fileName);
using (Stream s = new FileStream(fullFileName, FileMode.Create))
{
await s.WriteAsync(file, 0, file.Length);
}
return new FileSystemStorageFile(_endPoint, folder, fileName);
}
}
public class FileSystemStorageFile : IStorageFile
{
public Uri Uri { get; private set; }
internal FileSystemStorageFile(Uri endPoint, string folder, string fileName)
{
Uri = new Uri(string.Format("{0}/{1}/{2}", endPoint.AbsoluteUri.TrimEnd('/'), folder.Trim('/'), fileName.TrimStart('/')));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment