private static async void ReadPageBlob(CloudPageBlob pageBlob) { var samplePagedata = new byte[512]; await pageBlob.DownloadRangeToByteArrayAsync(samplePagedata, 0, 0, samplePagedata.Length); }
public override byte[] ReadSectors(long sectorIndex, int sectorCount) { var buffer = new byte[BytesPerSector * sectorCount]; cloudBlob.DownloadRangeToByteArrayAsync(buffer, 0, BytesPerSector * sectorIndex, buffer.Length).Wait(); return(buffer); }
/// <summary> /// Basic operations to work with page blobs /// </summary> /// <returns>Task</returns> private static async Task BasicStoragePageBlobOperationsAsync() { const string PageBlobName = "samplepageblob"; string pageBlobContainerName = "demopageblobcontainer-" + Guid.NewGuid(); // Retrieve storage account information from connection string // How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting("StorageConnectionString")); // Create a blob client for interacting with the blob service. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Create a container for organizing blobs within the storage account. Console.WriteLine("1. Creating Container"); CloudBlobContainer container = blobClient.GetContainerReference(pageBlobContainerName); await container.CreateIfNotExistsAsync(); // Create a page blob in the newly created container. Console.WriteLine("2. Creating Page Blob"); CloudPageBlob pageBlob = container.GetPageBlobReference(PageBlobName); await pageBlob.CreateAsync(512 * 2 /*size*/); // size needs to be multiple of 512 bytes // Write to a page blob Console.WriteLine("2. Write to a Page Blob"); byte[] samplePagedata = new byte[512]; Random random = new Random(); random.NextBytes(samplePagedata); await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length); // List all blobs in this container. Because a container can contain a large number of blobs the results // are returned in segments (pages) with a maximum of 5000 blobs per segment. You can define a smaller size // using the maxResults parameter on ListBlobsSegmentedAsync. Console.WriteLine("3. List Blobs in Container"); BlobContinuationToken token = null; do { BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(token); token = resultSegment.ContinuationToken; foreach (IListBlobItem blob in resultSegment.Results) { // Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory Console.WriteLine("{0} (type: {1}", blob.Uri, blob.GetType()); } } while (token != null); // Read from a page blob //Console.WriteLine("4. Read from a Page Blob"); int bytesRead = await pageBlob.DownloadRangeToByteArrayAsync(samplePagedata, 0, 0, samplePagedata.Count()); // Clean up after the demo Console.WriteLine("6. Delete page Blob"); await pageBlob.DeleteIfExistsAsync(); Console.WriteLine("7. Delete Container"); await container.DeleteIfExistsAsync(); }
/// <summary> /// Basic operations to work with page blobs /// </summary> /// <returns>Task</returns> private static async Task BasicStoragePageBlobOperations() { const string PageBlobName = "samplepageblob"; // Retrieve storage account information from connection string // How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx CloudStorageAccount storageAccount = storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); // Create a blob client for interacting with the blob service. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Create a container for organizing blobs within the storage account. Console.WriteLine("1. Creating Container"); CloudBlobContainer container = blobClient.GetContainerReference("democontainerpageblob"); await container.CreateIfNotExistsAsync(); // Create a page blob to the newly creating container. Page blob needs to be Console.WriteLine("2. Creating Page Blob"); CloudPageBlob pageBlob = container.GetPageBlobReference(PageBlobName); await pageBlob.CreateAsync(512 * 2 /*size*/); // size needs to be multiple of 512 bytes // Write to a page blob Console.WriteLine("2. Write to a Page Blob"); byte[] samplePagedata = new byte[512]; Random random = new Random(); random.NextBytes(samplePagedata); await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length); // List all blobs in this container Console.WriteLine("3. List Blobs in Container"); foreach (IListBlobItem blob in container.ListBlobs()) { // Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory // Use blob.GetType() and cast to appropriate type to gain access to properties specific to each type Console.WriteLine("{0} (type: {1})\n", blob.Uri, blob.GetType()); } // Read from a page blob Console.WriteLine("4. Read from a Page Blob"); int bytesRead = await pageBlob.DownloadRangeToByteArrayAsync(samplePagedata, 0, 0, samplePagedata.Count()); // Clean up after the demo Console.WriteLine("5. Delete page Blob and container"); await pageBlob.DeleteAsync(); await container.DeleteAsync(); }
protected async Task <byte[]> GetUsedBytesWrittenToLastPageAsync(CloudPageBlob pageBlob, long offsetOfLastPage, CancellationToken cancellationToken) { byte[] pageBlobReadBuffer = new byte[ByteCountInPage]; int bytesRead = await pageBlob.DownloadRangeToByteArrayAsync(pageBlobReadBuffer, 0, offsetOfLastPage, ByteCountInPage, CreateAccessCondition(pageBlob), this.RequestOptions, this.OperationContext, cancellationToken); //if (bytesRead != ByteCountInPage) // throw new ApplicationException("Failure to download the requested byte count from the page blob! " + // $"(bytes expected: {ByteCountInPage}, bytes downloaded: {bytesRead})"); string value = this.Encoding.GetString(pageBlobReadBuffer).TrimEnd('\x00'); return(this.Encoding.GetBytes(value)); }
private static async Task SaveObjectInBlob <T>(CloudBlobContainer container, string pageBlobName, T obj) { CloudPageBlob pageBlob = container.GetPageBlobReference(pageBlobName); await pageBlob.CreateAsync(512 * 2 /*size*/); // size needs to be multiple of 512 bytes byte[] samplePagedata = new byte[2048]; var serializedObject = ObjectToByteArray <T>(obj); serializedObject.CopyTo(samplePagedata, 0); await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length); int bytesRead = await pageBlob.DownloadRangeToByteArrayAsync(samplePagedata, 0, 0, samplePagedata.Length); System.Console.WriteLine("Read:" + bytesRead + " bytes"); }
private static async Task ManageBlob(CloudBlobContainer container, string pageBlobName) { CloudPageBlob pageBlob = container.GetPageBlobReference(pageBlobName); await pageBlob.CreateAsync(512 * 2 /*size*/); // size needs to be multiple of 512 bytes byte[] samplePagedata = new byte[512]; Random random = new Random(); random.NextBytes(samplePagedata); await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length); int bytesRead = await pageBlob.DownloadRangeToByteArrayAsync(samplePagedata, 0, 0, samplePagedata.Length); System.Console.WriteLine("Read:" + bytesRead + " bytes"); }
public async Task BasicPageBlobOperationsAsync() { const string PageBlobName = "samplepageblob"; CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting("StorageConnectionString")); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); Console.WriteLine("1. Creating Container"); CloudBlobContainer container = blobClient.GetContainerReference("democontainerpageblob"); await container.CreateIfNotExistsAsync(); Console.WriteLine("2. Creating Page Blob"); CloudPageBlob pageBlob = container.GetPageBlobReference(PageBlobName); Console.WriteLine("2. Write to a Page Blob"); byte[] samplePagedata = new byte[512]; Random random = new Random(); random.NextBytes(samplePagedata); await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length); Console.WriteLine("3. List Blobs in Container"); BlobContinuationToken token = null; do { BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(token); token = resultSegment.ContinuationToken; foreach (IListBlobItem blob in resultSegment.Results) { Console.WriteLine("{0} (type: {1}", blob.Uri, blob.GetType()); } } while (token != null); int bytesRead = await pageBlob.DownloadRangeToByteArrayAsync(samplePagedata, 0, 0, samplePagedata.Count()); Console.WriteLine("5. Delete page Blob"); await pageBlob.DeleteAsync(); }
private async Task BasicStoragePageBlobOperationsAsync() { WriteLine("-- Testing PageBlob --"); const string PageBlobName = "samplepageblob"; // Create a blob client for interacting with the blob service. CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient(); // Create a container for organizing blobs within the storage account. WriteLine("1. Creating Container"); CloudBlobContainer container = blobClient.GetContainerReference("democontainerpageblob"); await container.CreateIfNotExistsAsync(); // Create a page blob in the newly created container. WriteLine("2. Creating Page Blob"); CloudPageBlob pageBlob = container.GetPageBlobReference(PageBlobName); await pageBlob.CreateAsync(512 * 2 /*size*/); // size needs to be multiple of 512 bytes // Write to a page blob WriteLine("2. Write to a Page Blob"); byte[] samplePagedata = new byte[512]; Random random = new Random(); random.NextBytes(samplePagedata); await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length); // List all blobs in this container. Because a container can contain a large number of blobs the results // are returned in segments (pages) with a maximum of 5000 blobs per segment. You can define a smaller size // using the maxResults parameter on ListBlobsSegmentedAsync. WriteLine("3. List Blobs in Container"); BlobContinuationToken token = null; do { BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(token); token = resultSegment.ContinuationToken; foreach (IListBlobItem blob in resultSegment.Results) { // Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory WriteLine(string.Format("{0} (type: {1}", blob.Uri, blob.GetType())); } } while (token != null); // Read from a page blob WriteLine("4. Read from a Page Blob"); int bytesRead = await pageBlob.DownloadRangeToByteArrayAsync(samplePagedata, 0, 0, samplePagedata.Length); // Clean up after the demo WriteLine("5. Delete page Blob"); await pageBlob.DeleteAsync(); // When you delete a container it could take several seconds before you can recreate a container with the same // name - hence to enable you to run the demo in quick succession the container is not deleted. If you want // to delete the container uncomment the line of code below. WriteLine("6. Delete Container"); await container.DeleteAsync(); WriteLine("-- Test Complete --"); }
/// <summary> /// Demonstrates how to do backup using incremental snapshots. See this article on /// backing up Azure virtual machine disks with incremental snapshots: /// https://azure.microsoft.com/en-us/documentation/articles/storage-incremental-snapshots/ /// The article also describes how to restore a disk from incremental snapshots. /// </summary> /// <returns>Task</returns> public static async Task IncrementalSnapshotBackupAsync() { const int TotalBlobSize = 512 * 6; // The total amount of data in the blob const int UpdateWriteSize = 512 * 2; // The amount of data to update in the blob const int UpdateWriteOffset = 512 * 2; // The offset at which to write updated data const int ClearPagesOffset = 512 * 5; // The offset at which to begin clearing page data const int ClearPagesSize = 512; // The amount of data to clear in the blob string SimulationID = Guid.NewGuid().ToString(); // The simulation ID string ContainerName = "container-" + SimulationID; // The name of the primary container string PageBlobName = "binary-data"; // The name of the primary page blob // Retrieve storage account information from connection strings // How to create storage connection strings - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString")); CloudStorageAccount backupStorageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("BackupStorageConnectionString")); // Create blob clients for interacting with the blob service. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobClient backupBlobClient = backupStorageAccount.CreateCloudBlobClient(); // Create containers for organizing blobs within the storage accounts. container = blobClient.GetContainerReference(ContainerName); backupContainer = backupBlobClient.GetContainerReference("copy-of-" + ContainerName); try { // The call below will fail if the sample is configured to use the storage emulator // in the connection string, but the emulator is not running. BlobRequestOptions requestOptions = new BlobRequestOptions() { RetryPolicy = new NoRetry() }; await container.CreateIfNotExistsAsync(requestOptions, null); await backupContainer.CreateIfNotExistsAsync(requestOptions, null); } catch (StorageException) { Console.WriteLine("If you are running with the default connection string, please make " + "sure you have started the storage emulator. Press the Windows key and type Azure " + "Storage to select and run it from the list of applications - then restart the sample."); Console.ReadLine(); throw; } // Create, write to, and snapshot a page blob in the newly created container. CloudPageBlob pageBlob = container.GetPageBlobReference(PageBlobName); await pageBlob.CreateAsync(TotalBlobSize); byte[] samplePagedata = new byte[TotalBlobSize]; Random random = new Random(); random.NextBytes(samplePagedata); await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length); CloudPageBlob pageBlobSnap = await pageBlob.CreateSnapshotAsync(); // Create a blob in the backup account CloudPageBlob pageBlobBackup = backupContainer.GetPageBlobReference("copy-of-" + PageBlobName); // Copy to the blob in the backup account. Notice that the <isServiceCopy> flag here is // set to 'false'. This forces data to be first downloaded from the source, then uploaded // to the destination. If <isServiceCopy> were set to 'true' we could avoid the I/O of // downloading and then uploading, but could not guarantee that the copy completed even // after the local task completed. Since the remainder of this sample depends on the // successful completion of this operation, we must set <isServiceCopy> to 'false.' await TransferManager.CopyAsync(pageBlobSnap, pageBlobBackup, false); // Snapshot the backup copy CloudPageBlob pageBlobBackupSnap = await pageBlob.CreateSnapshotAsync(); // Change the contents of the original page blob (note: we must convert the byte array to a // stream in order to write to a non-zero offset in the page blob) and snapshot. byte[] updatePageData = new byte[UpdateWriteSize]; random.NextBytes(updatePageData); Stream updatePageDataStream = new MemoryStream(updatePageData); await pageBlob.WritePagesAsync(updatePageDataStream, UpdateWriteOffset, null); await pageBlob.ClearPagesAsync(ClearPagesOffset, ClearPagesSize); CloudPageBlob newPageBlobSnap = await pageBlob.CreateSnapshotAsync(); // Get the incremental changes and write to the backup. IEnumerable <PageDiffRange> changedPages = await newPageBlobSnap.GetPageRangesDiffAsync(pageBlobSnap.SnapshotTime.Value); foreach (PageDiffRange pageRange in changedPages) { // If this page range is cleared, remove the old data in the backup. if (pageRange.IsClearedPageRange) { await pageBlobBackup.ClearPagesAsync( pageRange.StartOffset, pageRange.EndOffset - pageRange.StartOffset + 1); } // If this page range is not cleared, write the new data to the backup. else { byte[] toWrite = new byte[pageRange.EndOffset - pageRange.StartOffset + 1]; await newPageBlobSnap.DownloadRangeToByteArrayAsync( toWrite, 0, pageRange.StartOffset, pageRange.EndOffset - pageRange.StartOffset + 1); Stream toWriteStream = new MemoryStream(toWrite); await pageBlobBackup.WritePagesAsync(toWriteStream, pageRange.StartOffset, null); } } // Snapshot the backup blob and delete the old snapshot from the primary account CloudPageBlob pageBlobBackupSnapv2 = await pageBlobBackup.CreateSnapshotAsync(); await pageBlobSnap.DeleteAsync(); }
/// <summary> /// Basic operations to work with page blobs /// </summary> /// <returns>Task</returns> private static async Task BasicStoragePageBlobOperationsAsync() { Stopwatch timer = new Stopwatch(); const string PageBlobName = "samplepageblob"; // Retrieve storage account information from connection string // How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting("StorageConnectionString")); // Create a blob client for interacting with the blob service. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Create a container for organizing blobs within the storage account. Console.WriteLine("1. Creating Container"); CloudBlobContainer container = blobClient.GetContainerReference("democontainerpageblob"); await container.CreateIfNotExistsAsync(); Console.WriteLine("Starting Timer.."); timer.Start(); // Create a page blob in the newly created container. Console.WriteLine("2. Creating Page Blob"); CloudPageBlob pageBlob = container.GetPageBlobReference(PageBlobName); await pageBlob.CreateAsync(512 * 2 /*size*/); // size needs to be multiple of 512 bytes // Write to a page blob Console.WriteLine("2. Write to a Page Blob"); byte[] samplePagedata = new byte[512]; Random random = new Random(); random.NextBytes(samplePagedata); await pageBlob.UploadFromByteArrayAsync(samplePagedata, 0, samplePagedata.Length); Console.WriteLine("Stopping Timer.."); timer.Stop(); TimeSpan ts = timer.Elapsed; Console.WriteLine("Time to upload the page blob(milliseconds): " + ts.Milliseconds.ToString()); timer.Reset(); // List all blobs in this container. Because a container can contain a large number of blobs the results // are returned in segments (pages) with a maximum of 5000 blobs per segment. You can define a smaller size // using the maxResults parameter on ListBlobsSegmentedAsync. Console.WriteLine("3. List Blobs in Container"); BlobContinuationToken token = null; do { BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(token); token = resultSegment.ContinuationToken; foreach (IListBlobItem blob in resultSegment.Results) { // Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory Console.WriteLine("{0} (type: {1}", blob.Uri, blob.GetType()); } } while (token != null); // Read from a page blob //Console.WriteLine("4. Read from a Page Blob"); int bytesRead = await pageBlob.DownloadRangeToByteArrayAsync(samplePagedata, 0, 0, samplePagedata.Count()); // Clean up after the demo Console.WriteLine("5. Delete page Blob"); await pageBlob.DeleteAsync(); // When you delete a container it could take several seconds before you can recreate a container with the same // name - hence to enable you to run the demo in quick succession the container is not deleted. If you want // to delete the container uncomment the line of code below. //Console.WriteLine("6. Delete Container"); //await container.DeleteAsync(); }
/// <summary> /// Download data to the stream. /// </summary> /// <param name="blob">The cloud blob.</param> /// <param name="target">The byte array where to write to.</param> /// <param name="index">The offset index.</param> /// <param name="blobOffset">The starting offset of the data range, in bytes.</param> /// <param name="length">The length of the data to download from the blob, in bytes.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The async task.</returns> public async Task <int> DownloadAsync(CloudPageBlob blob, byte[] target, int index, long blobOffset, long length, CancellationToken cancellationToken) { return(await blob.DownloadRangeToByteArrayAsync(target, index, blobOffset, length, cancellationToken)); }