public void UploadCopyBlobWithPremiumPageBlobTier() { string xIOStorageConnectionString = Test.Data.Get("XIOStorageConnectionString"); PowerShellAgent.SetStorageContext(xIOStorageConnectionString); string filePath = FileUtil.GenerateOneTempTestFile(); CloudBlobClient xIOBlobClient = CloudStorageAccount.Parse(xIOStorageConnectionString).CreateCloudBlobClient(); CloudBlobContainer container = xIOBlobClient.GetContainerReference(Utility.GenNameString("container")); container.CreateIfNotExists(); string blobName = Utility.GenNameString("blob"); string blobName2 = Utility.GenNameString("blob"); string blobName3 = Utility.GenNameString("blob"); try { //Create local file string localMD5 = FileUtil.GetFileContentMD5(filePath); //upload Test.Assert(CommandAgent.SetAzureStorageBlobContent(filePath, container.Name, StorageBlob.BlobType.PageBlob, blobName, true, premiumPageBlobTier: PremiumPageBlobTier.P20), "upload a blob name with premiumPageBlobTier should succeed"); CloudPageBlob blob = container.GetPageBlobReference(blobName); blob.FetchAttributes(); ////The check MD5 code are comment, and add code to set contentMD5 since a bug in server that set blob tier will clear contentMD5. Will revert it when the bug fixed. //ExpectEqual(localMD5, blob.Properties.ContentMD5, "content md5"); blob.Properties.ContentMD5 = localMD5; blob.SetProperties(); Test.Assert(PremiumPageBlobTier.P20 == blob.Properties.PremiumPageBlobTier, "PremiumPageBlobTier shoud match: {0} == {1}", PremiumPageBlobTier.P20, blob.Properties.PremiumPageBlobTier); //Copy 1 Test.Assert(CommandAgent.StartAzureStorageBlobCopy(container.Name, blobName, container.Name, blobName2, premiumPageBlobTier: PremiumPageBlobTier.P30), "upload a blob name with premiumPageBlobTier should succeed"); blob = container.GetPageBlobReference(blobName2); blob.FetchAttributes(); ExpectEqual(localMD5, blob.Properties.ContentMD5, "content md5"); Test.Assert(PremiumPageBlobTier.P30 == blob.Properties.PremiumPageBlobTier, "PremiumPageBlobTier shoud match: {0} == {1}", PremiumPageBlobTier.P30, blob.Properties.PremiumPageBlobTier); //Copy 2 Test.Assert(CommandAgent.StartAzureStorageBlobCopy(blob, container.Name, blobName3, premiumPageBlobTier: PremiumPageBlobTier.P4), "upload a blob name with premiumPageBlobTier should succeed"); blob = container.GetPageBlobReference(blobName3); blob.FetchAttributes(); ExpectEqual(localMD5, blob.Properties.ContentMD5, "content md5"); Test.Assert(PremiumPageBlobTier.P4 == blob.Properties.PremiumPageBlobTier, "PremiumPageBlobTier shoud match: {0} == {1}", PremiumPageBlobTier.P4, blob.Properties.PremiumPageBlobTier); } finally { string StorageConnectionString = Test.Data.Get("StorageConnectionString"); PowerShellAgent.SetStorageContext(StorageConnectionString); container.DeleteIfExists(); FileUtil.RemoveFile(filePath); } }
public void RemoveSnapshot() { string containerName = Utility.GenNameString("container"); string blobName = Utility.GenNameString("blob"); CloudBlobContainer container = blobUtil.CreateContainer(containerName); try { ICloudBlob blob = blobUtil.CreatePageBlob(container, blobName); List <ICloudBlob> blobs = new List <ICloudBlob>(); blob.FetchAttributes(); int count = random.Next(1, 5); for (int i = 0; i < count; i++) { CloudPageBlob snapshot = ((CloudPageBlob)blob).CreateSnapshot(); snapshot.FetchAttributes(); blobs.Add(snapshot); } blobs.Add(blob); List <IListBlobItem> blobLists = container.ListBlobs(string.Empty, true, BlobListingDetails.All).ToList(); Test.Assert(blobLists.Count == blobs.Count, string.Format("container {0} should contain {1} blobs, and actually it contain {2} blobs", containerName, blobs.Count, blobLists.Count)); Test.Assert(agent.RemoveAzureStorageBlob(blobName, containerName, true), Utility.GenComparisonData("Remove-AzureStorageBlob and snapshot", true)); blobLists = container.ListBlobs(string.Empty, true, BlobListingDetails.All).ToList(); Test.Assert(blobLists.Count == 1, string.Format("container {0} should contain {1} blobs", containerName, 1)); ICloudBlob remainBlob = blobLists[0] as ICloudBlob; Test.Assert(blob.Name == remainBlob.Name, string.Format("Blob name should be {0}, and actually it's {1}", blob.Name, remainBlob.Name)); Test.Assert(null == remainBlob.SnapshotTime, "snapshot time should be null"); } finally { blobUtil.RemoveContainer(containerName); } }
private async Task <bool> WaitForStatus(CloudBlobContainer container, string blobName) { DateTime startWait = DateTime.UtcNow; while (true) { try { CloudPageBlob blob = container.GetPageBlobReference(blobName); blob.FetchAttributes(); if (blob.Metadata.ContainsKey("status") && blob.Metadata.ContainsKey("heartbeat")) { return(true); } DateTime endWait = DateTime.UtcNow; TimeSpan duration = endWait - startWait; if (duration.TotalSeconds > 20) { this.SetException(new ApplicationException("Job never put attributes in heartbeat")); return(false); } await Task.Delay(1000); } catch (Exception e) { this.SetException(new ApplicationException("Failed waiting for heartbeat attributes", e)); return(false); } } }
public static long GetActualDiskSizeAsync(this CloudPageBlob pageBlob, long range = 5 /* default is 5 GB */) { pageBlob.FetchAttributes(); long pageBlobSize = 0; Object _Lock = new Object(); long rangeSize = range * 1024 * 1024 * 1024; List <long> offsetList = new List <long>(); long startOffset = 0; while (startOffset < pageBlob.Properties.Length) { offsetList.Add(startOffset); startOffset += rangeSize; } // add blob name size and metadata size pageBlobSize += (124 + pageBlob.Name.Length * 2 + pageBlob.Metadata.Sum(m => m.Key.Length + m.Value.Length + 3)); Parallel.ForEach(offsetList, (currentOffset) => { long tmp = pageBlob.GetPageRanges(currentOffset, rangeSize).Sum(r => 12 + (r.EndOffset - r.StartOffset)); lock (_Lock) { pageBlobSize += tmp; } // Console.WriteLine("Process {0} on thread {1}: {2}", currentOffset, Thread.CurrentThread.ManagedThreadId, GetFormattedDiskSize(pageBlobSize)); }); Task.WaitAll(); return(pageBlobSize); }
private IEnumerable <IndexRange> GetPageRanges() { pageBlob.FetchAttributes(new AccessCondition(), blobRequestOptions); IEnumerable <PageRange> pageRanges = pageBlob.GetPageRanges(null, null, new AccessCondition(), blobRequestOptions); pageRanges.OrderBy(range => range.StartOffset); return(pageRanges.Select(pr => new IndexRange(pr.StartOffset, pr.EndOffset))); }
public long Read() { _blob.FetchAttributes(); var s = _blob.Metadata["committedsize"]; //Log.Debug("Checkpoint were '{0}'", s ?? "N/A"); var read = Int64.Parse(s ?? "0"); return(read); }
/// <summary> /// Tries to resize the VHD file, using the parameters specified. /// </summary> /// <param name="newSizeInGb">The new size of the VHD file, in gigabytes.</param> /// <param name="blobUri">The <see cref="Uri"/> to locate the VHD in the Azure Storage account.</param> /// <param name="accountName">The name of the Azure Storage account.</param> /// <param name="accountKey">The key of the Azure Storage account.</param> /// <returns>Returns <see cref="ResizeResult.Error"/> if there were issues while trying to do the resize operation. /// Returns <see cref="ResizeResult.Shrink"/> if this is a shrink operation which needs user confirmation. /// Returns <see cref="ResizeResult.Success"/> if everything went fine.</returns> public ResizeResult ResizeVhdBlob(int newSizeInGb, Uri blobUri, string accountName, string accountKey) { NewSize = ByteSize.FromGigaBytes(newSizeInGb); // Check if blob exists blob = new CloudPageBlob(blobUri); if (!string.IsNullOrEmpty(accountName) && !string.IsNullOrEmpty(accountKey)) { blob = new CloudPageBlob(blobUri, new StorageCredentials(accountName, accountKey)); } try { if (!blob.Exists()) { Console.WriteLine("The specified blob does not exist."); return(ResizeResult.Error); } } catch (StorageException ex) { Console.WriteLine("The specified storage account credentials are invalid. " + ex.ToString()); return(ResizeResult.Error); } // Determine blob attributes Console.WriteLine("[{0}] Determining blob size...", DateTime.Now.ToShortTimeString()); blob.FetchAttributes(); originalLength = blob.Properties.Length; // Read current footer Console.WriteLine("[{0}] Reading VHD file format footer...", DateTime.Now.ToShortTimeString()); footer = new byte[512]; using (var stream = new MemoryStream()) { blob.DownloadRangeToStream(stream, originalLength - 512, 512); stream.Position = 0; stream.Read(footer, 0, 512); stream.Close(); } footerInstance = Footer.FromBytes(footer, 0); // Make sure this is a "fixed" disk if (footerInstance.DiskType != FileType.Fixed) { Console.WriteLine("The specified VHD blob is not a fixed-size disk. WindowsAzureDiskResizer can only resize fixed-size VHD files."); return(ResizeResult.Error); } if (footerInstance.CurrentSize >= (long)NewSize.Bytes) { // The specified VHD blob is larger than the specified new size. Shrinking disks is a potentially dangerous operation // Ask the user for confirmation return(ResizeResult.Shrink); } return(DoResizeVhdBlob()); }
public static byte[] GetBlobMd5Hash(this CloudPageBlob blob, BlobRequestOptions requestOptions) { blob.FetchAttributes(new AccessCondition(), requestOptions); if (String.IsNullOrEmpty(blob.Properties.ContentMD5)) { return(null); } return(Convert.FromBase64String(blob.Properties.ContentMD5)); }
public static byte[] GetBlobMd5Hash(this CloudPageBlob blob) { blob.FetchAttributes(); if (String.IsNullOrEmpty(blob.Properties.ContentMD5)) { return(null); } return(Convert.FromBase64String(blob.Properties.ContentMD5)); }
public void GetSnapshotBlobs() { string containerName = Utility.GenNameString("container"); string pageBlobName = Utility.GenNameString("page"); string blockBlobName = Utility.GenNameString("block"); Test.Info("Create test container and blobs"); CloudBlobContainer container = blobUtil.CreateContainer(containerName); try { ICloudBlob pageBlob = blobUtil.CreatePageBlob(container, pageBlobName); ICloudBlob blockBlob = blobUtil.CreateBlockBlob(container, blockBlobName); List <ICloudBlob> blobs = new List <ICloudBlob>(); pageBlob.FetchAttributes(); blockBlob.FetchAttributes(); int minSnapshot = 1; int maxSnapshot = 5; int count = random.Next(minSnapshot, maxSnapshot); int snapshotInterval = 1 * 1000; Test.Info("Create random snapshot for specified blobs"); for (int i = 0; i < count; i++) { CloudBlockBlob snapshot = ((CloudBlockBlob)blockBlob).CreateSnapshot(); snapshot.FetchAttributes(); blobs.Add(snapshot); Thread.Sleep(snapshotInterval); } blobs.Add(blockBlob); count = random.Next(minSnapshot, maxSnapshot); for (int i = 0; i < count; i++) { CloudPageBlob snapshot = ((CloudPageBlob)pageBlob).CreateSnapshot(); snapshot.FetchAttributes(); blobs.Add(snapshot); Thread.Sleep(snapshotInterval); } blobs.Add(pageBlob); Test.Assert(agent.GetAzureStorageBlob(string.Empty, containerName), Utility.GenComparisonData("Get-AzureStorageBlob with snapshot blobs", true)); Test.Assert(agent.Output.Count == blobs.Count, String.Format("Expect to retrieve {0} blobs, actually retrieved {1} blobs", blobs.Count, agent.Output.Count)); agent.OutputValidation(blobs); } finally { blobUtil.RemoveContainer(containerName); } }
/// <summary> /// refetches the blob attributes. this only needs to be done when fresher attributes /// than fetched when the wrapped page was first created /// </summary> /// <param name="accessCondition">the access condition</param> public void RefetchAttributes(bool disregardConcurrency) { try { var accessCondition = disregardConcurrency ? null : AccessCondition.GenerateIfMatchCondition(_pageBlob.Properties.ETag); Logger.Verbose("Fetching attributes for blob [{0}]", _pageBlob.Uri); _pageBlob.FetchAttributes(accessCondition); } catch (AzureStorage.StorageException ex) { throw HandleAndRemapCommonExceptions(ex); } }
public static void DownloadVHDFromCloud(CloudBlobClient blobStorage, string containerName, string blobName) { CloudBlobContainer container = blobStorage.GetContainerReference(containerName); CloudPageBlob pageBlob = container.GetPageBlobReference(blobName); // Get the length of the blob pageBlob.FetchAttributes(); long vhdLength = pageBlob.Properties.Length; long totalDownloaded = 0; Console.WriteLine("Vhd size: " + Megabytes(vhdLength)); // Create a new local file to write into FileStream fileStream = new FileStream(blobName, FileMode.Create, FileAccess.Write); fileStream.SetLength(128 * OneMegabyteAsBytes); // Download the valid ranges of the blob, and write them to the file IEnumerable <PageRange> pageRanges = pageBlob.GetPageRanges(); Stream blobStream = pageBlob.OpenRead(); foreach (PageRange range in pageRanges) { // EndOffset is inclusive... so need to add 1 int rangeSize = (int)(range.EndOffset + 1 - range.StartOffset); // Chop range into 4MB chucks, if needed for (int subOffset = 0; subOffset < rangeSize; subOffset += FourMegabyteAsBytes) { int subRangeSize = Math.Min(rangeSize - subOffset, FourMegabyteAsBytes); blobStream.Seek(range.StartOffset + subOffset, SeekOrigin.Begin); fileStream.Seek(range.StartOffset + subOffset, SeekOrigin.Begin); Console.WriteLine("Range: ~" + Megabytes(range.StartOffset + subOffset) + " + " + PrintSize(subRangeSize)); byte[] buffer = new byte[subRangeSize]; blobStream.Read(buffer, 0, subRangeSize); fileStream.Write(buffer, 0, subRangeSize); totalDownloaded += subRangeSize; if (totalDownloaded > 128 * OneMegabyteAsBytes) { break; } } if (totalDownloaded > 128 * OneMegabyteAsBytes) { break; } } Console.WriteLine("Downloaded " + Megabytes(totalDownloaded) + " of " + Megabytes(vhdLength)); }
private async Task <JobStatus> WaitForExit(CloudBlobContainer container, string blobName) { try { TimeSpan offset; { CloudPageBlob blob = container.GetPageBlobReference(blobName); blob.FetchAttributes(); DateTime lastValue = DateTime.Parse(blob.Metadata["heartbeat"]); offset = DateTime.UtcNow - lastValue; } while (true) { CloudPageBlob blob = container.GetPageBlobReference(blobName); blob.FetchAttributes(); if (blob.Metadata["status"] == "success") { return(JobStatus.Success); } else if (blob.Metadata["status"] == "failure") { return(JobStatus.Failure); } else if (blob.Metadata["status"] == "killed") { return(JobStatus.Cancelled); } DateTime newValue = DateTime.Parse(blob.Metadata["heartbeat"]); DateTime adjusted = newValue + offset; TimeSpan staleness = DateTime.UtcNow - adjusted; if (staleness.TotalSeconds > 20) { this.SetException(new ApplicationException("Job stopped writing heartbeats")); return(JobStatus.Cancelled); } await Task.Delay(2 * 1000); } } catch (Exception e) { this.SetException(new ApplicationException("Failed waiting for completion", e)); return(JobStatus.Cancelled); } }
/// <summary> /// Returns the size of the VHD file specified, in bytes. /// </summary> /// <param name="vhdFileUri">The location of the VHD file in the storage account, as a <see cref="Uri"/> object.</param> /// <param name="useDevelopment">True to use the Azure Storage Emulator, False to connect to the storage account using the other parameters.</param> /// <param name="accountName">The name of the account to use, if useDevelopment is false.</param> /// <param name="accountKey">The key of the account to use, if useDevelopment is false.</param> /// <returns></returns> public static long GetVhdSizeInContainer(Uri vhdFileUri, bool useDevelopment = true, string accountName = null, string accountKey = null) { var account = GetStorageAccount(useDevelopment, accountName, accountKey); var client = account.CreateCloudBlobClient(); var blob = new CloudPageBlob(vhdFileUri, client); if (blob.Exists()) { blob.FetchAttributes(); return(blob.Properties.Length); } else { return(0L); } }
private void PopulateCache() { if (this.blocks == null) { this.blocks = new Dictionary <string, long>(); this.pages = new Dictionary <string, long>(); // can happen when we are looking at cached results if (this.client == null) { return; } var cloudBlobdir = this.client.Container.GetDirectoryReference(this.path); var blobs = cloudBlobdir.ListBlobs(); foreach (IListBlobItem item in blobs) { if (item is CloudBlockBlob) { CloudBlockBlob blob = (CloudBlockBlob)item; blocks.Add(blob.Name, blob.Properties.Length); } else if (item is CloudPageBlob) { CloudPageBlob pageBlob = (CloudPageBlob)item; // not accurate //pages.Add(pageBlob.Name, pageBlob.Properties.Length); pageBlob.FetchAttributes(); var metadata = pageBlob.Metadata; if (metadata.ContainsKey("writePosition")) { long sz; if (long.TryParse(metadata["writePosition"], out sz)) { pages.Add(pageBlob.Name, sz); } } } else if (item is CloudBlobDirectory) { //CloudBlobDirectory directory = (CloudBlobDirectory)item; } } } }
/// <summary> /// Returns true if the blob exists, false otherwise /// </summary> /// <param name="blob"></param> /// <returns></returns> public static bool Exists(this CloudPageBlob blob) { try { blob.FetchAttributes(); return(true); } catch (StorageClientException e) { if (e.ErrorCode == StorageErrorCode.ResourceNotFound) { Trace.TraceWarning("CloudPageBlob {0} was not found due to StorageClientException {1}", blob.Uri, e); return(false); } Trace.TraceError("Unexecpted exception checking existence of CloudPageBlob {0} : {1}", blob.Uri, e); throw; } }
public void StorageExceptionVerifyXml() { Uri baseAddressUri = new Uri(TestBase.TargetTenantConfig.BlobServiceEndpoint); CloudBlobClient client = new CloudBlobClient(baseAddressUri, TestBase.StorageCredentials); CloudBlobContainer container = client.GetContainerReference(Guid.NewGuid().ToString("N")); try { container.Create(); CloudBlockBlob blob = container.GetBlockBlobReference("blob1"); byte[] buffer = new byte[1024]; Random random = new Random(); random.NextBytes(buffer); using (MemoryStream stream = new MemoryStream(buffer)) { blob.UploadFromStream(stream); } CloudPageBlob blob2 = container.GetPageBlobReference("blob1"); StorageException e = TestHelper.ExpectedException <StorageException>( () => blob2.FetchAttributes(), "Fetching attributes of a block blob using a page blob reference should fail"); using (Stream s = new MemoryStream()) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(s, e); s.Position = 0; // Reset stream position StorageException e2 = (StorageException)formatter.Deserialize(s); Assert.IsInstanceOfType(e2.InnerException, typeof(InvalidOperationException)); Assert.AreEqual(e.IsRetryable, e2.IsRetryable); Assert.AreEqual(e.RequestInformation.HttpStatusCode, e2.RequestInformation.HttpStatusCode); Assert.AreEqual(e.RequestInformation.HttpStatusMessage, e2.RequestInformation.HttpStatusMessage); } } finally { container.DeleteIfExists(); } }
// public static long pageBlobSize = 0; /// <summary> /// Based on this script: http://gallery.technet.microsoft.com/scriptcenter/Get-Billable-Size-of-32175802 /// </summary> /// <returns></returns> public static long GetActualDiskSize(this CloudPageBlob pageBlob, bool PageRange = false) { pageBlob.FetchAttributes(); if (PageRange) { long pageBlobSize = 0; long startOffset = 0; long rangeSize = 1024 * 1024 * 1024; //1 GB Console.Write("Calculating..."); while (startOffset < pageBlob.Properties.Length) { pageBlobSize += pageBlob.GetPageRanges(startOffset, rangeSize).Sum(r => 12 + (r.EndOffset - r.StartOffset)); startOffset += rangeSize; Console.Write("..."); } return(124 + pageBlob.Name.Length * 2 + pageBlob.Metadata.Sum(m => m.Key.Length + m.Value.Length + 3) + pageBlobSize); } else { return(124 + pageBlob.Name.Length * 2 + pageBlob.Metadata.Sum(m => m.Key.Length + m.Value.Length + 3) + pageBlob.GetPageRanges().Sum(r => 12 + (r.EndOffset - r.StartOffset))); } }
private static void Download(string accountName, string accountKey, string containerName, string blobName, string localFilePath, int segmentSize, int delayBeetweenChunksInSeconds) { var cloudStorageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=" + accountName + ";AccountKey=" + accountKey); var blobContainer = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference(containerName); CloudPageBlob blob = blobContainer.GetPageBlobReference(blobName); blob.FetchAttributes(); long startPosition = 0; if (File.Exists(localFilePath)) { log.AddLine("Local file exists. Resuming download..."); using (FileStream fs = new FileStream(localFilePath, FileMode.Open)) { fs.Seek(0, SeekOrigin.End); startPosition = fs.Position; } } long blobLength = blob.Properties.Length; var blobLengthRemaining = blobLength - startPosition; DateTime momentBeforeRead = DateTime.Now; double percentDone = 0; TimeSpan timeForOneRead = new TimeSpan(0); double speedBytesPerSecond = 0; TimeSpan timeRemaining = new TimeSpan(0); int secondsRemaining = 0; timeoutTimer = new Timer(Timer_Tick, null, Timeout.Infinite, Timeout.Infinite); while (blobLengthRemaining > 0) { long blockSize = Math.Min(segmentSize, blobLengthRemaining); byte[] blobContents = new byte[blockSize]; using (MemoryStream ms = new MemoryStream()) { timeoutTimer.Change(new TimeSpan(0, 0, 10), new TimeSpan(0, 0, 10)); blob.DownloadRangeToStream(ms, startPosition, blockSize); timeoutTimer.Change(Timeout.Infinite, Timeout.Infinite); ms.Position = 0; ms.Read(blobContents, 0, blobContents.Length); using (FileStream fs = new FileStream(localFilePath, FileMode.OpenOrCreate)) { fs.Position = startPosition; fs.Write(blobContents, 0, blobContents.Length); } } startPosition += blockSize; blobLengthRemaining -= blockSize; //blobLength:100=startPosition:x percentDone = startPosition * 100 / blobLength; timeForOneRead = DateTime.Now.Subtract(momentBeforeRead); momentBeforeRead = DateTime.Now; speedBytesPerSecond = blockSize / timeForOneRead.TotalSeconds; secondsRemaining = (int)Math.Round(blobLengthRemaining / speedBytesPerSecond); timeRemaining = new TimeSpan(0, 0, secondsRemaining); Console.SetCursorPosition(0, 19); Console.WriteLine(" "); Console.SetCursorPosition(0, 19); log.AddLine(string.Format("{0}%, {1}/{2} bytes, {3} kbytes/sec, remaining (dd:hh:mm:ss): {4}:{5}:{6}:{7}", percentDone, startPosition, blobLength, Math.Round(speedBytesPerSecond / 1024, 2), timeRemaining.Days, timeRemaining.Hours, timeRemaining.Minutes, timeRemaining.Seconds ) ); attempt = 1; if (delayBeetweenChunksInSeconds > 0) { Thread.Sleep(delayBeetweenChunksInSeconds); } } }
private static async Task PageBlobProcessAsync() { CloudStorageAccount storageAccount = null; CloudBlobContainer cloudBlobContainer = null; // Retrieve the connection string for use with the application. The storage connection string is stored // in an environment variable on the machine running the application called storageconnectionstring. // If the environment variable is created after the application is launched in a console or with Visual // Studio, the shell needs to be closed and reloaded to take the environment variable into account. string storageConnectionString = ConfigurationManager.AppSettings["StorageConnectionString"]; //Environment.GetEnvironmentVariable("StorageConnectionString"); //storageAccount = CloudStorageAccount.DevelopmentStorageAccount; // Check whether the connection string can be parsed. if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount)) { try { // Create the CloudBlobClient that represents the Blob storage endpoint for the storage account. CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); //127.0.0.1:10000/devstoreaccount1/testcont // Create a container called 'quickstartblobs' and append a GUID value to it to make the name unique. cloudBlobContainer = cloudBlobClient.GetContainerReference("testcontpageblob");// "quickstartblobs" + Guid.NewGuid().ToString()); await cloudBlobContainer.CreateIfNotExistsAsync(); Console.WriteLine("Created container '{0}'", cloudBlobContainer.Name); Console.WriteLine(); // Set the permissions so the blobs are public. BlobContainerPermissions permissions = new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }; await cloudBlobContainer.SetPermissionsAsync(permissions); byte[] data = new byte[512]; Random rnd = new Random(); rnd.NextBytes(data); // Get a reference to the blob address, then upload the file to the blob. // Use the value of localFileName for the blob name. CloudPageBlob cloudPageBlob = cloudBlobContainer.GetPageBlobReference("SamplePage"); cloudPageBlob.Create(512); cloudPageBlob.WritePages(new MemoryStream(data), 0); // List the blobs in the container. Console.WriteLine("Listing blobs in container."); BlobContinuationToken blobContinuationToken = null; do { var results = await cloudBlobContainer.ListBlobsSegmentedAsync(null, blobContinuationToken); // Get the value of the continuation token returned by the listing call. blobContinuationToken = results.ContinuationToken; foreach (IListBlobItem item in results.Results) { Console.WriteLine(item.Uri); } } while (blobContinuationToken != null); // Loop while the continuation token is not null. Console.WriteLine("Read the PageRanges"); cloudPageBlob.FetchAttributes(); Console.WriteLine("Blob length = {0}", cloudPageBlob.Properties.Length); IEnumerable <PageRange> ranges = cloudPageBlob.GetPageRanges(); Console.Write("{0}:<", "Writing Data From PageBlob"); foreach (PageRange range in ranges) { Console.Write("[{0}-{1}]", range.StartOffset, range.EndOffset); } Console.WriteLine(">"); Console.WriteLine("Delete the PageBlob"); await cloudPageBlob.DeleteAsync(); } catch (StorageException ex) { Console.WriteLine("Error returned from the service: {0}", ex.Message); } finally { // Console.WriteLine("Press any key to delete the sample files and example container."); Console.ReadLine(); // Clean up resources. This includes the container and the two temp files. //Console.WriteLine("Deleting the container and any blobs it contains"); if (cloudBlobContainer != null) { // await cloudBlobContainer.DeleteIfExistsAsync(); } } } else { Console.WriteLine( "A connection string has not been defined in the system environment variables. " + "Add a environment variable named 'storageconnectionstring' with your storage " + "connection string as a value."); } }
/// <summary> /// Based on this script: http://gallery.technet.microsoft.com/scriptcenter/Get-Billable-Size-of-32175802 /// </summary> /// <returns></returns> public static long GetActualDiskSize(this CloudPageBlob pageBlob) { pageBlob.FetchAttributes(); return(124 + pageBlob.Name.Length * 2 + pageBlob.Metadata.Sum(m => m.Key.Length + m.Value.Length + 3) + pageBlob.GetPageRanges().Sum(r => 12 + (r.EndOffset - r.StartOffset))); }