- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
100 lines (84 loc) · 4.21 KB
/
Program.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
usingMicrosoft.WindowsAzure.Storage;
usingMicrosoft.WindowsAzure.Storage.Blob;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Diagnostics;
usingSystem.Threading;
usingSystem.Threading.Tasks;
namespacePutBlockFromURL
{
classProgram
{
staticStringaccount1_connectionstring="<add your connection string for the source account>";
staticStringaccount2_connectionstring="<add your connection string for the dest account>";
staticStringcontainer="mycontainer";
staticStringblob="myfile";
staticvoidMain(string[]args){
// Time the copy operation
StopwatchstopWatch=newStopwatch();
stopWatch.Start();
// MainAsync is the static method that calls async Storage APIs
MainAsync(args).GetAwaiter().GetResult();
// Stop the watch and print
stopWatch.Stop();
Console.WriteLine("Finished the copy in "+stopWatch.Elapsed);
Console.WriteLine("Hit any key to exit ");
Console.ReadKey();
}
staticasyncTaskMainAsync(string[]args)
{
// Copy a blob from account1 to account2 using Put Block From URL
CloudStorageAccountaccount1=CloudStorageAccount.Parse(account1_connectionstring);
CloudStorageAccountaccount2=CloudStorageAccount.Parse(account2_connectionstring);
CloudBlobClientclient1=account1.CreateCloudBlobClient();
CloudBlobClientclient2=account2.CreateCloudBlobClient();
CloudBlobContainercontainer1=client1.GetContainerReference(container);
CloudBlobContainercontainer2=client2.GetContainerReference(container);
// Generate a read only SAS token valid for 1 hour
// This will be used as a source for Put Block From URL
// Append the SAS token to the source
CloudBlockBlobblob1=container1.GetBlockBlobReference(blob);
SharedAccessBlobPolicypolicy=newSharedAccessBlobPolicy();
policy.SharedAccessExpiryTime=DateTime.Now.AddHours(1);
policy.SharedAccessStartTime=DateTime.Now.AddSeconds(-10);
policy.Permissions=SharedAccessBlobPermissions.Read;
StringSAS=blob1.GetSharedAccessSignature(policy);
Stringfull_uri=blob1.Uri.ToString()+SAS;
// Get a reference to the destination blob
CloudBlockBlobblob2=container2.GetBlockBlobReference(blob+"_destination");
// Get Blob Properties to find out the length of the source blob
awaitblob1.FetchAttributesAsync();
// Based on 100MB blocks, let's figure out the number of blocks required to copy the data
// We will call (blocks) times the PutBlockFromURL API
intblock_size=100*1024*1024;
longblocks=(blob1.Properties.Length+block_size-1)/block_size;
try
{
// Get a block id List
List<string>destBlockList=GetBlockIdList((int)blocks);
// Call PutBlock multiple times
// Alternatively do not await, and add all calls into a List<Task>() and then call WhenAll to concurrently copy all
for(inti=0;i<blocks;i++)
{
Console.WriteLine("Putting block "+i+" with offset "+(long)i*(long)block_size);
awaitblob2.PutBlockAsync(destBlockList[i],newUri(full_uri),(long)i*(long)block_size,block_size,null);
}
// Let's now PutBlockList to commit all Blocks in the destBlockList
awaitblob2.PutBlockListAsync(destBlockList);
}
catch(StorageExceptionex){
Console.WriteLine(ex.RequestInformation.HttpStatusCode);
Console.WriteLine(ex.Message);
}
}
publicstaticList<string>GetBlockIdList(intcount)
{
List<string>blocks=newList<string>();
for(inti=0;i<count;i++)
{
blocks.Add(Convert.ToBase64String(Guid.NewGuid().ToByteArray()));
}
returnblocks;
}
}
}