public void DeleteDirectory(string directoryName) { directoryName = StorageUtils.NormalizeDirectoryName(directoryName); CloudBlobDirectory directory = PublicContainer.GetDirectoryReference(directoryName); foreach (IListBlobItem item in directory.ListBlobsSegmentedAsync(null).GetAwaiter().GetResult().Results) { if (item.GetType() == typeof(CloudBlockBlob)) { CloudBlockBlob blob = (CloudBlockBlob)item; blob.DeleteAsync().GetAwaiter().GetResult(); } else if (item.GetType() == typeof(CloudPageBlob)) { CloudPageBlob pageBlob = (CloudPageBlob)item; pageBlob.DeleteAsync().GetAwaiter().GetResult(); } else if (item.GetType() == typeof(CloudBlobDirectory)) { CloudBlobDirectory dir = (CloudBlobDirectory)item; DeleteDirectory(dir.Prefix); } } }
/// <summary> /// <see cref="IDevice.RemoveSegmentAsync(int, AsyncCallback, IAsyncResult)"/> /// </summary> /// <param name="segment"></param> /// <param name="callback"></param> /// <param name="result"></param> public override void RemoveSegmentAsync(int segment, AsyncCallback callback, IAsyncResult result) { if (this.blobs.TryRemove(segment, out BlobEntry blob)) { CloudPageBlob pageBlob = blob.PageBlob; if (this.underLease) { this.BlobManager.ConfirmLeaseAsync().GetAwaiter().GetResult(); } if (!this.BlobManager.CancellationToken.IsCancellationRequested) { pageBlob.DeleteAsync(cancellationToken: this.BlobManager.CancellationToken) .ContinueWith((Task t) => { if (t.IsFaulted) { this.BlobManager?.HandleBlobError(nameof(RemoveSegmentAsync), "could not remove page blob for segment", pageBlob?.Name, t.Exception, false); } callback(result); }); } } }
private async Task InitStream(string accountName, string accountKey, string containerName, bool isPublic, bool deleteIfExists) { _container = Utils.InitContainer(accountName, accountKey, containerName, isPublic); // Get a reference to the page blob that will be created. _pageBlob = _container.GetPageBlobReference(_streamName); BlobRequestOptions options = new BlobRequestOptions(); options.MaximumExecutionTime = MaxExecutionTime; bool exists = await Utils.WrapInRetry(_logger, async() => { return(await _pageBlob.ExistsAsync(options, null)); }); if (exists) { if (deleteIfExists) { await Utils.WrapInRetry(_logger, async() => { await _pageBlob.DeleteAsync(DeleteSnapshotsOption.None, null, options, null); }); } else { throw new ApplicationException("Trying to open existing page blob " + _streamName); } } await Utils.WrapInRetry(_logger, async() => { await _pageBlob.CreateAsync(_streamSize, null, options, null); }); await Utils.WrapInRetry(_logger, async() => { _pageBlob.Metadata["writePosition"] = 0.ToString(); await _pageBlob.SetMetadataAsync(null, options, null); await _pageBlob.SetPropertiesAsync(null, options, null); }); }
/// <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(); }
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(); }
/// <summary> /// <see cref="IDevice.RemoveSegmentAsync(int, AsyncCallback, IAsyncResult)"/> /// </summary> /// <param name="segment"></param> /// <param name="callback"></param> /// <param name="result"></param> public override void RemoveSegmentAsync(int segment, AsyncCallback callback, IAsyncResult result) { if (this.blobs.TryRemove(segment, out BlobEntry entry)) { CloudPageBlob pageBlob = entry.PageBlob; Task deletionTask = this.BlobManager.PerformWithRetriesAsync( null, this.underLease, "CloudPageBlob.DeleteAsync", "DeleteDeviceSegment", "", pageBlob.Name, 5000, true, async(numAttempts) => { await pageBlob.DeleteAsync(cancellationToken: this.PartitionErrorHandler.Token); return(1); }); deletionTask.ContinueWith((Task t) => callback(result)); } }
/// <summary> /// Delete the device blobs in storage. /// </summary> /// <returns></returns> Task DeleteAsync() { Task Delete(BlobEntry entry) { CloudPageBlob pageBlob = entry.PageBlob; return(this.BlobManager.PerformWithRetriesAsync( BlobManager.AsynchronousStorageWriteMaxConcurrency, this.underLease, "CloudPageBlob.DeleteAsync", "DeleteDevice", "", pageBlob.Name, 5000, false, async(numAttempts) => { await pageBlob.DeleteAsync(cancellationToken: this.PartitionErrorHandler.Token); return 1; })); } return(Task.WhenAll(this.blobs.Values.Select(Delete).ToList())); }
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 --"); }
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."); } }
/// <inheritdoc /> public Task DeleteAsync(CancellationToken cancellationToken) { return(_sdk.DeleteAsync(DeleteSnapshotsOption.None, accessCondition: null, options: null, operationContext: null, cancellationToken: cancellationToken)); }
/// <inheritdoc /> public Task DeleteAsync(CancellationToken cancellationToken) { return(_sdk.DeleteAsync(cancellationToken)); }
public static void Delete(this CloudPageBlob blob) { blob.DeleteAsync().Wait(); }
/// <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(); }