forked from jefking/King.A-Trak
/
Azure.cs
196 lines (175 loc) · 5.29 KB
/
Azure.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
namespace Abc.ATrak
{
using System.Configuration;
using System.Diagnostics;
using Microsoft.WindowsAzure.StorageClient;
/// <summary>
/// Azure Storage Item
/// </summary>
public class Azure : IStorageItem
{
#region Members
/// <summary>
/// MD5 Key for Metadata
/// </summary>
private const string MD5MetadataKey = "MD5";
/// <summary>
/// Cloud Blob
/// </summary>
private readonly CloudBlob blob;
/// <summary>
/// Create Snap Shot of blobs
/// </summary>
private static readonly bool createSnapShot = true;
#endregion
#region Constructors
/// <summary>
/// Staticly initializes Azure members
/// </summary>
static Azure()
{
bool.TryParse(ConfigurationManager.AppSettings["CreateSnapShot"], out createSnapShot);
}
/// <summary>
/// Initializes a new instance of the Azure
/// </summary>
/// <param name="container">Container</param>
/// <param name="objId">Object Id</param>
public Azure(CloudBlobContainer container, string objId)
{
this.Path = objId;
this.blob = container.GetBlobReference(objId);
this.RelativePath = this.blob.Name;
}
#endregion
#region Properties
/// <summary>
/// Gets the MD5 Hash
/// </summary>
public string MD5
{
get;
private set;
}
/// <summary>
/// Gets the Content Type
/// </summary>
public string ContentType
{
get;
private set;
}
/// <summary>
/// Gets the Path
/// </summary>
public string Path
{
get;
private set;
}
/// <summary>
/// Gets the Relative Path
/// </summary>
public string RelativePath
{
get;
private set;
}
#endregion
#region Methods
/// <summary>
/// Check to see if item exists
/// </summary>
/// <returns>Exists</returns>
public bool Exists()
{
try
{
this.blob.FetchAttributes();
this.ContentType = this.blob.Properties.ContentType;
this.MD5 = this.blob.Metadata[MD5MetadataKey];
return true;
}
catch (StorageClientException)
{
return false;
}
}
/// <summary>
/// Save Storage Item
/// </summary>
/// <param name="source">Storage Item</param>
/// <param name="exists">Exists</param>
public void Save(IStorageItem source, bool exists = false)
{
if (exists)
{
if (createSnapShot)
{
this.blob.CreateSnapshot();
Trace.WriteLine(string.Format("Created snapshot of blob: '{0}'.", this.blob.Uri));
}
}
if (source.Exists())
{
this.blob.Properties.ContentType = source.ContentType;
//// Currently there is a bug in the library that this isn't being stored or retrieved properly, this will be compatible when the new library comes out
this.blob.Properties.ContentMD5 = source.MD5;
this.blob.UploadByteArray(source.GetData());
if (!string.IsNullOrWhiteSpace(source.MD5))
{
this.blob.Metadata[MD5MetadataKey] = source.MD5;
this.blob.SetMetadata();
}
}
}
/// <summary>
/// Get Cloud Data
/// </summary>
/// <returns>Data for object</returns>
public byte[] GetData()
{
var bytes = this.blob.DownloadByteArray();
if (string.IsNullOrWhiteSpace(this.MD5))
{
using (var createHash = System.Security.Cryptography.MD5.Create())
{
var hash = createHash.ComputeHash(bytes);
this.MD5 = System.Convert.ToBase64String(hash);
}
if (createSnapShot)
{
blob.CreateSnapshot();
}
blob.Properties.ContentMD5 = this.MD5;
this.blob.Metadata[MD5MetadataKey] = this.MD5;
this.blob.SetMetadata();
}
return bytes;
}
/// <summary>
/// Delete
/// </summary>
public void Delete()
{
var options = new BlobRequestOptions()
{
DeleteSnapshotsOption = DeleteSnapshotsOption.IncludeSnapshots,
};
var deleted = this.blob.DeleteIfExists(options);
if (deleted)
{
Trace.Write(string.Format("{0} deleted.", this.Path));
}
}
/// <summary>
/// To String
/// </summary>
/// <returns>String reprensentation of object</returns>
public override string ToString()
{
return string.Format("{0}", this.Path);
}
#endregion
}
}