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(); } }
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(); } }