5. Загрузка данных в Blob Storage
var storageAccount = CloudStorageAccount.Parse(
ConfigurationManager
.ConnectionStrings[ConnectionStringSetting]
.ConnectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
var pageBlob = container.GetPageBlobReference(blobName);
using (var fileStream = new FileStream(filePath, FileMode.Open)) {
pageBlob.Create(fileStream.Length);
var buffer = new byte[partSize];
while (offset < fileStream.Length) {
var bytesRead = fileStream.Read(buffer, 0, partSize);
pageBlob.WritePages(new MemoryStream(buffer, 0, bytesRead), offset);
offset += bytesRead;
}
}
5
6. 1. public static void UploadVHDFile(string filePath, string containerName, string blobName)
2. {
3. const int partSize = 1024 * 1024;
4.
5. var storageAccount = CloudStorageAccount.Parse(
6. ConfigurationManager.ConnectionStrings[ConnectionStringSetting].ConnectionString);
7.
8. var blobClient = storageAccount.CreateCloudBlobClient();
9. var container = blobClient.GetContainerReference(containerName);
10. container.CreateIfNotExist();
11.
12. var pageBlob = container.GetPageBlobReference(blobName);
13. pageBlob.DeleteIfExists();
14. pageBlob.Properties.ContentType = "binary/octet-stream";
15.
16. using (var fileStream = new FileStream(filePath, FileMode.Open))
17. {
18. int offset = 0;
19.
20. pageBlob.Create(fileStream.Length);
21.
22. var buffer = new byte[partSize];
23. while (offset < fileStream.Length)
24. {
25. var bytesRead = fileStream.Read(buffer, 0, partSize);
26.
27. pageBlob.WritePages(new MemoryStream(buffer, 0, bytesRead), offset);
28.
29. offset += bytesRead;
30.
31. Console.Write('.');
32. }
33. }
34. }
6
7. Режимы работы Azure Drive
CloudDrive
Snapshot
client
URL
Exception
Mount Mount
Да
Author Lease режим Snapshot режим Publisher
чтение
чтение
запись
Windows Azure
Drive
7
8. Работа с Windows Azure Drive
var account = CloudStorageAccount.Parse(
RoleEnvironment.GetConfigurationSettingValue("ConnectionString"));
var drive = account.CreateCloudDrive("drives/vhd");
try
{
drive.Mount(0, DriveMountOptions.None);
...
}
catch (CloudDriveException e)
{
drive = account.CreateCloudDrive(drive.Snapshot().AbsoluteUri);
drive.Mount(0, DriveMountOptions.None);
}
finally
{
drive.Unmount();
}
8
9. 1. public ActionResult Index()
2. {
3. var account = CloudStorageAccount.Parse(
4. RoleEnvironment.GetConfigurationSettingValue("ConnectionString"));
5.
6. var drive = account.CreateCloudDrive("drives/vhd");
7. var letter = string.Empty;
8.
9. FileInfo[] files;
10. try
11. {
12. letter = drive.Mount(0, DriveMountOptions.None);
13. }
14. catch (CloudDriveException e)
15. {
16. drive = account.CreateCloudDrive(drive.Snapshot().AbsoluteUri);
17. letter = drive.Mount(0, DriveMountOptions.None);
18. }
19. finally
20. {
21. files = new DirectoryInfo(drive.LocalPath).GetFiles();
22. drive.Unmount();
23. }
24. ViewBag.Message = String.Format("Azure Drive mounted on: {0}", letter);
25.
26. return View(files);
27. }
9
11. Ссылки и материалы
Differences Between the Storage Emulator and
Windows Azure Storage Services:
http://msdn.microsoft.com/en-us/gg433135
Windows Azure Drive whitepaper:
http://go.microsoft.com/?linkid=9710117
11