Example #1
0
        public async Task CloudBlobClientListContainersSegmentedAsync()
        {
            string          name           = GetRandomContainerName();
            List <string>   containerNames = new List <string>();
            CloudBlobClient blobClient     = GenerateCloudBlobClient();

            for (int i = 0; i < 3; i++)
            {
                string containerName = name + i.ToString();
                containerNames.Add(containerName);
                await blobClient.GetContainerReference(containerName).CreateAsync();
            }

            BlobContinuationToken token = null;

            do
            {
                ContainerResultSegment results = await blobClient.ListContainersSegmentedAsync(token);

                token = results.ContinuationToken;

                foreach (CloudBlobContainer container in results.Results)
                {
                    if (containerNames.Remove(container.Name))
                    {
                        await container.DeleteAsync();
                    }
                }
            }while (token != null);
            Assert.AreEqual <int>(0, containerNames.Count);
        }
        public async Task CloudBlobContainerSetMetadataAsync()
        {
            CloudBlobContainer container = GetRandomContainerReference();
            try
            {
                await container.CreateAsync();

                CloudBlobContainer container2 = container.ServiceClient.GetContainerReference(container.Name);
                await container2.FetchAttributesAsync();
                Assert.AreEqual(0, container2.Metadata.Count);

                container.Metadata.Add("key1", "value1");
                await container.SetMetadataAsync();

                await container2.FetchAttributesAsync();
                Assert.AreEqual(1, container2.Metadata.Count);
                Assert.AreEqual("value1", container2.Metadata["key1"]);

                ContainerResultSegment results = await container.ServiceClient.ListContainersSegmentedAsync(container.Name, ContainerListingDetails.Metadata, null, null, null, null);
                CloudBlobContainer container3 = results.Results.First();
                Assert.AreEqual(1, container3.Metadata.Count);
                Assert.AreEqual("value1", container3.Metadata["key1"]);

                container.Metadata.Clear();
                await container.SetMetadataAsync();

                await container2.FetchAttributesAsync();
                Assert.AreEqual(0, container2.Metadata.Count);
            }
            finally
            {
                container.DeleteIfExistsAsync().Wait();
            }
        }
        public async Task CloudBlobClientListContainersWithPublicAccessAsync()
        {
            string             name       = GetRandomContainerName();
            CloudBlobClient    blobClient = GenerateCloudBlobClient();
            CloudBlobContainer container  = blobClient.GetContainerReference(name);

            try
            {
                await container.CreateAsync();

                BlobContainerPublicAccessType[] accessValues = { BlobContainerPublicAccessType.Container, BlobContainerPublicAccessType.Off, BlobContainerPublicAccessType.Blob };
                BlobContainerPermissions        permissions  = new BlobContainerPermissions();
                foreach (BlobContainerPublicAccessType access in accessValues)
                {
                    permissions.PublicAccess = access;
                    await container.SetPermissionsAsync(permissions);

                    Assert.AreEqual(access, container.Properties.PublicAccess);

                    CloudBlobContainer container2 = blobClient.GetContainerReference(name);
                    Assert.IsFalse(container2.Properties.PublicAccess.HasValue);
                    await container2.FetchAttributesAsync();

                    Assert.AreEqual(access, container2.Properties.PublicAccess);

                    CloudBlobContainer       container3      = blobClient.GetContainerReference(name);
                    BlobContainerPermissions containerAccess = await container3.GetPermissionsAsync();

                    Assert.AreEqual(access, containerAccess.PublicAccess);
                    Assert.AreEqual(access, container3.Properties.PublicAccess);

                    List <CloudBlobContainer> listedContainers = new List <CloudBlobContainer>();
                    BlobContinuationToken     token            = null;
                    do
                    {
                        ContainerResultSegment resultSegment = await blobClient.ListContainersSegmentedAsync(name, token);

                        foreach (CloudBlobContainer returnedContainer in resultSegment.Results)
                        {
                            listedContainers.Add(returnedContainer);
                        }
                        token = resultSegment.ContinuationToken;
                    }while (token != null);

                    Assert.AreEqual(1, listedContainers.Count());
                    Assert.AreEqual(access, listedContainers.First().Properties.PublicAccess);
                }
            }
            finally
            {
                container.DeleteAsync().GetAwaiter().GetResult();
            }
        }
        public async Task CloudBlobClientListContainersSegmentedAsync()
        {
            AssertSecondaryEndpoint();

            string          name           = GetRandomContainerName();
            List <string>   containerNames = new List <string>();
            CloudBlobClient blobClient     = GenerateCloudBlobClient();

            for (int i = 0; i < 3; i++)
            {
                string containerName = name + i.ToString();
                containerNames.Add(containerName);
                await blobClient.GetContainerReference(containerName).CreateAsync();
            }

            List <string>         listedContainerNames = new List <string>();
            BlobContinuationToken token = null;

            do
            {
                ContainerResultSegment resultSegment = await blobClient.ListContainersSegmentedAsync(token);

                token = resultSegment.ContinuationToken;

                foreach (CloudBlobContainer container in resultSegment.Results)
                {
                    Assert.IsTrue(blobClient.GetContainerReference(container.Name).StorageUri.Equals(container.StorageUri));
                    listedContainerNames.Add(container.Name);
                }
            }while (token != null);

            foreach (string containerName in listedContainerNames)
            {
                if (containerNames.Remove(containerName))
                {
                    await blobClient.GetContainerReference(containerName).DeleteAsync();
                }
            }

            Assert.AreEqual(0, containerNames.Count);
        }
        public async Task CloudBlobClientListContainersSegmentedWithPrefixAsync()
        {
            string          name           = GetRandomContainerName();
            List <string>   containerNames = new List <string>();
            CloudBlobClient blobClient     = GenerateCloudBlobClient();

            for (int i = 0; i < 3; i++)
            {
                string containerName = name + i.ToString();
                containerNames.Add(containerName);
                await blobClient.GetContainerReference(containerName).CreateAsync();
            }

            List <string>         listedContainerNames = new List <string>();
            BlobContinuationToken token = null;

            do
            {
                ContainerResultSegment resultSegment = await blobClient.ListContainersSegmentedAsync(name, ContainerListingDetails.None, 1, token, null, null);

                token = resultSegment.ContinuationToken;

                int count = 0;
                foreach (CloudBlobContainer container in resultSegment.Results)
                {
                    count++;
                    listedContainerNames.Add(container.Name);
                }
                Assert.IsTrue(count <= 1);
            }while (token != null);

            Assert.AreEqual(containerNames.Count, listedContainerNames.Count);
            foreach (string containerName in listedContainerNames)
            {
                Assert.IsTrue(containerNames.Remove(containerName));
                await blobClient.GetContainerReference(containerName).DeleteAsync();
            }
        }