コード例 #1
0
        public async Task CloudBlobContainerWebContainerOperationsAsync()
        {
            // Test operations with shard key
            CloudBlobClient    blobClient   = GenerateCloudBlobClient();
            CloudBlobContainer webContainer = blobClient.GetContainerReference("$web");

            try
            {
                await webContainer.DeleteIfExistsAsync();

                Assert.IsFalse(await webContainer.ExistsAsync());
                await TestHelper.SpinUpToNSecondsIgnoringFailuresAsync(async() => await webContainer.CreateAsync(), 120);

                Assert.IsTrue(await webContainer.ExistsAsync());
                Assert.IsTrue(
                    (await blobClient.ListContainersSegmentedAsync("$", null))
                    .Results
                    .Any(container => container.Name == webContainer.Name)
                    );

                await ValidateWebContainerAsync(webContainer);

                // Clear out the old data, faster than deleting / re-creating the container.
                foreach (CloudBlob blob in (await webContainer.ListBlobsSegmentedAsync(string.Empty, true, BlobListingDetails.All, default(int?), default(BlobContinuationToken), default(BlobRequestOptions), default(OperationContext))).Results)
                {
                    await blob.DeleteAsync();
                }

                // Test relevant operations with a service SAS.
                string webContainerSAS = webContainer.GetSharedAccessSignature(new SharedAccessBlobPolicy()
                {
                    SharedAccessExpiryTime = DateTime.Now + TimeSpan.FromDays(30), Permissions = SharedAccessBlobPermissions.Create | SharedAccessBlobPermissions.Delete | SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.List | SharedAccessBlobPermissions.Write
                });
                await ValidateWebContainerAsync(new CloudBlobContainer(new Uri(webContainer.Uri + webContainerSAS)));

                await webContainer.DeleteAsync();

                Assert.IsFalse(
                    (await blobClient.ListContainersSegmentedAsync("$", null))
                    .Results
                    .Any(container => container.Name == webContainer.Name)
                    );
            }
            finally
            {
                webContainer.DeleteIfExistsAsync().Wait();
            }
        }
コード例 #2
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);
        }
コード例 #3
0
        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();
            }
        }
コード例 #4
0
        private async Task<IEnumerable<CloudBlobContainer>> ListContainers(CloudBlobClient blobClient, string prefix)
        {
            BlobContinuationToken continuationToken = null;
            var containers = new List<CloudBlobContainer>();

            do {
                var response = await blobClient
                    .ListContainersSegmentedAsync(prefix, continuationToken)
                    .ConfigureAwait(false);

                containers.AddRange(response.Results);
                continuationToken = response.ContinuationToken;
            }
            while (continuationToken != null);

            return containers;
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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();
            }
        }
コード例 #7
0
        /// <summary>
        /// Lists containers in the storage account whose names begin with the specified prefix, and return container metadata
        /// as part of the listing operation.
        /// </summary>
        /// <param name="blobClient">The Blob service client.</param>
        /// <param name="prefix">The container name prefix.</param>
        /// <returns>A Task object.</returns>
        private static async Task ListContainersWithPrefixAsync(CloudBlobClient blobClient, string prefix)
        {
            Console.WriteLine("List all containers beginning with prefix {0}, plus container metadata:", prefix);

            BlobContinuationToken continuationToken = null;
            ContainerResultSegment resultSegment = null;

            try
            {
                do
                {
                    // List containers beginning with the specified prefix, returning segments of 5 results each. 
                    // Note that passing in null for the maxResults parameter returns the maximum number of results (up to 5000).
                    // Requesting the container'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 blobClient.ListContainersSegmentedAsync(
                        prefix, ContainerListingDetails.Metadata, 5, continuationToken, null, null);

                    // Enumerate the containers returned.
                    foreach (var container in resultSegment.Results)
                    {
                        Console.WriteLine("\tContainer:" + container.Name);

                        // Write the container's metadata keys and values.
                        foreach (var metadataItem in container.Metadata)
                        {
                            Console.WriteLine("\t\tMetadata key: " + metadataItem.Key);
                            Console.WriteLine("\t\tMetadata value: " + metadataItem.Value);
                        }
                    }

                    // Get the continuation token.
                    continuationToken = resultSegment.ContinuationToken;

                } while (continuationToken != null);

                Console.WriteLine();
            }
            catch (StorageException e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
                throw;
            }
        }