1 | using System.Configuration;
|
---|
2 | using System.IO;
|
---|
3 | using HeuristicLab.Services.Optimization.Billing.Interfaces;
|
---|
4 | using Microsoft.WindowsAzure;
|
---|
5 | using Microsoft.WindowsAzure.StorageClient;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.Services.Optimization.Billing.BillingEngine {
|
---|
8 | public class AzureInvoiceDao : IInvoiceDao {
|
---|
9 | private CloudStorageAccount storageAccount;
|
---|
10 | private CloudStorageAccount StorageAccount {
|
---|
11 | get {
|
---|
12 | if (storageAccount == null) {
|
---|
13 | storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings[AzureConstants.CLOUD_STORAGE_SETTINGS_STRING]);
|
---|
14 | }
|
---|
15 | return storageAccount;
|
---|
16 | }
|
---|
17 | }
|
---|
18 |
|
---|
19 | private CloudBlobClient blobClient;
|
---|
20 | private CloudBlobClient BlobClient {
|
---|
21 | get {
|
---|
22 | if (blobClient == null) {
|
---|
23 | blobClient = StorageAccount.CreateCloudBlobClient();
|
---|
24 | }
|
---|
25 | return blobClient;
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | public AzureInvoiceDao() {
|
---|
30 |
|
---|
31 | }
|
---|
32 |
|
---|
33 | public bool SaveDocument(string documentName, string contents) {
|
---|
34 | CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.INVOICE_BLOB_CONTAINER);
|
---|
35 | container.CreateIfNotExist();
|
---|
36 | var blob = container.GetBlobReference(documentName);
|
---|
37 | blob.UploadText(contents);
|
---|
38 | return true;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public string LoadDocument(string documentName) {
|
---|
42 | CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.INVOICE_BLOB_CONTAINER);
|
---|
43 | container.CreateIfNotExist();
|
---|
44 | var blob = container.GetBlobReference(documentName);
|
---|
45 | return blob.DownloadText();
|
---|
46 | }
|
---|
47 |
|
---|
48 | public void LoadDocument(string documentName, Stream target) {
|
---|
49 | CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.INVOICE_BLOB_CONTAINER);
|
---|
50 | container.CreateIfNotExist();
|
---|
51 | var blob = container.GetBlobReference(documentName);
|
---|
52 | blob.DownloadToStream(target);
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | private static class AzureConstants {
|
---|
57 | public static readonly string INVOICE_BLOB_CONTAINER = "invoices";
|
---|
58 | public static readonly string CLOUD_STORAGE_SETTINGS_STRING = "StorageConnectionString";
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|