-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
82 lines (68 loc) · 2.7 KB
/
Copy pathProgram.cs
File metadata and controls
82 lines (68 loc) · 2.7 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
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
namespace SamplePageBlobUpload
{
class Program
{
// Path to the file you want to upload
static readonly string file = @"";
// Valid storage container name
static readonly string containerName = "";
// Valid storage blob name
static readonly string blobName = "";
// Connections string for the destination storage account
static readonly string storageAccountConnectionString = "";
static void Main(string[] args)
{
CloudPageBlob blob = null;
try
{
var storageAccount = CloudStorageAccount.Parse(storageAccountConnectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
container.CreateIfNotExists();
blob = container.GetPageBlobReference(blobName);
}
catch (StorageException ex)
{
Console.WriteLine("Failed to get blob");
if (ex.InnerException is WebException)
{
var wex = ex.InnerException as WebException;
if (wex.Response is HttpWebResponse)
{
var response = wex.Response as HttpWebResponse;
Console.WriteLine(response.StatusDescription);
}
}
throw ex;
}
if (blob.Exists())
throw new Exception("The target blob already exists");
if (!File.Exists(file))
throw new Exception("The file doesn't exist");
UploadFile(blob, file).Wait();
}
static async Task UploadFile(CloudPageBlob blob, string path)
{
using (var fileStream = File.OpenRead(path))
{
var fileSize = fileStream.Length;
Console.WriteLine("File size is {0} bytes", fileSize);
var sizeToPad = 512 - (int)(fileSize % 512);
Console.WriteLine("Need to pad with {0} bytes", sizeToPad);
using (var blobStream = await blob.OpenWriteAsync(fileSize + sizeToPad))
{
Console.WriteLine("Writing file bytes");
await fileStream.CopyToAsync(blobStream);
Console.WriteLine("Padding page blob");
await blobStream.WriteAsync(new byte[sizeToPad], 0, sizeToPad);
}
}
}
}
}