Ejemplo n.º 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();
            }
        }
Ejemplo n.º 2
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();
            }
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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();
            }
        }