Exemple #1
0
        public async Task CloudBlobContainerConditionalAccessAsync()
        {
            OperationContext   operationContext = new OperationContext();
            CloudBlobContainer container        = GetRandomContainerReference();

            try
            {
                await container.CreateAsync();

                await container.FetchAttributesAsync();

                string         currentETag         = container.Properties.ETag;
                DateTimeOffset currentModifiedTime = container.Properties.LastModified.Value;

                // ETag conditional tests
                container.Metadata["ETagConditionalName"] = "ETagConditionalValue";
                await container.SetMetadataAsync();

                await container.FetchAttributesAsync();

                string newETag = container.Properties.ETag;
                Assert.AreNotEqual(newETag, currentETag, "ETage should be modified on write metadata");

                // LastModifiedTime tests
                currentModifiedTime = container.Properties.LastModified.Value;

                container.Metadata["DateConditionalName"] = "DateConditionalValue";

                await TestHelper.ExpectedExceptionAsync(
                    async() => await container.SetMetadataAsync(AccessCondition.GenerateIfModifiedSinceCondition(currentModifiedTime), null, operationContext),
                    operationContext,
                    "IfModifiedSince conditional on current modified time should throw",
                    HttpStatusCode.PreconditionFailed,
                    "ConditionNotMet");

                container.Metadata["DateConditionalName"] = "DateConditionalValue2";
                currentETag = container.Properties.ETag;

                DateTimeOffset pastTime = currentModifiedTime.Subtract(TimeSpan.FromMinutes(5));
                await container.SetMetadataAsync(AccessCondition.GenerateIfModifiedSinceCondition(pastTime), null, null);

                pastTime = currentModifiedTime.Subtract(TimeSpan.FromHours(5));
                await container.SetMetadataAsync(AccessCondition.GenerateIfModifiedSinceCondition(pastTime), null, null);

                pastTime = currentModifiedTime.Subtract(TimeSpan.FromDays(5));
                await container.SetMetadataAsync(AccessCondition.GenerateIfModifiedSinceCondition(pastTime), null, null);

                await container.FetchAttributesAsync();

                newETag = container.Properties.ETag;
                Assert.AreNotEqual(newETag, currentETag, "ETage should be modified on write metadata");
            }
            finally
            {
                container.DeleteIfExistsAsync().Wait();
            }
        }
Exemple #2
0
        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);
            }
        }
        public async Task CloudBlobCreateContainerEncryptionScopeTask()
        {
            // Arrange
            CloudBlobContainer container = GetRandomContainerReference();

            string defaultEncryptionScope = TestBase.TargetTenantConfig.EncryptionScope;

            BlobContainerEncryptionScopeOptions encryptionScopeOptions = new BlobContainerEncryptionScopeOptions
            {
                DefaultEncryptionScope         = defaultEncryptionScope,
                PreventEncryptionScopeOverride = false
            };

            try
            {
                var context = new OperationContext();
                await container.CreateAsync(BlobContainerPublicAccessType.Off, encryptionScopeOptions, null, context, CancellationToken.None);

                await container.FetchAttributesAsync();

                Assert.AreEqual(defaultEncryptionScope, container.Properties.EncryptionScopeOptions.DefaultEncryptionScope);
                Assert.IsFalse(container.Properties.EncryptionScopeOptions.PreventEncryptionScopeOverride);
            }
            finally
            {
                await container.DeleteIfExistsAsync();
            }
        }
        public async Task CloudBlobContainerCreateWithMetadataAsync()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Metadata.Add("key1", "value1");
                await container.CreateAsync();

                CloudBlobContainer container2 = container.ServiceClient.GetContainerReference(container.Name);
                await container2.FetchAttributesAsync();

                Assert.AreEqual(1, container2.Metadata.Count);
                Assert.AreEqual("value1", container2.Metadata["key1"]);
                // Metadata keys should be case-insensitive
                Assert.AreEqual("value1", container2.Metadata["KEY1"]);

                Assert.IsTrue(container2.Properties.LastModified.Value.AddHours(1) > DateTimeOffset.Now);
                Assert.IsNotNull(container2.Properties.ETag);
            }
            finally
            {
                await container.DeleteAsync();
            }
        }
        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"]);
                // Metadata keys should be case-insensitive
                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"]);
                Assert.AreEqual("value1", container3.Metadata["KEY1"]);

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

                await container2.FetchAttributesAsync();

                Assert.AreEqual(0, container2.Metadata.Count);
            }
            finally
            {
                await container.DeleteAsync();
            }
        }
Exemple #6
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();
            }
        }