private static async Task<int> ReadTrafficReportsAsync(CloudBlobContainer container, int maxEvents) { var count = 0; BlobContinuationToken dirToken = null; do { var dirResult = await container.ListBlobsSegmentedAsync(dirToken); dirToken = dirResult.ContinuationToken; foreach (var dirItem in dirResult.Results) { if (dirItem is CloudBlobDirectory) { var dir = dirItem as CloudBlobDirectory; BlobContinuationToken blobToken = null; var blobResult = await dir.ListBlobsSegmentedAsync(blobToken); foreach (var blobItem in blobResult.Results) { if (blobItem is CloudBlockBlob) { var blob = blobItem as CloudBlockBlob; var content = await blob.DownloadTextAsync(); count++; using (var writer = new StreamWriter(string.Format("{0:D5}.xml", count))) { await writer.WriteAsync(content); } Console.WriteLine(count); if (count >= maxEvents) break; } } } if (count >= maxEvents) break; } } while (dirToken != null && count < maxEvents); return count; }
public async Task CopyContainer(CloudBlobContainer sourceContainer, string destination) { var uri = new Uri(sourceContainer.Uri.AbsoluteUri.TrimEnd('/') + '/'); destination = Path.Combine(destination, sourceContainer.Name); BlobContinuationToken continuationToken = null; do { var segments = await sourceContainer.ListBlobsSegmentedAsync(prefix: null, useFlatBlobListing: true, blobListingDetails: BlobListingDetails.Metadata, maxResults: MaxParallelDownloads, currentToken: continuationToken, options: null, operationContext: null); var tasks = new BlockingCollection<Task>(MaxParallelDownloads); Parallel.ForEach(segments.Results.Cast<CloudBlockBlob>(), srcFile => { var relativePath = uri.MakeRelativeUri(srcFile.Uri); var destLocation = Path.Combine(destination, relativePath.OriginalString); if (File.Exists(destLocation) && File.GetLastWriteTimeUtc(destLocation) == srcFile.Properties.LastModified) { // If the file looks unchanged, skip it. return; } Directory.CreateDirectory(Path.GetDirectoryName(destLocation)); tasks.Add(srcFile.DownloadToFileAsync(destLocation, FileMode.Create)); }); await Task.WhenAll(tasks); continuationToken = segments.ContinuationToken; } while (continuationToken != null); }
private static async Task TestAccessAsync(BlobContainerPublicAccessType accessType, CloudBlobContainer container, CloudBlob inputBlob) { StorageCredentials credentials = new StorageCredentials(); container = new CloudBlobContainer(container.Uri, credentials); CloudPageBlob blob = new CloudPageBlob(inputBlob.Uri, credentials); OperationContext context = new OperationContext(); BlobRequestOptions options = new BlobRequestOptions(); if (accessType.Equals(BlobContainerPublicAccessType.Container)) { await blob.FetchAttributesAsync(); await container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, null, options, context); await container.FetchAttributesAsync(); } else if (accessType.Equals(BlobContainerPublicAccessType.Blob)) { await blob.FetchAttributesAsync(); await TestHelper.ExpectedExceptionAsync( async () => await container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, null, options, context), context, "List blobs while public access does not allow for listing", HttpStatusCode.NotFound); await TestHelper.ExpectedExceptionAsync( async () => await container.FetchAttributesAsync(null, options, context), context, "Fetch container attributes while public access does not allow", HttpStatusCode.NotFound); } else { await TestHelper.ExpectedExceptionAsync( async () => await blob.FetchAttributesAsync(null, options, context), context, "Fetch blob attributes while public access does not allow", HttpStatusCode.NotFound); await TestHelper.ExpectedExceptionAsync( async () => await container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, null, options, context), context, "List blobs while public access does not allow for listing", HttpStatusCode.NotFound); await TestHelper.ExpectedExceptionAsync( async () => await container.FetchAttributesAsync(null, options, context), context, "Fetch container attributes while public access does not allow", HttpStatusCode.NotFound); } }
private IEnumerable <CloudBlob> GetCloudBlobs(string prefix) { BlobContinuationToken continuationToken = null; while (true) { var result = _delegateContainer.ListBlobsSegmentedAsync(AddRootPath(prefix), true, BlobListingDetails.None, null, continuationToken, null, null).Result; var blobs = result.Results.OfType <CloudBlob>(); foreach (var blob in blobs) { yield return(blob); } if (result.ContinuationToken == null) { break; } continuationToken = result.ContinuationToken; } }
public async Task <List <AzureBlobItem> > GetBlobListAsync(int ticketReference, bool useFlatListing = true) { //Container Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer blobContainer = await GetContainerAsync(); var list = new List <AzureBlobItem>(); BlobContinuationToken token = null; do { BlobResultSegment resultSegment = await blobContainer.ListBlobsSegmentedAsync(ticketReference.ToString(), useFlatListing, new BlobListingDetails(), 100, token, null, null); token = resultSegment.ContinuationToken; foreach (IListBlobItem item in resultSegment.Results) { list.Add(new AzureBlobItem(item)); } } while (token != null); return(list.OrderBy(i => i.Folder).ThenBy(i => i.Name).ToList()); }
public async Task CopyContainer(CloudBlobContainer srcContainer, CloudBlobContainer destContainer) { BlobContinuationToken continuationToken = null; do { var segments = await srcContainer.ListBlobsSegmentedAsync(prefix: null, useFlatBlobListing: true, blobListingDetails: BlobListingDetails.Metadata, maxResults: MaxParallelDownloads, currentToken: continuationToken, options: null, operationContext: null); var tasks = new BlockingCollection<Task>(MaxParallelDownloads); Parallel.ForEach(segments.Results.Cast<CloudBlockBlob>(), srcFile => { var destLocation = srcFile.Uri.AbsoluteUri.Replace(srcContainer.Name, destContainer.Name); var destFile = destContainer.GetBlockBlobReference(destLocation); var copyTask = destFile.StartCopyFromBlobAsync(srcFile); tasks.Add(copyTask); }); await Task.WhenAll(tasks); continuationToken = segments.ContinuationToken; } while (continuationToken != null); }
public async Task CloudBlobContainerListBlobsAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); List <string> blobNames = await CreateBlobsAsync(container, 3, BlobType.PageBlob); BlobResultSegment results = await container.ListBlobsSegmentedAsync(null); Assert.AreEqual(blobNames.Count, results.Results.Count()); foreach (IListBlobItem blobItem in results.Results) { Assert.IsInstanceOfType(blobItem, typeof(CloudPageBlob)); Assert.IsTrue(blobNames.Remove(((CloudPageBlob)blobItem).Name)); } } finally { container.DeleteIfExistsAsync().Wait(); } }
private async Task<IEnumerable<IListBlobItem>> TransferBlobs( bool rename, CloudBlobContainer fromContainer, CloudBlobContainer toContainer, CancellationToken cancellationToken) { var requestOptions = new BlobRequestOptions() { RetryPolicy = new LinearRetry(TimeSpan.FromSeconds(2), 5) }; var leaseId = Guid.NewGuid().ToString(); var leaseResult = string.Empty; var autoEvent = new AutoResetEvent(false); var waitEvent = new AutoResetEvent(false); var leaseTimer = new Timer( async s => { try { if (string.IsNullOrEmpty(leaseResult)) { leaseResult = await fromContainer.AcquireLeaseAsync( TimeSpan.FromSeconds(60), leaseId, null, requestOptions, null, cancellationToken); waitEvent.Set(); } else { await fromContainer.RenewLeaseAsync( AccessCondition.GenerateLeaseCondition(leaseId), requestOptions, null, cancellationToken); } } catch (StorageException exception) { if (exception.RequestInformation.HttpStatusCode == (int)HttpStatusCode.Conflict) { this.logger.LogInformation("Staging container already has a lease."); } } }, autoEvent, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(50)); waitEvent.WaitOne(); try { BlobContinuationToken token = null; var blobList = new List<CopySpec>(); do { var result = await fromContainer.ListBlobsSegmentedAsync(token, cancellationToken); token = result.ContinuationToken; blobList.AddRange(result.Results.OfType<CloudBlob>().Select(b => new CopySpec() {SourceBlob = b})); } while (token != null); // Copy var copiedBlobList = new List<CopySpec>(); foreach (var blob in blobList) { var srcBlobName = blob.SourceBlob.Uri.Segments[2]; var blobName = rename ? $"{Path.GetFileNameWithoutExtension(srcBlobName)}{Guid.NewGuid().ToString().Replace("-", "")}{Path.GetExtension(srcBlobName)}" : srcBlobName; var destBlobRef = toContainer.GetBlobReference(blobName); blob.DestBlob = destBlobRef; try { await destBlobRef.StartCopyAsync( blob.SourceBlob.Uri, AccessCondition.GenerateEmptyCondition(), AccessCondition.GenerateEmptyCondition(), requestOptions, null, cancellationToken); copiedBlobList.Add(blob); } catch (Exception e) { this.logger.LogError($"Error while copying {blobName}. {e.ToString()}"); } } this.logger.LogInformation($"Started copying {copiedBlobList.Count} blobs"); var blobsToRemove = new List<CopySpec>(); var blobsToCheck = copiedBlobList.Select(b => b.SourceBlob.Uri.AbsoluteUri).ToList(); do { var withProperties = copiedBlobList.Select(b => { b.DestBlob.FetchAttributes(AccessCondition.GenerateEmptyCondition(), requestOptions, null); return b; }).ToList(); foreach (var blob in withProperties) { if (blob.DestBlob.CopyState.Status == CopyStatus.Aborted || blob.DestBlob.CopyState.Status == CopyStatus.Failed) { this.logger.LogError($"Cannot copy {blob.DestBlob.Uri}"); blobsToCheck.Remove(blob.SourceBlob.Uri.AbsoluteUri); } if (blob.DestBlob.CopyState.Status != CopyStatus.Success) continue; blobsToRemove.Add(blob); blobsToCheck.Remove(blob.SourceBlob.Uri.AbsoluteUri); } } while (blobsToCheck.Any()); this.logger.LogInformation($"{blobsToRemove.Count} blobs copied."); foreach (var blob in blobsToRemove) { try { await blob.SourceBlob.DeleteAsync( DeleteSnapshotsOption.IncludeSnapshots, AccessCondition.GenerateEmptyCondition(), requestOptions, null, cancellationToken); this.logger.LogInformation($"Deleted {blob.SourceBlob.Uri.AbsoluteUri}"); } catch (StorageException e) { if (e.RequestInformation.HttpStatusCode == (int)HttpStatusCode.NotFound) { this.logger.LogInformation($"Blob not found {blob.SourceBlob.Uri}"); } else { this.logger.LogError(e.ToString()); } } catch (Exception exception) { this.logger.LogError(exception.ToString()); } }; leaseTimer.Dispose(); await fromContainer.ReleaseLeaseAsync(AccessCondition.GenerateLeaseCondition(leaseId), requestOptions, null, cancellationToken); this.logger.LogInformation($"{blobsToRemove.Count} blobs deleted."); return copiedBlobList.Where(b => b.DestBlob.CopyState.Status == CopyStatus.Success).Select(b => b.DestBlob); } catch (Exception exception) { this.logger.LogCritical(exception.ToString()); return default(IEnumerable<IListBlobItem>); } }
public async Task CloudBlobDirectoryFlatListingAsync() { foreach (String delimiter in Delimiters) { CloudBlobClient client = GenerateCloudBlobClient(); client.DefaultDelimiter = delimiter; string name = GetRandomContainerName(); CloudBlobContainer container = client.GetContainerReference(name); try { await container.CreateAsync(); if (await CloudBlobDirectorySetupWithDelimiterAsync(container, delimiter)) { BlobResultSegment segment = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter, false, BlobListingDetails.None, null, null, null, null); List <IListBlobItem> simpleList1 = new List <IListBlobItem>(); simpleList1.AddRange(segment.Results); while (segment.ContinuationToken != null) { segment = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter, false, BlobListingDetails.None, null, segment.ContinuationToken, null, null); simpleList1.AddRange(segment.Results); } Assert.IsTrue(simpleList1.Count == 3); IListBlobItem item11 = simpleList1.ElementAt(0); Assert.IsTrue(item11.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "Blob1")); IListBlobItem item12 = simpleList1.ElementAt(1); Assert.IsTrue(item12.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter)); IListBlobItem item13 = simpleList1.ElementAt(2); Assert.IsTrue(item13.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter)); CloudBlobDirectory midDir2 = (CloudBlobDirectory)item13; BlobResultSegment segment2 = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter + "MidDir1", true, BlobListingDetails.None, null, null, null, null); List <IListBlobItem> simpleList2 = new List <IListBlobItem>(); simpleList2.AddRange(segment2.Results); while (segment2.ContinuationToken != null) { segment2 = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter + "MidDir1", true, BlobListingDetails.None, null, segment2.ContinuationToken, null, null); simpleList2.AddRange(segment2.Results); } Assert.IsTrue(simpleList2.Count == 2); IListBlobItem item21 = simpleList2.ElementAt(0); Assert.IsTrue(item21.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir1" + delimiter + "EndBlob1")); IListBlobItem item22 = simpleList2.ElementAt(1); Assert.IsTrue(item22.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir2" + delimiter + "EndBlob2")); BlobResultSegment segment3 = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter + "MidDir1" + delimiter, false, BlobListingDetails.None, null, null, null, null); List <IListBlobItem> simpleList3 = new List <IListBlobItem>(); simpleList3.AddRange(segment3.Results); while (segment3.ContinuationToken != null) { segment3 = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter + "MidDir1" + delimiter, false, BlobListingDetails.None, null, segment3.ContinuationToken, null, null); simpleList3.AddRange(segment3.Results); } Assert.IsTrue(simpleList3.Count == 2); IListBlobItem item31 = simpleList3.ElementAt(0); Assert.IsTrue(item31.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir1" + delimiter)); IListBlobItem item32 = simpleList3.ElementAt(1); Assert.IsTrue(item32.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir2" + delimiter)); BlobResultSegment segment4 = await midDir2.ListBlobsSegmentedAsync(true, BlobListingDetails.None, null, null, null, null); List <IListBlobItem> simpleList4 = new List <IListBlobItem>(); simpleList4.AddRange(segment4.Results); while (segment4.ContinuationToken != null) { segment4 = await midDir2.ListBlobsSegmentedAsync(true, BlobListingDetails.None, null, segment4.ContinuationToken, null, null); simpleList4.AddRange(segment4.Results); } Assert.IsTrue(simpleList4.Count == 2); IListBlobItem item41 = simpleList4.ElementAt(0); Assert.IsTrue(item41.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir1" + delimiter + "EndBlob1")); IListBlobItem item42 = simpleList4.ElementAt(1); Assert.IsTrue(item42.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir2" + delimiter + "EndBlob2")); } } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } } }
private async Task<int> ProcessContainerAsync(CloudBlobContainer container, MemoryStream[] buffers, int maxParallelSize) { Log($"Processing container `{container.Name}`", 0); var count = 0; BlobResultSegment blobListSegment = null; Func<bool> hasMoreSegments = () => blobListSegment?.ContinuationToken != null; do { blobListSegment = await container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.None, null, blobListSegment?.ContinuationToken, null, null, CancellationToken.None); var blobs = blobListSegment.Results.OfType<CloudBlob>() .Where(blob => !(BlobFilter?.Invoke(blob) == false)) .ToList(); Log($"Fetched {(!hasMoreSegments() ? "final" : "partial")} listing of {blobs.Count} blobs", 0); if (buffers.Length > 0) { using (var queue = new BlockingCollection<Func<MemoryStream, Task<CloudBlob>>>()) { var sequentialBlobs = new List<CloudBlob>(); foreach (var blob in blobs) { AddBlobToQueue(blob, queue, sequentialBlobs, maxParallelSize); } queue.CompleteAdding(); Log($"Transfering {sequentialBlobs.Count} blobs sequentially and {queue.Count} blobs in parallel", 1); var pool = Enumerable.Range(0, buffers.Length) .Select(id => Worker(queue, buffers[id])) .ToList(); foreach (var blob in sequentialBlobs) { using (await ArchiveStreamLock.LockAsync()) { await WriteEntryAsync(blob); } } await Task.WhenAll(pool); } } else { foreach (var blob in blobs) { await WriteEntryAsync(blob); } } count += blobs.Count; } while (hasMoreSegments()); return count; }
private static async Task<List<CloudBlockBlob>> GetLogBlobsToReplay(CloudBlobContainer container, string containerRoot, DateTimeOffset fromDate, DateTimeOffset untilDate) { // Get the log file blobs that should be replayed var blobsToReplay = new List<CloudBlockBlob>(); BlobContinuationToken continuationToken = null; do { var segment = await container.ListBlobsSegmentedAsync(containerRoot, useFlatBlobListing: true, blobListingDetails: BlobListingDetails.Metadata, maxResults: null, currentToken: continuationToken, options: null, operationContext: null); continuationToken = segment.ContinuationToken; foreach (var blob in segment.Results) { var cloudBlockBlob = blob as CloudBlockBlob; if (cloudBlockBlob != null && cloudBlockBlob.Properties.LastModified.HasValue) { // Check if the blob timestamp falls within our prefix range var timestamp = cloudBlockBlob.Properties.LastModified.Value; if (timestamp >= fromDate && timestamp <= untilDate) { blobsToReplay.Add(cloudBlockBlob); } } } } while (continuationToken != null); return blobsToReplay; }
private static void TestAccessTask(BlobContainerPublicAccessType accessType, CloudBlobContainer container, CloudBlob inputBlob) { StorageCredentials credentials = new StorageCredentials(); container = new CloudBlobContainer(container.Uri, credentials); CloudPageBlob blob = new CloudPageBlob(inputBlob.Uri, credentials); if (accessType.Equals(BlobContainerPublicAccessType.Container)) { blob.FetchAttributesAsync().Wait(); BlobContinuationToken token = null; do { BlobResultSegment results = container.ListBlobsSegmented(token); results.Results.ToArray(); token = results.ContinuationToken; } while (token != null); container.FetchAttributesAsync().Wait(); } else if (accessType.Equals(BlobContainerPublicAccessType.Blob)) { blob.FetchAttributesAsync().Wait(); TestHelper.ExpectedExceptionTask( container.ListBlobsSegmentedAsync(null), "List blobs while public access does not allow for listing", HttpStatusCode.NotFound); TestHelper.ExpectedExceptionTask( container.FetchAttributesAsync(), "Fetch container attributes while public access does not allow", HttpStatusCode.NotFound); } else { TestHelper.ExpectedExceptionTask( blob.FetchAttributesAsync(), "Fetch blob attributes while public access does not allow", HttpStatusCode.NotFound); TestHelper.ExpectedExceptionTask( container.ListBlobsSegmentedAsync(null), "List blobs while public access does not allow for listing", HttpStatusCode.NotFound); TestHelper.ExpectedExceptionTask( container.FetchAttributesAsync(), "Fetch container attributes while public access does not allow", HttpStatusCode.NotFound); } }
private IAsyncEnumerable<ICloudBlob> ListBlobs(CloudBlobContainer container, string prefix, BlobListingDetails options) { // Clean up the prefix if required prefix = prefix == null ? null : SafePath.MakeSafeFilePath(prefix); return AsyncEnumerableEx.Create<ICloudBlob>(async (y) => { BlobContinuationToken token = new BlobContinuationToken(); try { do { var segment = await container.ListBlobsSegmentedAsync(prefix, true, options, null, token, null, null, y.CancellationToken).ConfigureAwait(false); LeoTrace.WriteLine("Listed blob segment for prefix: " + prefix); foreach (var blob in segment.Results.OfType<ICloudBlob>()) { await y.YieldReturn(blob).ConfigureAwait(false); } token = segment.ContinuationToken; } while (token != null && !y.CancellationToken.IsCancellationRequested); } catch (StorageException e) { if (e.RequestInformation.HttpStatusCode != 404) { throw e.Wrap(container.Name + "_" + (prefix ?? string.Empty) + "*"); } } }); }
public void CloudBlobSnapshotTask() { CloudBlobContainer container = GetRandomContainerReference(); try { container.CreateAsync().Wait(); MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudAppendBlob appendBlob = container.GetAppendBlobReference(BlobName); appendBlob.CreateOrReplaceAsync().Wait(); appendBlob.AppendBlockAsync(originalData, null).Wait(); CloudBlob blob = container.GetBlobReference(BlobName); blob.FetchAttributesAsync().Wait(); CloudBlob snapshot1 = blob.SnapshotAsync().Result; Assert.AreEqual(blob.Properties.ETag, snapshot1.Properties.ETag); Assert.AreEqual(blob.Properties.LastModified, snapshot1.Properties.LastModified); Assert.IsTrue(snapshot1.IsSnapshot); Assert.IsNotNull(snapshot1.SnapshotTime, "Snapshot does not have SnapshotTime set"); Assert.AreEqual(blob.Uri, snapshot1.Uri); Assert.AreNotEqual(blob.SnapshotQualifiedUri, snapshot1.SnapshotQualifiedUri); Assert.AreNotEqual(snapshot1.Uri, snapshot1.SnapshotQualifiedUri); Assert.IsTrue(snapshot1.SnapshotQualifiedUri.Query.Contains("snapshot")); CloudBlob snapshot2 = blob.SnapshotAsync().Result; Assert.IsTrue(snapshot2.SnapshotTime.Value > snapshot1.SnapshotTime.Value); snapshot1.FetchAttributesAsync().Wait(); snapshot2.FetchAttributesAsync().Wait(); blob.FetchAttributesAsync().Wait(); AssertAreEqual(snapshot1.Properties, blob.Properties); CloudBlob snapshot1Clone = new CloudBlob(new Uri(blob.Uri + "?snapshot=" + snapshot1.SnapshotTime.Value.ToString("O")), blob.ServiceClient.Credentials); Assert.IsNotNull(snapshot1Clone.SnapshotTime, "Snapshot clone does not have SnapshotTime set"); Assert.AreEqual(snapshot1.SnapshotTime.Value, snapshot1Clone.SnapshotTime.Value); snapshot1Clone.FetchAttributesAsync().Wait(); AssertAreEqual(snapshot1.Properties, snapshot1Clone.Properties); CloudBlob snapshotCopy = container.GetBlobReference("blob2"); snapshotCopy.StartCopyAsync(snapshot1.Uri, null, null, null, null).Wait(); WaitForCopy(snapshotCopy); Assert.AreEqual(CopyStatus.Success, snapshotCopy.CopyState.Status); using (Stream snapshotStream = snapshot1.OpenReadAsync().Result) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } appendBlob.CreateOrReplaceAsync().Wait(); blob.FetchAttributesAsync().Wait(); using (Stream snapshotStream = snapshot1.OpenReadAsync().Result) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } List <IListBlobItem> blobs = container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, null, null, null) .Result .Results .ToList(); Assert.AreEqual(4, blobs.Count); AssertAreEqual(snapshot1, (CloudBlob)blobs[0]); AssertAreEqual(snapshot2, (CloudBlob)blobs[1]); AssertAreEqual(blob, (CloudBlob)blobs[2]); AssertAreEqual(snapshotCopy, (CloudBlob)blobs[3]); } finally { container.DeleteIfExistsAsync().Wait(); } }
async Task UseContainerSAS(string sas) { //Try performing container operations with the SAS provided. //Return a reference to the container using the SAS URI. CloudBlobContainer container = new CloudBlobContainer(new Uri(sas)); string date = DateTime.Now.ToString(); try { //Write operation: write a new blob to the container. CloudBlockBlob blob = container.GetBlockBlobReference("xamarinblob_" + date + ".txt"); string blobContent = textEntry.Text; //"This blob was created with a shared access signature granting write permissions to the container. "; MemoryStream msWrite = new MemoryStream(Encoding.UTF8.GetBytes(blobContent)); msWrite.Position = 0; using (msWrite) { await blob.UploadFromStreamAsync(msWrite); } Debug.WriteLine("Write operation succeeded for SAS " + sas); Debug.WriteLine(""); } catch (Exception e) { Debug.WriteLine("Write operation failed for SAS " + sas); Debug.WriteLine("Additional error information: " + e.Message); Debug.WriteLine(""); } try { //Read operation: Get a reference to one of the blobs in the container and read it. CloudBlockBlob blob = container.GetBlockBlobReference("xamarinblob_" + date + ".txt"); string data = await blob.DownloadTextAsync(); Debug.WriteLine("Read operation succeeded for SAS " + sas); Debug.WriteLine("Blob contents: " + data); output.Text = "READ BACK FROM SERVER:" + Environment.NewLine + Environment.NewLine + data; } catch (Exception e) { Debug.WriteLine("Additional error information: " + e.Message); Debug.WriteLine("Read operation failed for SAS " + sas); Debug.WriteLine(""); } try { //Delete operation: Delete a blob in the container. CloudBlockBlob blob = container.GetBlockBlobReference("xamarinblob_" + date + ".txt"); //CloudBlockBlob blob = container.GetBlockBlobReference("sasblob_” + date + “.txt"); /* Additional error information: <?xml version="1.0" encoding="utf-16"?><!--An exception has occurred. For more information please deserialize this message via RequestResult.TranslateFromExceptionMessage.--> <RequestResult> <HTTPStatusCode>404</HTTPStatusCode> <HttpStatusMessage>The specified blob does not exist.</HttpStatusMessage> <TargetLocation>Primary</TargetLocation> <ServiceRequestID>26b235cb-0001-0009-345d-873767000000</ServiceRequestID> <ContentMd5 /> <Etag /> <RequestDate>Tue, 05 May 2015 10:58:59 GMT</RequestDate> <StartTime>Tue, 05 May 2015 17:58:59 GMT</StartTime> <EndTime>Tue, 05 May 2015 17:58:59 GMT</EndTime> <Error> <Code>BlobNotFound</Code> <Message>The specified blob does not exist. RequestId:26b235cb-0001-0009-345d-873767000000 Time:2015-05-05T17:58:59.2841607Z</Message> </Error> <ExceptionInfo> <Type /> <HResult>-2147467259</HResult> <Message>The specified blob does not exist.</Message> <Source>Microsoft.WindowsAzure.Storage</Source> <StackTrace> at Microsoft.WindowsAzure.Storage.Shared.Protocol.HttpResponseParsers.ProcessExpectedStatusCodeNoException[NullType] (HttpStatusCode expectedStatusCode, HttpStatusCode actualStatusCode, Microsoft.WindowsAzure.Storage.Core.NullType retVal, Microsoft.WindowsAzure.Storage.Core.Executor.StorageCommandBase`1 cmd, System.Exception ex) [0x00004] in <filename unknown>:0 at Microsoft.WindowsAzure.Storage.Shared.Protocol.HttpResponseParsers.ProcessExpectedStatusCodeNoException[NullType] (HttpStatusCode expectedStatusCode, System.Net.Http.HttpResponseMessage resp, Microsoft.WindowsAzure.Storage.Core.NullType retVal, Microsoft.WindowsAzure.Storage.Core.Executor.StorageCommandBase`1 cmd, System.Exception ex) [0x00011] in <filename unknown>:0 at Microsoft.WindowsAzure.Storage.Blob.CloudBlob+<>c__DisplayClass77.<GetBlobImpl>b__75 (Microsoft.WindowsAzure.Storage.Core.Executor.RESTCommand`1 cmd, System.Net.Http.HttpResponseMessage resp, System.Exception ex, Microsoft.WindowsAzure.Storage.OperationContext ctx) [0x00014] in <filename unknown>:0 at Microsoft.WindowsAzure.Storage.Core.Executor.Executor+<ExecuteAsyncInternal>d__0`1[Microsoft.WindowsAzure.Storage.Core.NullType].MoveNext () [0x0059a] in <filename unknown>:0 </StackTrace> </ExceptionInfo> </RequestResult> */ // await blob.DeleteAsync(); //Debug.WriteLine("Delete operation succeeded for SAS " + sas); //Debug.WriteLine(""); } catch (Exception e) { Debug.WriteLine("Delete operation failed for SAS " + sas); Debug.WriteLine("Additional error information: " + e.Message); //Debug.WriteLine(""); } try { output.Text += Environment.NewLine + Environment.NewLine; // List blobs // //http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/#list-the-blobs-in-a-container BlobContinuationToken continuationToken = null; var x = await container.ListBlobsSegmentedAsync(continuationToken); foreach (IListBlobItem item in x.Results) { if (item.GetType() == typeof(CloudBlockBlob)) { CloudBlockBlob blob = (CloudBlockBlob)item; Debug.WriteLine("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri); output.Text += blob.Uri + Environment.NewLine; } else if (item.GetType() == typeof(CloudPageBlob)) { CloudPageBlob pageBlob = (CloudPageBlob)item; Debug.WriteLine("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri); output.Text += pageBlob.Uri + Environment.NewLine; } else if (item.GetType() == typeof(CloudBlobDirectory)) { CloudBlobDirectory directory = (CloudBlobDirectory)item; Debug.WriteLine("Directory: {0}", directory.Uri); output.Text += directory.Uri + Environment.NewLine; } } } catch (Exception e) { Debug.WriteLine("Delete operation failed for SAS " + sas); Debug.WriteLine("Additional error information: " + e.Message); //Debug.WriteLine(""); } }
/// <summary> /// Lists blobs in the specified container using a hierarchical listing, and calls this method recursively to return the contents of each /// virtual directory. Reads the properties on each blob or virtual directory returned and writes them to the console window. /// </summary> /// <param name="container">A CloudBlobContainer object.</param> /// <param name="prefix">The blob prefix.</param> /// <returns>A Task object.</returns> private static async Task ListBlobsHierarchicalListingAsync(CloudBlobContainer container, string prefix) { // List blobs in segments. Console.WriteLine("List blobs (hierarchical listing):"); Console.WriteLine(); // Enumerate the result segment returned. BlobContinuationToken continuationToken = null; BlobResultSegment resultSegment = null; try { // Call ListBlobsSegmentedAsync recursively and enumerate the result segment returned, while the continuation token is non-null. // When the continuation token is null, the last segment has been returned and execution can exit the loop. // Note that blob snapshots cannot be listed in a hierarchical listing operation. do { resultSegment = await container.ListBlobsSegmentedAsync(prefix, false, BlobListingDetails.Metadata, null, null, null, null); foreach (var blobItem in resultSegment.Results) { Console.WriteLine("************************************"); Console.WriteLine(blobItem.Uri); // A hierarchical listing returns both virtual directories and blobs. // Call recursively with the virtual directory prefix to enumerate the contents of each virtual directory. if (blobItem is CloudBlobDirectory) { PrintVirtualDirectoryProperties((CloudBlobDirectory)blobItem); CloudBlobDirectory dir = blobItem as CloudBlobDirectory; await ListBlobsHierarchicalListingAsync(container, dir.Prefix); } else { // Write out blob properties and metadata. PrintBlobPropertiesAndMetadata((CloudBlob)blobItem); } } Console.WriteLine(); // Get the continuation token, if there are additional segments of results. continuationToken = resultSegment.ContinuationToken; } while (continuationToken != null); } catch (StorageException e) { Console.WriteLine(e.Message); Console.ReadLine(); throw; } }
public async Task CloudBlobSnapshotAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName); await blockBlob.UploadFromStreamAsync(originalData); CloudBlob blob = container.GetBlobReference(BlobName); await blob.FetchAttributesAsync(); Assert.IsFalse(blob.IsSnapshot); Assert.IsNull(blob.SnapshotTime, "Root blob has SnapshotTime set"); Assert.IsFalse(blob.SnapshotQualifiedUri.Query.Contains("snapshot")); Assert.AreEqual(blob.Uri, blob.SnapshotQualifiedUri); CloudBlob snapshot1 = await blob.SnapshotAsync(); Assert.AreEqual(blob.Properties.ETag, snapshot1.Properties.ETag); Assert.AreEqual(blob.Properties.LastModified, snapshot1.Properties.LastModified); Assert.IsTrue(snapshot1.IsSnapshot); Assert.IsNotNull(snapshot1.SnapshotTime, "Snapshot does not have SnapshotTime set"); Assert.AreEqual(blob.Uri, snapshot1.Uri); Assert.AreNotEqual(blob.SnapshotQualifiedUri, snapshot1.SnapshotQualifiedUri); Assert.AreNotEqual(snapshot1.Uri, snapshot1.SnapshotQualifiedUri); Assert.IsTrue(snapshot1.SnapshotQualifiedUri.Query.Contains("snapshot")); CloudBlob snapshot2 = await blob.SnapshotAsync(); Assert.IsTrue(snapshot2.SnapshotTime.Value > snapshot1.SnapshotTime.Value); await snapshot1.FetchAttributesAsync(); await snapshot2.FetchAttributesAsync(); await blob.FetchAttributesAsync(); AssertAreEqual(snapshot1.Properties, blob.Properties); CloudBlob snapshot1Clone = new CloudBlob(new Uri(blob.Uri + "?snapshot=" + snapshot1.SnapshotTime.Value.ToString("O")), blob.ServiceClient.Credentials); Assert.IsNotNull(snapshot1Clone.SnapshotTime, "Snapshot clone does not have SnapshotTime set"); Assert.AreEqual(snapshot1.SnapshotTime.Value, snapshot1Clone.SnapshotTime.Value); await snapshot1Clone.FetchAttributesAsync(); AssertAreEqual(snapshot1.Properties, snapshot1Clone.Properties); CloudBlob snapshotCopy = container.GetBlobReference("blob2"); await snapshotCopy.StartCopyAsync(TestHelper.Defiddler(snapshot1.Uri)); await WaitForCopyAsync(snapshotCopy); Assert.AreEqual(CopyStatus.Success, snapshotCopy.CopyState.Status); using (Stream snapshotStream = (await snapshot1.OpenReadAsync())) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } await blockBlob.PutBlockListAsync(new List <string>()); await blob.FetchAttributesAsync(); using (Stream snapshotStream = (await snapshot1.OpenReadAsync())) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, null, null, null); List <IListBlobItem> blobs = resultSegment.Results.ToList(); Assert.AreEqual(4, blobs.Count); AssertAreEqual(snapshot1, (CloudBlob)blobs[0]); AssertAreEqual(snapshot2, (CloudBlob)blobs[1]); AssertAreEqual(blob, (CloudBlob)blobs[2]); AssertAreEqual(snapshotCopy, (CloudBlob)blobs[3]); } finally { container.DeleteIfExistsAsync().Wait(); } }
public void CloudBlobSoftDeleteSnapshotTask() { CloudBlobContainer container = GetRandomContainerReference(); try { //Enables a delete retention policy on the blob with 1 day of default retention days container.ServiceClient.EnableSoftDelete(); container.Create(); // Upload some data to the blob. MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudAppendBlob appendBlob = container.GetAppendBlobReference(BlobName); appendBlob.UploadFromStream(originalData); CloudBlob blob = container.GetBlobReference(BlobName); //create snapshot via api CloudBlob snapshot = blob.Snapshot(); //create snapshot via write protection appendBlob.UploadFromStream(originalData); //we should have 2 snapshots 1 regular and 1 deleted: there is no way to get only the deleted snapshots but the below listing will get both snapshot types int blobCount = 0; int deletedSnapshotCount = 0; int snapShotCount = 0; BlobContinuationToken ct = null; do { foreach (IListBlobItem item in container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.Snapshots | BlobListingDetails.Deleted, null, ct, null, null) .Result .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); if (blobItem.IsSnapshot) { snapShotCount++; } if (blobItem.IsDeleted) { Assert.IsNotNull(blobItem.Properties.DeletedTime); Assert.AreEqual(blobItem.Properties.RemainingDaysBeforePermanentDelete, 0); deletedSnapshotCount++; } blobCount++; } } while (ct != null); Assert.AreEqual(blobCount, 3); Assert.AreEqual(deletedSnapshotCount, 1); Assert.AreEqual(snapShotCount, 2); blobCount = 0; ct = null; do { foreach (IListBlobItem item in container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.Snapshots, null, ct, null, null) .Result .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); Assert.IsFalse(blobItem.IsDeleted); Assert.IsNull(blobItem.Properties.DeletedTime); Assert.IsNull(blobItem.Properties.RemainingDaysBeforePermanentDelete); blobCount++; } } while (ct != null); Assert.AreEqual(blobCount, 2); //Delete Blob and snapshots blob.Delete(DeleteSnapshotsOption.IncludeSnapshots); Assert.IsFalse(blob.Exists()); Assert.IsFalse(snapshot.Exists()); blobCount = 0; ct = null; do { foreach (IListBlobItem item in container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.All, null, ct, null, null) .Result .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); Assert.IsTrue(blobItem.IsDeleted); Assert.IsNotNull(blobItem.Properties.DeletedTime); Assert.AreEqual(blobItem.Properties.RemainingDaysBeforePermanentDelete, 0); blobCount++; } }while(ct != null); Assert.AreEqual(blobCount, 3); blob.Undelete(); blob.FetchAttributes(); Assert.IsFalse(blob.IsDeleted); Assert.IsNull(blob.Properties.DeletedTime); Assert.IsNull(blob.Properties.RemainingDaysBeforePermanentDelete); blobCount = 0; ct = null; do { foreach (IListBlobItem item in container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.All, null, ct, null, null) .Result .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); Assert.IsFalse(blobItem.IsDeleted); Assert.IsNull(blobItem.Properties.DeletedTime); Assert.IsNull(blobItem.Properties.RemainingDaysBeforePermanentDelete); blobCount++; } }while(ct != null); Assert.AreEqual(blobCount, 3); } finally { container.ServiceClient.DisableSoftDelete(); container.DeleteIfExists(); } }
public void CloudBlobSoftDeleteNoSnapshotTask() { CloudBlobContainer container = GetRandomContainerReference(); try { //Enables a delete retention policy on the blob with 1 day of default retention days container.ServiceClient.EnableSoftDelete(); container.CreateAsync().Wait(); // Upload some data to the blob. MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudAppendBlob appendBlob = container.GetAppendBlobReference(BlobName); appendBlob.CreateOrReplaceAsync().Wait(); appendBlob.AppendBlockAsync(originalData, null).Wait(); CloudBlob blob = container.GetBlobReference(BlobName); Assert.IsTrue(blob.ExistsAsync().Result); Assert.IsFalse(blob.IsDeleted); blob.DeleteAsync().Wait(); Assert.IsFalse(blob.ExistsAsync().Result); int blobCount = 0; BlobContinuationToken ct = null; do { foreach (IListBlobItem item in container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.All, null, ct, null, null) .Result .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); Assert.IsTrue(blobItem.IsDeleted); Assert.IsNotNull(blobItem.Properties.DeletedTime); Assert.AreEqual(blobItem.Properties.RemainingDaysBeforePermanentDelete, 0); blobCount++; } } while (ct != null); Assert.AreEqual(blobCount, 1); blob.UndeleteAsync().Wait(); blob.FetchAttributes(); Assert.IsFalse(blob.IsDeleted); Assert.IsNull(blob.Properties.DeletedTime); Assert.IsNull(blob.Properties.RemainingDaysBeforePermanentDelete); blobCount = 0; ct = null; do { foreach (IListBlobItem item in container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.All, null, ct, null, null) .Result .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); Assert.IsFalse(blobItem.IsDeleted); Assert.IsNull(blobItem.Properties.DeletedTime); Assert.IsNull(blobItem.Properties.RemainingDaysBeforePermanentDelete); blobCount++; } } while (ct != null); Assert.AreEqual(blobCount, 1); } finally { container.ServiceClient.DisableSoftDelete(); container.DeleteIfExists(); } }
/// <summary> /// Basic operations to work with block blobs /// </summary> /// <returns>A Task object.</returns> private static async Task BasicStorageBlockBlobOperationsWithAccountSASAsync() { const string ImageToUpload = "HelloWorld.png"; string containerName = ContainerPrefix + Guid.NewGuid(); // Get an account SAS token. string sasToken = GetAccountSASToken(); // Use the account SAS token to create authentication credentials. StorageCredentials accountSAS = new StorageCredentials(sasToken); // Informational: Print the Account SAS Signature and Token. Console.WriteLine(); Console.WriteLine("Account SAS Signature: " + accountSAS.SASSignature); Console.WriteLine("Account SAS Token: " + accountSAS.SASToken); Console.WriteLine(); // Get the URI for the container. Uri containerUri = GetContainerUri(containerName); // Get a reference to a container using the URI and the SAS token. CloudBlobContainer container = new CloudBlobContainer(containerUri, accountSAS); try { // Create a container for organizing blobs within the storage account. Console.WriteLine("1. Creating Container using Account SAS"); await container.CreateIfNotExistsAsync(); } catch (StorageException e) { Console.WriteLine(e.ToString()); Console.WriteLine("If you are running with the default configuration, 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; } try { // To view the uploaded blob in a browser, you have two options. The first option is to use a Shared Access Signature (SAS) token to delegate // access to the resource. See the documentation links at the top for more information on SAS. The second approach is to set permissions // to allow public access to blobs in this container. Uncomment the line below to use this approach. Then you can view the image // using: https://[InsertYourStorageAccountNameHere].blob.core.windows.net/democontainer/HelloWorld.png // await container.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }); // Upload a BlockBlob to the newly created container Console.WriteLine("2. Uploading BlockBlob"); CloudBlockBlob blockBlob = container.GetBlockBlobReference(ImageToUpload); await blockBlob.UploadFromFileAsync(ImageToUpload); // List all the blobs in the container 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); // Download a blob to your file system Console.WriteLine("4. Download Blob from {0}", blockBlob.Uri.AbsoluteUri); await blockBlob.DownloadToFileAsync(string.Format("./CopyOf{0}", ImageToUpload), FileMode.Create); // Create a read-only snapshot of the blob Console.WriteLine("5. Create a read-only snapshot of the blob"); CloudBlockBlob blockBlobSnapshot = await blockBlob.CreateSnapshotAsync(null, null, null, null); // Delete the blob and its snapshots. Console.WriteLine("6. Delete block Blob and all of its snapshots"); await blockBlob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots, null, null, null); } catch (Exception e) { Console.WriteLine(e.Message); Console.ReadLine(); throw; } finally { // Clean up after the demo. // Note that it is not necessary to delete all of the blobs in the container first; they will be deleted // with the container. Console.WriteLine("7. Delete Container"); await container.DeleteIfExistsAsync(); } }
private async Task<IEnumerable<IListBlobItem>> ListBlobsAsync(CloudBlobContainer blobContainer, string prefix, bool useFlatBlobListing, BlobListingDetails blobListingDetails = BlobListingDetails.None) { BlobContinuationToken continuationToken = null; List<IListBlobItem> results = new List<IListBlobItem>(); do { var response = await blobContainer .ListBlobsSegmentedAsync(prefix, useFlatBlobListing, blobListingDetails, null, continuationToken, null, null) .ConfigureAwait(false); continuationToken = response.ContinuationToken; results.AddRange(response.Results); } while (continuationToken != null); return results; }
/// <summary> /// Lists blobs in the specified container using a flat listing, with an optional segment size specified, and writes /// their properties and metadata to the console window. /// The flat listing returns a segment containing all of the blobs matching the listing criteria. /// In a flat listing, blobs are not organized by virtual directory. /// </summary> /// <param name="container">A CloudBlobContainer object.</param> /// <param name="segmentSize">The size of the segment to return in each call to the listing operation.</param> /// <returns>A Task object.</returns> private static async Task ListBlobsFlatListingAsync(CloudBlobContainer container, int? segmentSize) { // List blobs to the console window. Console.WriteLine("List blobs in segments (flat listing):"); Console.WriteLine(); int i = 0; BlobContinuationToken continuationToken = null; BlobResultSegment resultSegment = null; try { // Call ListBlobsSegmentedAsync and enumerate the result segment returned, while the continuation token is non-null. // When the continuation token is null, the last segment has been returned and execution can exit the loop. do { // This overload allows control of the segment size. You can return all remaining results by passing null for the maxResults parameter, // or by calling a different overload. // Note that requesting the blob's metadata as part of the listing operation // populates the metadata, so it's not necessary to call FetchAttributes() to read the metadata. resultSegment = await container.ListBlobsSegmentedAsync(string.Empty, true, BlobListingDetails.Metadata, segmentSize, continuationToken, null, null); if (resultSegment.Results.Count() > 0) { Console.WriteLine("Page {0}:", ++i); } foreach (var blobItem in resultSegment.Results) { Console.WriteLine("************************************"); Console.WriteLine(blobItem.Uri); // A flat listing operation returns only blobs, not virtual directories. // Write out blob properties and metadata. if (blobItem is CloudBlob) { PrintBlobPropertiesAndMetadata((CloudBlob)blobItem); } } Console.WriteLine(); // Get the continuation token. continuationToken = resultSegment.ContinuationToken; } while (continuationToken != null); } catch (StorageException e) { Console.WriteLine(e.Message); Console.ReadLine(); throw; } }
public async Task CloudPageBlobSnapshotAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudPageBlob blob = container.GetPageBlobReference("blob1"); await blob.UploadFromStreamAsync(originalData.AsInputStream()); CloudPageBlob snapshot1 = await blob.CreateSnapshotAsync(); Assert.AreEqual(blob.Properties.ETag, snapshot1.Properties.ETag); Assert.AreEqual(blob.Properties.LastModified, snapshot1.Properties.LastModified); Assert.IsNotNull(snapshot1.SnapshotTime, "Snapshot does not have SnapshotTime set"); CloudPageBlob snapshot2 = await blob.CreateSnapshotAsync(); Assert.IsTrue(snapshot2.SnapshotTime.Value > snapshot1.SnapshotTime.Value); await snapshot1.FetchAttributesAsync(); await snapshot2.FetchAttributesAsync(); await blob.FetchAttributesAsync(); AssertAreEqual(snapshot1.Properties, blob.Properties); CloudPageBlob snapshot1Clone = new CloudPageBlob(new Uri(blob.Uri + "?snapshot=" + snapshot1.SnapshotTime.Value.ToString("O")), blob.ServiceClient.Credentials); Assert.IsNotNull(snapshot1Clone.SnapshotTime, "Snapshot clone does not have SnapshotTime set"); Assert.AreEqual(snapshot1.SnapshotTime.Value, snapshot1Clone.SnapshotTime.Value); await snapshot1Clone.FetchAttributesAsync(); AssertAreEqual(snapshot1.Properties, snapshot1Clone.Properties); CloudPageBlob snapshotCopy = container.GetPageBlobReference("blob2"); await snapshotCopy.StartCopyFromBlobAsync(TestHelper.Defiddler(snapshot1.Uri)); await WaitForCopyAsync(snapshotCopy); Assert.AreEqual(CopyStatus.Success, snapshotCopy.CopyState.Status); await TestHelper.ExpectedExceptionAsync <InvalidOperationException>( async() => await snapshot1.OpenWriteAsync(1024), "Trying to write to a blob snapshot should fail"); using (Stream snapshotStream = (await snapshot1.OpenReadAsync()).AsStreamForRead()) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } await blob.CreateAsync(1024); using (Stream snapshotStream = (await snapshot1.OpenReadAsync()).AsStreamForRead()) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, null, null, null); List <IListBlobItem> blobs = resultSegment.Results.ToList(); Assert.AreEqual(4, blobs.Count); AssertAreEqual(snapshot1, (ICloudBlob)blobs[0]); AssertAreEqual(snapshot2, (ICloudBlob)blobs[1]); AssertAreEqual(blob, (ICloudBlob)blobs[2]); AssertAreEqual(snapshotCopy, (ICloudBlob)blobs[3]); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
/// <summary> /// List the blobs segmented in specified containers /// </summary> /// <param name="container">A cloudblobcontainer object</param> /// <param name="prefix">Blob prefix</param> /// <param name="useFlatBlobListing">Use flat blob listing(whether treat "container/" as directory)</param> /// <param name="blobListingDetails">Blob listing details</param> /// <param name="options">Blob request option</param> /// <param name="operationContext">Operation context</param> public Task<BlobResultSegment> ListBlobsSegmentedAsync(CloudBlobContainer container, string prefix, bool useFlatBlobListing, BlobListingDetails blobListingDetails, int? maxResults, BlobContinuationToken currentToken, BlobRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken) { return container.ListBlobsSegmentedAsync(prefix, useFlatBlobListing, blobListingDetails, maxResults, currentToken, options, operationContext, cancellationToken); }
public async Task<List<IListBlobItem>> ListBlobsAsync(CloudBlobContainer container) { BlobContinuationToken continuationToken = null; var results = new List<IListBlobItem>(); do { var response = await container.ListBlobsSegmentedAsync(continuationToken); continuationToken = response.ContinuationToken; results.AddRange(response.Results); } while (continuationToken != null); return results; }