-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
399 lines (348 loc) · 17.5 KB
/
Copy pathProgram.cs
File metadata and controls
399 lines (348 loc) · 17.5 KB
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
using System;
using System.Collections.Generic;
using Microsoft.WindowsAzure; // Namespace for CloudConfigurationManager
using Microsoft.WindowsAzure.Storage; // Namespace for CloudStorageAccount
using Microsoft.WindowsAzure.Storage.Blob; // Namespace for Blob storage types
using Microsoft.Azure; //Namespace for CloudConfigurationManager
using System.IO;
using System.Linq;
namespace azureStorageAccount
{
internal class Program
{
private static void Main(string[] args)
{
// Parse the connection string and return a reference to the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Create container or get
string containerName = "mycontainer";//"containerwith1source";//"mycontainer";//"vhdblobs";
//CloudBlobContainer container = CreateContainer(blobClient, "mycontainer");
CloudBlobContainer container = GetContainerReference(blobClient, containerName);
//PageBlobUploader.UploadVHDToCloud("Oracle-Linux-6-Generalized.128mb.variant.vhd", 128*1024*1024, blobClient, containerName, "Oracle-Linux-6-Generalized.128mb.variant.vhd");
string blobName = "destsparsepageblob1gb.vhd";// "Oracle -Linux-6-Generalized.10GB.vhd";
CloudPageBlob pageBlob = container.GetPageBlobReference(blobName);
//IEnumerable<PageRange> pageRanges = pageBlob.GetPageRanges();
/*
PageBlobDownloader.DownloadVHDFromCloud(blobClient, containerName, blobName);
*/
/*
CloudPageBlob sourcePageBlob = container.GetPageBlobReference("blobwithrandomchar4mb.vhd");
long size = GetCloudBlobSize(sourcePageBlob);
byte[] blobData = new byte[size];
sourcePageBlob.DownloadToByteArray(blobData, 0);
CloudPageBlob destinationPageBlob = container.GetPageBlobReference("blobwithrandomchar4mb2.vhd");
//bool isSame = VerifySourceBlobAndDestinationBlobIdentical(sourcePageBlob, destinationPageBlob);
*/
bool isSame = VerifySourceBlobsAndDestinationBlobsIdentical(
container,
new List<string>() { "Oracle-Linux-6-Generalized.128mb.vhd" },
container,
new List<string>() { "Oracle-Linux-6-Generalized.128mb.copy.vhd" });
PrintBlobSasUri(blobClient, containerName, "Oracle-Linux-6-Generalized.128mb.vhd");
PrintBlobSasUri(blobClient, containerName, "Oracle-Linux-6-Generalized.128mb.copy.vhd");
//PrintBlobSasUri(blobClient, containerName, "destdensepageblob4mb.vhd");
Console.WriteLine("Reached the bottom.");
Console.ReadLine();
}
public static CloudBlobContainer GetContainerReference(CloudBlobClient blobClient, string containerName)
{
return blobClient.GetContainerReference(containerName);
}
private static bool VerifySourceBlobsAndDestinationBlobsIdentical(CloudBlobContainer sourceBlobContainer,
IList<string> sourceBlobNames,
CloudBlobContainer destinationBlobContainer, IList<string> destinationBlobNames)
{
foreach (string sourceBlobName in sourceBlobNames)
{
CloudPageBlob sourceBlob = sourceBlobContainer.GetPageBlobReference(sourceBlobName);
foreach (string destinationBlobName in destinationBlobNames)
{
CloudPageBlob destinationBlob = destinationBlobContainer.GetPageBlobReference(destinationBlobName);
bool isSourceBlobAndDestinationBlobIdentical =
VerifySourceBlobAndDestinationBlobIdentical(sourceBlob, destinationBlob);
if (!isSourceBlobAndDestinationBlobIdentical)
{
return false;
}
}
}
return true;
}
private static bool VerifySourceBlobAndDestinationBlobIdentical(CloudPageBlob sourceBlob, CloudPageBlob destinationBlob)
{
/*
bool isPageRangeIdentical = ComparePageRange(sourceBlob, destinationBlob);
if (!isPageRangeIdentical)
{
return false;
}
*/
bool isBlobContentIdentical = CompareBlobContent(sourceBlob, destinationBlob);
if (!isBlobContentIdentical)
{
return false;
}
return true;
}
private static bool ComparePageRange(CloudPageBlob pageBlob0, CloudPageBlob pageBlob1)
{
List<PageRange> pageRanges0 = pageBlob0.GetPageRanges().ToList();
List<PageRange> pageRanges1 = pageBlob1.GetPageRanges().ToList();
int numPageRanges0 = pageRanges0.Count();
int numPageRanges1 = pageRanges1.Count();
if (numPageRanges0 != numPageRanges1)
{
return false;
}
pageRanges0 = pageRanges0.OrderBy(pageRange => pageRange.StartOffset).ToList();
pageRanges1 = pageRanges1.OrderBy(pageRange => pageRange.StartOffset).ToList();
for (int i = 0; i < numPageRanges0; ++i)
{
if (pageRanges0[i].StartOffset != pageRanges1[i].StartOffset ||
pageRanges0[i].EndOffset != pageRanges1[i].EndOffset)
{
return false;
}
}
return true;
}
private static bool CompareBlobContent(CloudBlob blob0, CloudBlob blob1)
{
const int bytesToRead = sizeof(Int64);
const int oneMegabyteInBytes = 1024 * 1024;
long blobLength0 = GetCloudBlobSize(blob0);
long blobLength1 = GetCloudBlobSize(blob1);
if (blobLength0 != blobLength1)
{
return false;
}
// Need to read in chunks in order to avoid insufficient memory issue.
long maxChunkSize = 64 * oneMegabyteInBytes;
byte[] bytes0 = new byte[bytesToRead];
byte[] bytes1 = new byte[bytesToRead];
long totalBytesRead = 0;
while (totalBytesRead < blobLength0)
{
long bytesToBeRead = (blobLength0 - totalBytesRead < maxChunkSize)
? (blobLength0 - totalBytesRead)
: maxChunkSize;
using (Stream blobStream0 = new MemoryStream())
using (Stream blobStream1 = new MemoryStream())
{
blob0.DownloadRangeToStream(blobStream0, totalBytesRead, bytesToBeRead);
blob1.DownloadRangeToStream(blobStream1, totalBytesRead, bytesToBeRead);
blobStream0.Seek(0, SeekOrigin.Begin);
blobStream1.Seek(0, SeekOrigin.Begin);
int iterations = (int)Math.Ceiling((double)blobStream0.Length / bytesToRead);
for (int i = 0; i < iterations; i++)
{
blobStream0.Read(bytes0, 0, bytesToRead);
blobStream1.Read(bytes1, 0, bytesToRead);
if (BitConverter.ToInt64(bytes0, 0) != BitConverter.ToInt64(bytes1, 0))
{
return false;
}
}
}
totalBytesRead += bytesToBeRead;
}
return true;
}
/// <summary>
/// THis is a working version for a more general blob not just page blob.
/// </summary>
/// <param name="sourceBlob"></param>
/// <param name="destinationBlob"></param>
/// <returns></returns>
public static bool VerifySourceBlobAndDestinationBlobIdentical2(CloudBlob sourceBlob, CloudBlob destinationBlob)
{
const int bytesToRead = sizeof(Int64);
const int numberOfPartition = 16;
long sourceBlobLength = GetCloudBlobSize(sourceBlob);
long destinationBlobLength = GetCloudBlobSize(destinationBlob);
if (sourceBlobLength != destinationBlobLength)
{
return false;
}
int numberOfBytesInOnePartition = (int)Math.Ceiling((double)sourceBlobLength / numberOfPartition);
int totalBytesRead = 0;
while (totalBytesRead < sourceBlobLength)
{
using (Stream sourceBlobStream = new MemoryStream())
using (Stream destinationBlobStream = new MemoryStream())
{
//sourceBlob.DownloadToStream(sourceBlobStream, null, null);
//destinationBlob.DownloadToStream(destinationBlobStream, null, null);
sourceBlob.DownloadRangeToStream(sourceBlobStream, totalBytesRead, numberOfBytesInOnePartition);
destinationBlob.DownloadRangeToStream(destinationBlobStream, totalBytesRead, numberOfBytesInOnePartition);
sourceBlobStream.Seek(0, SeekOrigin.Begin);
destinationBlobStream.Seek(0, SeekOrigin.Begin);
long sourceBlobStreamLength = sourceBlobStream.Length;
long destinationBlobStreamLength = destinationBlobStream.Length;
if (sourceBlobStreamLength != destinationBlobStreamLength)
{
return false;
}
int iterations = (int)Math.Ceiling((double)sourceBlobStreamLength / bytesToRead);
byte[] sourceBytes = new byte[bytesToRead];
byte[] destinationBytes = new byte[bytesToRead];
for (int i = 0; i < iterations; i++)
{
sourceBlobStream.Read(sourceBytes, 0, bytesToRead);
destinationBlobStream.Read(destinationBytes, 0, bytesToRead);
if (BitConverter.ToInt64(sourceBytes, 0) != BitConverter.ToInt64(destinationBytes, 0))
{
return false;
}
}
}
totalBytesRead += numberOfBytesInOnePartition;
}
return true;
}
/*
public static bool VerifySourceBlobAndDestinationBlobIdentical(CloudBlob sourceBlob, CloudBlob destinationBlob)
{
long sourceBlobLength = GetCloudBlobSize(sourceBlob);
long destinationBlobLength = GetCloudBlobSize(destinationBlob);
if (sourceBlobLength != destinationBlobLength)
{
return false;
}
int numberOfPartition = 16;
int numberOfBytesInOnePartition = (int) Math.Ceiling((double)sourceBlobLength/numberOfPartition);
int totalBytesRead = 0;
while (totalBytesRead < sourceBlobLength)
{
using (Stream sourceBlobStream = new MemoryStream())
using (Stream destinationBlobStream = new MemoryStream())
{
//sourceBlob.DownloadToStream(sourceBlobStream, null, null);
//destinationBlob.DownloadToStream(destinationBlobStream, null, null);
sourceBlob.DownloadRangeToStream(sourceBlobStream, totalBytesRead, numberOfBytesInOnePartition);
destinationBlob.DownloadRangeToStream(destinationBlobStream, totalBytesRead, numberOfBytesInOnePartition);
sourceBlobStream.Seek(0, SeekOrigin.Begin);
destinationBlobStream.Seek(0, SeekOrigin.Begin);
long sourceBlobStreamLength = sourceBlobStream.Length;
long destinationBlobStreamLength = destinationBlobStream.Length;
if (sourceBlobStreamLength != destinationBlobStreamLength)
{
return false;
}
int iterations = (int) Math.Ceiling((double) sourceBlobStreamLength/BYTES_TO_READ);
byte[] sourceBytes = new byte[BYTES_TO_READ];
byte[] destinationBytes = new byte[BYTES_TO_READ];
for (int i = 0; i < iterations; i++)
{
sourceBlobStream.Read(sourceBytes, 0, BYTES_TO_READ);
destinationBlobStream.Read(destinationBytes, 0, BYTES_TO_READ);
if (BitConverter.ToInt64(sourceBytes, 0) != BitConverter.ToInt64(destinationBytes, 0))
{
return false;
}
}
}
totalBytesRead += numberOfBytesInOnePartition;
}
return true;
}
*/
/*
public static bool CompareStreams(Stream stream0, Stream stream1)
{
}
*/
public static long GetCloudBlobSize(CloudBlob blob)
{
if (blob.Exists())
{
blob.FetchAttributes();
return blob.Properties.Length;
}
return -1;
}
private static FileInfo GetBlobFileInfo(CloudPageBlob blob)
{
return null;
}
const int BYTES_TO_READ = sizeof(Int64);
/// <summary>
/// This method compares if two file are identical.
/// </summary>
/// <param name="first"></param>
/// <param name="second"></param>
/// <returns></returns>
static bool FilesAreEqual(FileInfo first, FileInfo second)
{
if (first.Length != second.Length)
return false;
if (string.Equals(first.FullName, second.FullName, StringComparison.OrdinalIgnoreCase))
return true;
int iterations = (int)Math.Ceiling((double)first.Length / BYTES_TO_READ);
using (FileStream fs1 = first.OpenRead())
using (FileStream fs2 = second.OpenRead())
{
byte[] one = new byte[BYTES_TO_READ];
byte[] two = new byte[BYTES_TO_READ];
for (int i = 0; i < iterations; i++)
{
fs1.Read(one, 0, BYTES_TO_READ);
fs2.Read(two, 0, BYTES_TO_READ);
if (BitConverter.ToInt64(one, 0) != BitConverter.ToInt64(two, 0))
return false;
}
}
return true;
}
static void PrintBlobSasUri(CloudBlobClient blobClient, string containerName, string blobName)
{
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
string sasUri = GetBlobSasUri(container, blobName, null);
Console.WriteLine(sasUri);
}
static CloudBlobContainer CreateContainer(CloudBlobClient blobClient, string containerName)
{
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
// Create the container if it doesn't already exist.
container.CreateIfNotExists();
return container;
}
private static string GetBlobSasUri(CloudBlobContainer container, string blobName, string policyName = null)
{
string sasBlobToken;
// Get a reference to a blob within the container.
// Note that the blob may not exist yet, but a SAS can still be created for it.
CloudBlockBlob blob = container.GetBlockBlobReference(blobName);
if (policyName == null)
{
// Create a new access policy and define its constraints.
// Note that the SharedAccessBlobPolicy class is used both to define the parameters of an ad-hoc SAS, and
// to construct a shared access policy that is saved to the container's shared access policies.
SharedAccessBlobPolicy adHocSAS = new SharedAccessBlobPolicy()
{
// When the start time for the SAS is omitted, the start time is assumed to be the time when the storage service receives the request.
// Omitting the start time for a SAS that is effective immediately helps to avoid clock skew.
SharedAccessExpiryTime = DateTime.UtcNow.AddMonths(48),
Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Create | SharedAccessBlobPermissions.Add | SharedAccessBlobPermissions.Delete
};
// Generate the shared access signature on the blob, setting the constraints directly on the signature.
sasBlobToken = blob.GetSharedAccessSignature(adHocSAS);
Console.WriteLine("SAS for blob (ad hoc): {0}", sasBlobToken);
Console.WriteLine();
}
else
{
// Generate the shared access signature on the blob. In this case, all of the constraints for the
// shared access signature are specified on the container's stored access policy.
sasBlobToken = blob.GetSharedAccessSignature(null, policyName);
Console.WriteLine("SAS for blob (stored access policy): {0}", sasBlobToken);
Console.WriteLine();
}
// Return the URI string for the container, including the SAS token.
return blob.Uri + sasBlobToken;
}
}
}