예제 #1
0
        public async Task <int> GetFullBlobsAsync()
        {
            var    cloudBlobDic      = new List <Microsoft.Azure.Storage.Blob.CloudBlobDirectory>();
            string storageConnection = GetEnvironmentVariable("AzureWebJobsStorage");
            var    count             = 0;

            if (CloudStorageAccount.TryParse(storageConnection, out CloudStorageAccount cloudStorageAccount))
            {
                Microsoft.Azure.Storage.Blob.BlobContinuationToken dirToken = null;

                var cloudBlobClient    = cloudStorageAccount.CreateCloudBlobClient();
                var cloudBlobContainer = cloudBlobClient.GetContainerReference(_blobContainerName);
                var blobList           = await cloudBlobContainer.ListBlobsSegmentedAsync(string.Empty, false, Microsoft.Azure.Storage.Blob.BlobListingDetails.None, int.MaxValue, null, null, null);


                //var dirResult = await cloudBlobContainer.ListBlobsSegmentedAsync(dirToken);
                //dirToken = dirResult.ContinuationToken;



                foreach (var dirItem in blobList.Results)
                {
                    if (dirItem is Microsoft.Azure.Storage.Blob.CloudBlobDirectory)
                    {
                        var dir = dirItem as Microsoft.Azure.Storage.Blob.CloudBlobDirectory;
                        Microsoft.Azure.Storage.Blob.BlobContinuationToken blobToken = null;
                        var blobResult = await dir.ListBlobsSegmentedAsync(blobToken);

                        foreach (var blobItem in blobResult.Results)
                        {
                            if (blobItem is Microsoft.Azure.Storage.Blob.CloudBlockBlob)
                            {
                                var      blob       = blobItem as Microsoft.Azure.Storage.Blob.CloudBlockBlob;
                                var      filename   = blob.Name;
                                string[] multiArray = blob.Name.Split(new Char[] { '/' });
                                var      content    = await blob.DownloadTextAsync();

                                count++;
                                using (var writer = new StreamWriter(string.Format("{0:D5}.xml", count)))
                                {
                                    // await writer.WriteAsync(content);
                                    await UploadFile(content, multiArray[1], multiArray[0]);
                                }
                                Console.WriteLine(count);
                                //if (count >= maxEvents)
                                //    break;
                            }
                        }
                    }
                }
            }
            // return (List<Microsoft.Azure.Storage.Blob.CloudBlobDirectory>)blobList.Results;
            return(count);
        }
예제 #2
0
        public async Task CloudBlobSoftDeleteSnapshotTask()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                //Enables a delete retention policy on the blob with 1 day of default retention days
                container.ServiceClient.EnableSoftDelete();
                await container.CreateAsync();

                // Upload some data to the blob.
                var             blobName     = GetRandomBlobName();
                MemoryStream    originalData = new MemoryStream(GetRandomBuffer(1024));
                CloudAppendBlob appendBlob   = container.GetAppendBlobReference(blobName);
                await appendBlob.UploadFromStreamAsync(originalData);

                CloudBlob blob = container.GetBlobReference(blobName);

                //create snapshot via api
                CloudBlob snapshot = blob.Snapshot();
                //create snapshot via write protection
                await appendBlob.UploadFromStreamAsync(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
                {
                    Task <BlobResultSegment> resultSegments = container.ListBlobsSegmentedAsync
                                                                  (null, true, BlobListingDetails.Snapshots | BlobListingDetails.Deleted, null, ct, null, null);
                    List <IListBlobItem> blobs = resultSegments.Result
                                                 .Results
                                                 .ToList();
                    ct = resultSegments.Result.ContinuationToken;

                    foreach (IListBlobItem item in blobs)
                    {
                        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(3, blobCount);
                Assert.AreEqual(deletedSnapshotCount, 1);
                Assert.AreEqual(snapShotCount, 2);

                blobCount = 0;
                ct        = null;
                do
                {
                    foreach (IListBlobItem item in (await container.ListBlobsSegmentedAsync
                                                        (null, true, BlobListingDetails.Snapshots, null, ct, null, null))
                             .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 (await container.ListBlobsSegmentedAsync
                                                        (null, true, BlobListingDetails.All, null, ct, null, null))
                             .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 (await container.ListBlobsSegmentedAsync
                                                        (null, true, BlobListingDetails.All, null, ct, null, null))
                             .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();
                await container.DeleteAsync();
            }
        }
예제 #3
0
        public async Task CloudBlobDirectoryFlatListingTask()
        {
            foreach (String delimiter in Delimiters)
            {
                CloudBlobClient client = GenerateCloudBlobClient();
                client.DefaultDelimiter = delimiter;
                string             name      = GetRandomContainerName();
                CloudBlobContainer container = client.GetContainerReference(name);

                try
                {
                    await container.CreateAsync();

                    if (CloudBlobDirectorySetupWithDelimiter(container, delimiter))
                    {
                        BlobContinuationToken token     = null;
                        CloudBlobDirectory    directory = container.GetDirectoryReference("TopDir1");
                        List <IListBlobItem>  list1     = new List <IListBlobItem>();
                        do
                        {
                            BlobResultSegment result1 = await directory.ListBlobsSegmentedAsync(token);

                            list1.AddRange(result1.Results);
                            token = result1.ContinuationToken;
                        }while (token != null);

                        Assert.IsTrue(list1.Count == 3);

                        IListBlobItem item11 = list1.ElementAt(0);
                        Assert.IsTrue(item11.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "Blob1"));

                        IListBlobItem item12 = list1.ElementAt(1);
                        Assert.IsTrue(item12.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter));

                        IListBlobItem item13 = list1.ElementAt(2);
                        Assert.IsTrue(item13.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter));

                        CloudBlobDirectory midDir2 = (CloudBlobDirectory)item13;

                        List <IListBlobItem> list2 = new List <IListBlobItem>();
                        do
                        {
                            BlobResultSegment result2
                                = await midDir2.ListBlobsSegmentedAsync(true, BlobListingDetails.None, null, token, null, null);

                            list2.AddRange(result2.Results);
                            token = result2.ContinuationToken;
                        }while (token != null);

                        Assert.IsTrue(list2.Count == 2);

                        IListBlobItem item41 = list2.ElementAt(0);
                        Assert.IsTrue(item41.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir1" + delimiter + "EndBlob1"));

                        IListBlobItem item42 = list2.ElementAt(1);
                        Assert.IsTrue(item42.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir2" + delimiter + "EndBlob2"));
                    }
                }
                finally
                {
                    await container.DeleteAsync();
                }
            }
        }
예제 #4
0
        public async Task CloudBlobSoftDeleteNoSnapshotTask()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                //Enables a delete retention policy on the blob with 1 day of default retention days
                container.ServiceClient.EnableSoftDelete();
                await container.CreateAsync();

                // Upload some data to the blob.
                var             blobName     = GetRandomBlobName();
                MemoryStream    originalData = new MemoryStream(GetRandomBuffer(1024));
                CloudAppendBlob appendBlob   = container.GetAppendBlobReference(blobName);
                await appendBlob.CreateOrReplaceAsync();

                await appendBlob.AppendBlockAsync(originalData, null);


                CloudBlob blob = container.GetBlobReference(blobName);
                Assert.IsTrue(await blob.ExistsAsync());
                Assert.IsFalse(blob.IsDeleted);

                await blob.DeleteAsync();

                Assert.IsFalse(await blob.ExistsAsync());

                int blobCount            = 0;
                BlobContinuationToken ct = null;
                do
                {
                    foreach (IListBlobItem item in (await container.ListBlobsSegmentedAsync
                                                        (null, true, BlobListingDetails.All, null, ct, null, null))
                             .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);

                await blob.UndeleteAsync();

                await blob.FetchAttributesAsync();

                Assert.IsFalse(blob.IsDeleted);
                Assert.IsNull(blob.Properties.DeletedTime);
                Assert.IsNull(blob.Properties.RemainingDaysBeforePermanentDelete);

                blobCount = 0;
                ct        = null;
                do
                {
                    foreach (IListBlobItem item in (await container.ListBlobsSegmentedAsync
                                                        (null, true, BlobListingDetails.All, null, ct, null, null))
                             .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();
                await container.DeleteAsync();
            }
        }
 /// <summary>
 /// Creates a new instance of ContainerResultSegment.
 /// </summary>
 /// <param name="containers">An enumerable collection of <see cref="CloudBlobContainer"/> objects.</param>
 /// <param name="continuationToken">The <see cref="BlobContinuationToken"/> used to retrieve the next segment of <see cref="CloudBlobContainer"/> results.</param>
 public ContainerResultSegment(IEnumerable <CloudBlobContainer> containers, BlobContinuationToken continuationToken)
 {
     this.Results           = containers;
     this.ContinuationToken = continuationToken;
 }
예제 #6
0
        public void CloudBlobDirectoryFlatListingAPM()
        {
            foreach (String delimiter in Delimiters)
            {
                CloudBlobClient client = GenerateCloudBlobClient();
                client.DefaultDelimiter = delimiter;
                string             name      = GetRandomContainerName();
                CloudBlobContainer container = client.GetContainerReference(name);

                try
                {
                    container.Create();
                    if (CloudBlobDirectorySetupWithDelimiter(container, delimiter))
                    {
                        using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                        {
                            IAsyncResult          result;
                            BlobContinuationToken token     = null;
                            CloudBlobDirectory    directory = container.GetDirectoryReference("TopDir1");
                            List <IListBlobItem>  list1     = new List <IListBlobItem>();
                            do
                            {
                                result = directory.BeginListBlobsSegmented(token, ar => waitHandle.Set(), null);
                                waitHandle.WaitOne();
                                BlobResultSegment result1 = directory.EndListBlobsSegmented(result);
                                list1.AddRange(result1.Results);
                                token = result1.ContinuationToken;
                            }while (token != null);

                            Assert.IsTrue(list1.Count == 3);

                            IListBlobItem item11 = list1.ElementAt(0);
                            Assert.IsTrue(item11.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "Blob1"));

                            IListBlobItem item12 = list1.ElementAt(1);
                            Assert.IsTrue(item12.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter));

                            IListBlobItem item13 = list1.ElementAt(2);
                            Assert.IsTrue(item13.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter));

                            CloudBlobDirectory midDir2 = (CloudBlobDirectory)item13;

                            List <IListBlobItem> list2 = new List <IListBlobItem>();
                            do
                            {
                                result = midDir2.BeginListBlobsSegmented(true, BlobListingDetails.None, null, token, null, null, ar => waitHandle.Set(), null);
                                waitHandle.WaitOne();
                                BlobResultSegment result2 = midDir2.EndListBlobsSegmented(result);
                                list2.AddRange(result2.Results);
                                token = result2.ContinuationToken;
                            }while (token != null);

                            Assert.IsTrue(list2.Count == 2);

                            IListBlobItem item41 = list2.ElementAt(0);
                            Assert.IsTrue(item41.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir1" + delimiter + "EndBlob1"));

                            IListBlobItem item42 = list2.ElementAt(1);
                            Assert.IsTrue(item42.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir2" + delimiter + "EndBlob2"));
                        }
                    }
                }
                finally
                {
                    container.DeleteIfExists();
                }
            }
        }
예제 #7
0
 public virtual ICancellableAsyncResult BeginListBlobsSegmented(BlobContinuationToken currentToken, AsyncCallback callback, object state)
 {
     return(this.BeginListBlobsSegmented(false, BlobListingDetails.None, null /* maxResults */, currentToken, null /* options */, null /* operationContext */, callback, state));
 }
 /// <summary>
 /// Creates a new instance of a BlobResultSegment.
 /// </summary>
 /// <param name="blobs">An enumerable collection of <see cref="IListBlobItem"/> objects.</param>
 /// <param name="continuationToken">The <see cref="BlobContinuationToken"/> used to retrieve the next segment of <see cref="IListBlobItem"/> results.</param>
 public BlobResultSegment(IEnumerable <IListBlobItem> blobs, BlobContinuationToken continuationToken)
 {
     this.Results           = blobs;
     this.ContinuationToken = continuationToken;
 }
예제 #9
0
 public virtual BlobResultSegment ListBlobsSegmented(bool useFlatBlobListing, BlobListingDetails blobListingDetails, int?maxResults, BlobContinuationToken currentToken, BlobRequestOptions options, OperationContext operationContext)
 {
     return(this.Container.ListBlobsSegmented(this.Prefix, useFlatBlobListing, blobListingDetails, maxResults, currentToken, options, operationContext));
 }
예제 #10
0
 public virtual BlobResultSegment ListBlobsSegmented(BlobContinuationToken currentToken)
 {
     return(this.ListBlobsSegmented(false, BlobListingDetails.None, null /* maxResults */, currentToken, null /* options */, null /* operationContext */));
 }
예제 #11
0
 public virtual Task <BlobResultSegment> ListBlobsSegmentedAsync(bool useFlatBlobListing, BlobListingDetails blobListingDetails, int?maxResults, BlobContinuationToken currentToken, BlobRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     return(this.Container.ListBlobsSegmentedAsync(this.Prefix, useFlatBlobListing, blobListingDetails, maxResults, currentToken, options, operationContext, cancellationToken));
 }
예제 #12
0
 public virtual Task <BlobResultSegment> ListBlobsSegmentedAsync(BlobContinuationToken currentToken, CancellationToken cancellationToken)
 {
     return(this.Container.ListBlobsSegmentedAsync(this.Prefix, false /*useFlatBlobListDetails*/, BlobListingDetails.None, null /*maxResults*/, currentToken, default(BlobRequestOptions), default(OperationContext), cancellationToken));
 }
예제 #13
0
 public virtual ICancellableAsyncResult BeginListBlobsSegmented(bool useFlatBlobListing, BlobListingDetails blobListingDetails, int?maxResults, BlobContinuationToken currentToken, BlobRequestOptions options, OperationContext operationContext, AsyncCallback callback, object state)
 {
     return(CancellableAsyncResultTaskWrapper.Create(token => this.ListBlobsSegmentedAsync(useFlatBlobListing, blobListingDetails, maxResults, currentToken, options, operationContext, token), callback, state));
 }