using System.Configuration; using System.IO; using HeuristicLab.Services.Optimization.Billing.Interfaces; using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.StorageClient; namespace HeuristicLab.Services.Optimization.Billing.BillingEngine { public class AzureInvoiceDao : IInvoiceDao { private CloudStorageAccount storageAccount; private CloudStorageAccount StorageAccount { get { if (storageAccount == null) { storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings[AzureConstants.CLOUD_STORAGE_SETTINGS_STRING]); } return storageAccount; } } private CloudBlobClient blobClient; private CloudBlobClient BlobClient { get { if (blobClient == null) { blobClient = StorageAccount.CreateCloudBlobClient(); } return blobClient; } } public AzureInvoiceDao() { } public bool SaveDocument(string documentName, string contents) { CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.INVOICE_BLOB_CONTAINER); container.CreateIfNotExist(); var blob = container.GetBlobReference(documentName); blob.UploadText(contents); return true; } public string LoadDocument(string documentName) { CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.INVOICE_BLOB_CONTAINER); container.CreateIfNotExist(); var blob = container.GetBlobReference(documentName); return blob.DownloadText(); } public void LoadDocument(string documentName, Stream target) { CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.INVOICE_BLOB_CONTAINER); container.CreateIfNotExist(); var blob = container.GetBlobReference(documentName); blob.DownloadToStream(target); } private static class AzureConstants { public static readonly string INVOICE_BLOB_CONTAINER = "invoices"; public static readonly string CLOUD_STORAGE_SETTINGS_STRING = "StorageConnectionString"; } } }