示例#1
0
        // Retrieve the list of regions in use for a page blob.
        // Return true on success, false if not found, throw exception on error.

        public bool GetPageRegions(string containerName, string blobName, out PageRange[] regions)
        {
            regions = null;

            try
            {
                CloudBlobContainer container = BlobClient.GetContainerReference(containerName);
                CloudPageBlob      blob      = container.GetPageBlobReference(blobName);

                IEnumerable <PageRange> regionList = blob.GetPageRanges();
                regions = new PageRange[regionList.Count()];
                int i = 0;
                foreach (PageRange region in regionList)
                {
                    regions[i++] = region;
                }
                return(true);
            }
            catch (StorageClientException ex)
            {
                if ((int)ex.StatusCode == 404)
                {
                    return(false);
                }

                throw;
            }
        }
        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);
        }
示例#3
0
        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 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));
        }
        /// <summary>
        /// Get for a Page Blob the page ranges
        /// </summary>
        /// <param name="containerName">the container name</param>
        /// <param name="blobName">the blob name</param>
        /// <param name="pageRanges">OUT - enumeration of page ranges</param>
        /// <returns>Return true on success, false if unable to create, throw exception on error.</returns>
        public bool GetPageRanges(string containerName, string blobName, out IEnumerable <PageRange> pageRanges)
        {
            try
            {
                CloudBlobContainer container = _blobClient.GetContainerReference(containerName);
                CloudPageBlob      blob      = container.GetPageBlobReference(blobName);
                pageRanges = blob.GetPageRanges();
                return(true);
            }
            catch (StorageException ex)
            {
                if (ex.RequestInformation.HttpStatusCode == 404)
                {
                    pageRanges = null;
                    return(false);
                }

                throw;
            }
        }
        private static bool ComparePageRange(CloudPageBlob pageBlob0, CloudPageBlob pageBlob1)
        {
            List <PageRange> pageRanges0 = pageBlob0.GetPageRanges().ToList();
            List <PageRange> pageRanges1 = pageBlob1.GetPageRanges().ToList();
            int numPageRanges0           = pageRanges0.Count();
            int numPageRanges1           = pageRanges1.Count();

            if (numPageRanges0 != numPageRanges1)
            {
                return(false);
            }
            pageRanges0 = pageRanges0.OrderBy(pageRange => pageRange.StartOffset).ToList();
            pageRanges1 = pageRanges1.OrderBy(pageRange => pageRange.StartOffset).ToList();
            for (int i = 0; i < numPageRanges0; ++i)
            {
                if (pageRanges0[i].StartOffset != pageRanges1[i].StartOffset ||
                    pageRanges0[i].EndOffset != pageRanges1[i].EndOffset)
                {
                    return(false);
                }
            }
            return(true);
        }
        // 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)));
            }
        }
示例#8
0
        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)));
 }
        //******************
        //*                *
        //*  ShowPageBlob  *
        //*                *
        //******************
        // Display properties for a specific page blob.

        public void ShowPageBlob(CloudPageBlob blob)
        {
            Cursor = Cursors.Wait;

            try
            {
                PagesTab.Visibility   = System.Windows.Visibility.Visible;
                ContentTab.Visibility = System.Windows.Visibility.Collapsed;

                PageBlob      = blob;
                IsBlobChanged = false;
                this.Title    = "View Blob - " + blob.Name;

                // Display blob properties.

                PropBlobType.Text = "Page";

                PropCacheControl.Text = blob.Properties.CacheControl;

                PropContainer.Text = blob.Container.Name;

                PropContentDisposition.Text = blob.Properties.ContentDisposition;

                PropContentEncoding.Text = blob.Properties.ContentEncoding;

                PropContentLanguage.Text = blob.Properties.ContentLanguage;

                PropContentMD5.Text = blob.Properties.ContentMD5;

                PropContentType.Text = blob.Properties.ContentType;

                if (blob.CopyState != null)
                {
                    PropCopyState.Text = blob.CopyState.ToString();
                }

                if (blob.Properties.ETag != null)
                {
                    PropETag.Text = blob.Properties.ETag.Replace("\"", String.Empty).Replace("0x", String.Empty);
                }

                if (blob.IsSnapshot)
                {
                    PropIsSnapshot.Text = "True";
                }
                else
                {
                    PropIsSnapshot.Text = "False";
                }

                PropLastModified.Text = blob.Properties.LastModified.ToString();

                PropLeaseDuration.Text = blob.Properties.LeaseDuration.ToString();

                PropLeaseState.Text = blob.Properties.LeaseState.ToString();

                PropLeaseStatus.Text = blob.Properties.LeaseStatus.ToString();

                PropLength.Text = blob.Properties.Length.ToString();

                PropName.Text = blob.Name;

                PropParent.Text = blob.Parent.Container.Name;

                PropSnapshotQualifiedStorageUri.Text = blob.SnapshotQualifiedStorageUri.ToString().Replace("; ", ";\n");

                PropSnapshotQualifiedUri.Text = blob.SnapshotQualifiedUri.ToString().Replace("; ", ";\n");

                if (blob.SnapshotTime.HasValue)
                {
                    PropSnapshotTime.Text = blob.SnapshotTime.ToString();
                }

                PropStorageUri.Text = blob.StorageUri.ToString().Replace("; ", ";\n");

                PropStreamMinimumReadSizeInBytes.Text = blob.StreamMinimumReadSizeInBytes.ToString();

                PropStreamWriteSizeInBytes.Text = blob.StreamWriteSizeInBytes.ToString();

                PropUri.Text = blob.Uri.ToString().Replace("; ", ";\n");

                // Read page ranges in use and display in Pages tab.

                MaxPageNumber = (blob.Properties.Length / 512) - 1;

                IEnumerable <Microsoft.WindowsAzure.Storage.Blob.PageRange> ranges = PageBlob.GetPageRanges();

                PageRanges.Items.Clear();

                long startPage, endPage;
                int  rangeCount = 0;
                if (ranges != null)
                {
                    long pages = PageBlob.Properties.Length / 512;
                    foreach (Microsoft.WindowsAzure.Storage.Blob.PageRange range in ranges)
                    {
                        startPage = (range.StartOffset) / 512;
                        endPage   = (range.EndOffset) / 512;
                        long offset    = range.StartOffset;
                        long endOffset = offset + 512 - 1;
                        int  index     = 0;
                        for (long page = startPage; page <= endPage; page++)
                        {
                            index = PageRanges.Items.Add("Page " + page.ToString() + ": (" + offset.ToString() + " - " + endOffset.ToString() + ")");
                            rangeCount++;
                            offset    += 512;
                            endOffset += 512;
                        }
                    }
                }
                if (rangeCount == 0)
                {
                    PageRanges.Items.Add("None - no pages allocated");
                }

                Cursor = Cursors.Arrow;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }