-
Notifications
You must be signed in to change notification settings - Fork 4
/
AzureBlobStore.cs
438 lines (370 loc) · 17 KB
/
AzureBlobStore.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Copyright © Microsoft Corporation. All Rights Reserved.
// This code released under the terms of the
// Apache License, Version 2.0 (http://opensource.org/licenses/Apache-2.0)
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.IO;
using System.Text;
using Microsoft.Win32;
using Microsoft.Synchronization.Files;
using Microsoft.Synchronization.SimpleProviders;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
namespace FileSystemDurabilityPlugin
{
internal struct SyncedBlobAttributes
{
internal string _name;
internal DateTime _lastModifiedTime;
internal SyncedBlobAttributes(string name, DateTime lastModifiedTime)
{
_name = name;
_lastModifiedTime = lastModifiedTime;
}
}
internal class AzureBlobStore
{
public const uint MaxFileNameLength = 255;
private string _targetContainer = null;
private CloudBlobClient _blobStorage = null;
private const string FileNameKey = "FileName";
private const string AttributesKey = "Attributes";
private const string CreationTimeKey = "CreationTime";
private const string LastAccessTimeKey = "LastAccessTime";
private const string LastWriteTimeKey = "LastWriteTime";
private const string RelativePathKey = "RelativePath";
private const string PathWithNameKey = "PathWithName";
private const string SizeKey = "Size";
private const string IsDirectory = "IsDirectory";
internal AzureBlobStore(
string targetContainer,
CloudStorageAccount storageAccount
)
{
_blobStorage = storageAccount.CreateCloudBlobClient();
_targetContainer = targetContainer;
CreateContainer();
}
void CreateContainer()
{
//
// Azure Blob Storage requires some delay between deleting and recreating containers. This loop
// compensates for that delay if the first attempt to creat the container fails then wait 30
// seconds and try again. While the service actually returns error 409 with the correct description
// of the failure the storage client interprets that as the container existing and the
// call to CreateContainer returns false. For now just work around this by attempting to create
// a test blob in the container bofore proceeding.
//
for (int i = 1; i <= 10; i++)
{
CloudBlobContainer blobContainer = Container;
blobContainer.CreateIfNotExist();
MemoryStream stream = new MemoryStream();
try
{
blobContainer.GetBlobReference("__testblob").UploadFromStream(stream);
blobContainer.GetBlobReference("__testblob").Delete();
var permissions = blobContainer.GetPermissions();
permissions.PublicAccess = BlobContainerPublicAccessType.Container;
blobContainer.SetPermissions(permissions);
break;
}
catch (StorageClientException e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Failed creating container, retrying in 30 seconds...");
// Wait 30 seconds and retry
System.Threading.Thread.Sleep(30000);
if (i == 10)
{
// Only retry 10 times
throw;
}
}
}
}
private CloudBlobContainer Container
{
get
{
return _blobStorage.GetContainerReference(_targetContainer);
}
}
// ListBlobs is called by AzureBlobSyncProvider.EnumerateItems. It walks through the items in the store building up the necessary information
// to detect changes.
internal List<ItemFieldDictionary> ListBlobs()
{
List<ItemFieldDictionary> items = new List<ItemFieldDictionary>();
// Include all items in the listing including those with path like ('/') file names
BlobRequestOptions opts = new BlobRequestOptions();
opts.UseFlatBlobListing = true;
foreach (IListBlobItem o in Container.ListBlobs(opts))
{
CloudBlob blob = Container.GetBlobReference(o.Uri.ToString());
blob.FetchAttributes();
ItemFieldDictionary dict = new ItemFieldDictionary();
dict.Add(new ItemField(ItemFields.CUSTOM_FIELD_NAME, typeof(string), o.Uri.ToString()));
dict.Add(new ItemField(ItemFields.CUSTOM_FIELD_TIMESTAMP, typeof(ulong), (ulong)blob.Properties.LastModifiedUtc.ToBinary()));
items.Add(dict);
}
return items;
}
// Create a file retriever for the blob with the given name.
// Note that this code attempts to handle
internal FileRetriever GetFileRetriever(
string name,
DateTime expectedLastUpdate
)
{
DateTime creationTime = DateTime.Now;
DateTime lastAccessTime = DateTime.Now;
DateTime lastWriteTime = DateTime.Now;
FileStream stream = null;
long size = 0;
string relativePath = "";
FileAttributes fileAttributes = 0;
string tempFileName = Path.GetTempFileName();
CloudBlob blob = Container.GetBlobReference(name);
BlobProperties props = blob.Properties;
// Specify an optimistic concurrency check to prevent races with other endpoints syncing at the same time.
BlobRequestOptions opts = new BlobRequestOptions();
opts.AccessCondition = AccessCondition.IfNotModifiedSince(expectedLastUpdate);
try
{
blob.DownloadToFile(tempFileName);
}
catch (StorageException e)
{
if (e.ErrorCode == StorageErrorCode.BlobAlreadyExists || e.ErrorCode == StorageErrorCode.ConditionFailed)
{
throw new ApplicationException("Concurrency Violation", e);
}
throw;
}
string fileName;
string pathWithName;
if (blob.Metadata.AllKeys.Contains(AzureBlobStore.FileNameKey))
fileName = blob.Metadata[AzureBlobStore.FileNameKey];
if (blob.Metadata.AllKeys.Contains(AzureBlobStore.CreationTimeKey))
creationTime = DateTime.Parse(blob.Metadata[AzureBlobStore.CreationTimeKey]);
if (blob.Metadata.AllKeys.Contains(AzureBlobStore.LastAccessTimeKey))
lastAccessTime = DateTime.Parse(blob.Metadata[AzureBlobStore.LastAccessTimeKey]);
if (blob.Metadata.AllKeys.Contains(AzureBlobStore.LastWriteTimeKey))
lastWriteTime = DateTime.Parse(blob.Metadata[AzureBlobStore.LastWriteTimeKey]);
if (blob.Metadata.AllKeys.Contains(AzureBlobStore.RelativePathKey))
relativePath = blob.Metadata[AzureBlobStore.RelativePathKey];
if (blob.Metadata.AllKeys.Contains(AzureBlobStore.AttributesKey))
fileAttributes = (FileAttributes)long.Parse(blob.Metadata[AzureBlobStore.AttributesKey]);
if (blob.Metadata.AllKeys.Contains(AzureBlobStore.IsDirectory))
{
if (!bool.Parse(blob.Metadata[AzureBlobStore.IsDirectory]))
{
// Special handling for directories
stream = File.OpenRead(tempFileName);
size = long.Parse(blob.Metadata[AzureBlobStore.SizeKey]);
}
}
pathWithName = blob.Metadata[AzureBlobStore.PathWithNameKey];
// Note that the IsDirectory property for the FileData object is pulled from the Attributes
FileData fd = new FileData(
pathWithName,
fileAttributes,
creationTime,
lastAccessTime,
lastWriteTime,
size);
return new FileRetriever(fd, relativePath, stream);
}
// Create the name value collection of associated file metadata for the given blob.
private void SetupMetadata(
NameValueCollection metadata,
FileData fileData,
string relativePath
)
{
// Create metadata to be associated with the blob
metadata[AzureBlobStore.FileNameKey] = fileData.Name;
metadata[AzureBlobStore.AttributesKey] = ((long)fileData.Attributes).ToString();
metadata[AzureBlobStore.CreationTimeKey] = fileData.CreationTime.ToString();
metadata[AzureBlobStore.LastAccessTimeKey] = fileData.LastAccessTime.ToString();
metadata[AzureBlobStore.LastWriteTimeKey] = fileData.LastWriteTime.ToString();
// Azure Blob Storage does not seem to like null or empty string values. So if that is what would be written
// don't write anything at all.
if (relativePath != "")
{
metadata[AzureBlobStore.RelativePathKey] = relativePath;
}
metadata[AzureBlobStore.PathWithNameKey] = fileData.RelativePath;
metadata[AzureBlobStore.SizeKey] = fileData.Size.ToString();
metadata[AzureBlobStore.IsDirectory] = fileData.IsDirectory.ToString();
}
// Called by AzureBlobSyncProvider.InsertItem.
internal SyncedBlobAttributes InsertFile(FileData fileData, string relativePath, Stream dataStream)
{
if (fileData.Name.Length > MaxFileNameLength)
{
throw new ApplicationException("Name Too Long");
}
CloudBlob blob = Container.GetBlobReference(fileData.RelativePath.ToLower());
BlobProperties blobProperties = blob.Properties;
DateTime uninitTime = blobProperties.LastModifiedUtc;
SetupMetadata(blob.Metadata, fileData, relativePath);
blobProperties.ContentType = LookupMimeType(Path.GetExtension(fileData.Name));
if (fileData.IsDirectory)
{
// Directories have no stream
dataStream = new MemoryStream();
}
// Specify an optimistic concurrency check to prevent races with other endpoints syncing at the same time.
BlobRequestOptions opts = new BlobRequestOptions();
opts.AccessCondition = AccessCondition.IfNotModifiedSince(uninitTime);
try
{
blob.UploadFromStream(dataStream, opts);
}
catch (StorageException e)
{
if (e.ErrorCode == StorageErrorCode.BlobAlreadyExists || e.ErrorCode == StorageErrorCode.ConditionFailed)
{
throw new ApplicationException("Concurrency Violation", e);
}
throw;
}
blobProperties = blob.Properties;
SyncedBlobAttributes attributes = new SyncedBlobAttributes(blob.Uri.ToString(), blobProperties.LastModifiedUtc);
return attributes;
}
// Called by AzureBlobSyncProvider.UpdateItem to help with writing item updates
internal SyncedBlobAttributes UpdateFile(
string oldName,
FileData fileData,
string relativePath,
Stream dataStream,
DateTime expectedLastModified
)
{
CloudBlob blob = Container.GetBlobReference(oldName);
try
{
blob.FetchAttributes();
}
catch (StorageClientException e)
{
// Someone may have deleted the blob in the mean time
if (e.ErrorCode == StorageErrorCode.BlobNotFound)
{
throw new ApplicationException("Concurrency Violation", e);
}
throw;
}
BlobProperties blobProperties = blob.Properties;
//
// For directories create an empty data stream
//
if (dataStream == null)
{
dataStream = new MemoryStream();
}
SetupMetadata(blob.Metadata, fileData, relativePath);
blobProperties.ContentType = LookupMimeType(Path.GetExtension(fileData.Name));
// Specify an optimistic concurrency check to prevent races with other endpoints syncing at the same time.
BlobRequestOptions opts = new BlobRequestOptions();
opts.AccessCondition = AccessCondition.IfNotModifiedSince(expectedLastModified);
try
{
blob.UploadFromStream(dataStream, opts);
}
catch (StorageClientException e)
{
// Someone must have modified the file in the mean time
if (e.ErrorCode == StorageErrorCode.BlobNotFound || e.ErrorCode == StorageErrorCode.ConditionFailed)
{
throw new ApplicationException("Storage Error", e);
}
throw;
}
blobProperties = blob.Properties;
SyncedBlobAttributes attributes = new SyncedBlobAttributes(blob.Uri.ToString(), blobProperties.LastModifiedUtc);
return attributes;
}
// Called by AzureBlobSyncProvider.DeleteItem to help with removing blobs.
internal void DeleteFile(
string name,
DateTime expectedLastModified
)
{
CloudBlob blob = Container.GetBlobReference(name);
try
{
blob.FetchAttributes();
}
catch (StorageClientException e)
{
// Someone may have deleted the blob in the mean time
if (e.ErrorCode == StorageErrorCode.BlobNotFound)
{
throw new ApplicationException("Concurrency Violation", e);
}
throw;
}
BlobProperties blobProperties = blob.Properties;
bool isDirectory = bool.Parse(blob.Metadata[AzureBlobStore.IsDirectory]);
// If this is a directory then we need to look for children.
if (isDirectory)
{
IEnumerable<IListBlobItem> items = Container.ListBlobs();
if (items.Count() > 0)
{
throw new ApplicationException("Constraint Violation - Directory Not Empty");
}
}
// Specify an optimistic concurrency check to prevent races with other endpoints syncing at the same time.
BlobRequestOptions opts = new BlobRequestOptions();
opts.AccessCondition = AccessCondition.IfNotModifiedSince(expectedLastModified);
try
{
// http://social.msdn.microsoft.com/Forums/en/windowsazure/thread/08e71f7b-ca9e-4107-b5fe-06890bb512e6
//blob.Delete(opts);
blob.DeleteIfExists();
}
catch (StorageClientException e)
{
// Someone must have modified the file in the mean time
if (e.ErrorCode == StorageErrorCode.BlobNotFound || e.ErrorCode == StorageErrorCode.ConditionFailed)
{
throw new ApplicationException("Concurrency Violation", e);
}
throw;
}
}
// Specify the mime type correctly for uploaded files.
private string LookupMimeType(string extension)
{
extension = extension.ToLower();
RegistryKey key = Registry.ClassesRoot.OpenSubKey("MIME\\Database\\Content Type");
try
{
foreach (string keyName in key.GetSubKeyNames())
{
RegistryKey temp = key.OpenSubKey(keyName);
if (extension.Equals(temp.GetValue("Extension")))
{
return keyName;
}
}
}
finally
{
key.Close();
}
return "";
}
// Helper method called to figure out the root path for all files in blob storage.
internal string GetRootPath()
{
return Container.Uri.AbsoluteUri;
}
}
}