public void Upload(CloudStorageAccount account, string driveBlobUri) { FileInfo fileInfo = new FileInfo(this.fileName); Console.WriteLine("Uploading virtual hard disk (VHD) file: {0}", fileInfo.Name); Console.WriteLine(); CloudBlobClient blobClient = account.CreateCloudBlobClient(); blobClient.Timeout = new TimeSpan(0, 0, 180); // create page blob to provide backing storage for the drive - pad length to be multiple of 512 bytes CloudPageBlob driveBlob = blobClient.GetPageBlobReference(driveBlobUri); long paddedLength = (fileInfo.Length + PAGEBLOB_PAGE_SIZE - 1) & ~(PAGEBLOB_PAGE_SIZE - 1); Console.WriteLine("Creating Azure Drive"); Console.WriteLine(" URI: {0}", driveBlob.Uri.AbsoluteUri); Console.WriteLine(" Size: {0:n3} MB", paddedLength / (1024 * 1024)); Console.WriteLine(); // first ensure that container exists and then create the blob CloudBlobContainer container = blobClient.GetContainerReference(driveBlob.Container.Name); container.CreateIfNotExist(); driveBlob.Create(paddedLength); this.fileOffset = 0; this.blockLength = 0; Console.WriteLine("{0} - Uploading data...\r\n", DateTime.Now.ToLongTimeString()); using (stream = fileInfo.OpenRead()) { int blockNumber = 0; ProgressIndicator progress = new ProgressIndicator(); while (Read()) { Console.Write("\r Writing block {0,3} at offset: {1,16:n0}, size: {2,16:n1} KB", blockNumber, this.blockOffset, Convert.ToDecimal(this.blockLength) / 1024); progress.Enable(); driveBlob.WritePages(new MemoryStream(this.buffer, 0, this.blockLength, false), this.blockOffset); Console.WriteLine("\r *"); progress.Disable(); blockNumber++; } Console.WriteLine(); Console.WriteLine("{0} - Upload complete.", DateTime.Now.ToLongTimeString()); } }
private static string MountCloudDrive(string uri, string connectionString) { Console.WriteLine("Configuring CloudDrive", "Information"); /* * LocalResource localCache = RoleEnvironment.GetLocalResource("Two10.MountVHD.Cache"); * * const int TRIES = 30; * * // Temporary workaround for ERROR_UNSUPPORTED_OS seen with Windows Azure Drives * // See http://blogs.msdn.com/b/windowsazurestorage/archive/2010/12/17/error-unsupported-os-seen-with-windows-azure-drives.aspx * for (int i = 0; i < TRIES; i++) * { * try * { * CloudDrive.InitializeCache(localCache.RootPath, localCache.MaximumSizeInMegabytes); * break; * } * catch (CloudDriveException ex) * { * if (!ex.Message.Equals("ERROR_UNSUPPORTED_OS")) * { * throw; * } * * if (i >= (TRIES - 1)) * { * // If the workaround fails then it would be dangerous to continue silently, so exit * Console.WriteLine("Workaround for ERROR_UNSUPPORTED_OS see http://bit.ly/fw7qzo FAILED", "Error"); * System.Environment.Exit(-1); * } * * Console.WriteLine("Using temporary workaround for ERROR_UNSUPPORTED_OS see http://bit.ly/fw7qzo", "Information"); * Thread.Sleep(10000); * } * }*/ CloudStorageAccount cloudDriveStorageAccount = CloudStorageAccount.Parse(connectionString); CloudBlobClient blobClient = cloudDriveStorageAccount.CreateCloudBlobClient(); CloudPageBlob pageBlob = blobClient.GetPageBlobReference(uri); var cloudDrive = cloudDriveStorageAccount.CreateCloudDrive(pageBlob.Uri.ToString()); cloudDrive.CreateIfNotExist(1024 * 250); try { Console.WriteLine(string.Format("Mounting {0}", cloudDrive.Uri), "Information"); return(cloudDrive.Mount(0, DriveMountOptions.None)); } catch (CloudDriveException e) { Console.WriteLine(e); } finally { Console.WriteLine(string.Format("CloudDrive {0} mounted at {1}", cloudDrive.Uri, cloudDrive.LocalPath), "Information"); } return(null); }