public async Task CloudPageBlobSetMetadataAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); await blob.CreateAsync(1024); CloudPageBlob blob2 = container.GetPageBlobReference("blob1"); await blob2.FetchAttributesAsync(); Assert.AreEqual(0, blob2.Metadata.Count); OperationContext operationContext = new OperationContext(); blob.Metadata["key1"] = null; Assert.ThrowsException <AggregateException>( () => blob.SetMetadataAsync(null, null, operationContext).AsTask().Wait(), "Metadata keys should have a non-null value"); Assert.IsInstanceOfType(operationContext.LastResult.Exception.InnerException, typeof(ArgumentException)); blob.Metadata["key1"] = ""; Assert.ThrowsException <AggregateException>( () => blob.SetMetadataAsync(null, null, operationContext).AsTask().Wait(), "Metadata keys should have a non-empty value"); Assert.IsInstanceOfType(operationContext.LastResult.Exception.InnerException, typeof(ArgumentException)); blob.Metadata["key1"] = "value1"; await blob.SetMetadataAsync(); await blob2.FetchAttributesAsync(); Assert.AreEqual(1, blob2.Metadata.Count); Assert.AreEqual("value1", blob2.Metadata["key1"]); BlobResultSegment results = await container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.Metadata, null, null, null, null); CloudPageBlob blob3 = (CloudPageBlob)results.Results.First(); Assert.AreEqual(1, blob3.Metadata.Count); Assert.AreEqual("value1", blob3.Metadata["key1"]); blob.Metadata.Clear(); await blob.SetMetadataAsync(); await blob2.FetchAttributesAsync(); Assert.AreEqual(0, blob2.Metadata.Count); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
public void PageBlobReadLockToETagTestTask() { byte[] outBuffer = new byte[1 * 1024 * 1024]; byte[] buffer = GetRandomBuffer(2 * outBuffer.Length); CloudBlobContainer container = GetRandomContainerReference(); try { container.CreateAsync().Wait(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); blob.StreamMinimumReadSizeInBytes = outBuffer.Length; using (MemoryStream wholeBlob = new MemoryStream(buffer)) { blob.UploadFromStreamAsync(wholeBlob).Wait(); } using (Stream blobStream = blob.OpenReadAsync().Result) { blobStream.Read(outBuffer, 0, outBuffer.Length); blob.SetMetadataAsync().Wait(); TestHelper.ExpectedException( () => blobStream.Read(outBuffer, 0, outBuffer.Length), "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } using (Stream blobStream = blob.OpenReadAsync().Result) { long length = blobStream.Length; blob.SetMetadataAsync().Wait(); TestHelper.ExpectedException( () => blobStream.Read(outBuffer, 0, outBuffer.Length), "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } AccessCondition accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(DateTimeOffset.Now.Subtract(TimeSpan.FromHours(1))); blob.SetMetadataAsync().Wait(); TestHelper.ExpectedExceptionTask( blob.OpenReadAsync(accessCondition, null, null), "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } finally { container.DeleteIfExistsAsync().Wait(); } }
public void CloudPageBlobCopyTestTask() { CloudBlobContainer container = GetRandomContainerReference(); try { container.CreateAsync().Wait(); CloudPageBlob source = container.GetPageBlobReference("source"); string data = new string('a', 512); UploadTextTask(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; source.SetMetadataAsync().Wait(); CloudPageBlob copy = container.GetPageBlobReference("copy"); string copyId = copy.StartCopyAsync(TestHelper.Defiddler(source)).Result; Assert.AreEqual(BlobType.PageBlob, copy.BlobType); WaitForCopyTask(copy); Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status); Assert.AreEqual(source.Uri.AbsolutePath, copy.CopyState.Source.AbsolutePath); Assert.AreEqual(data.Length, copy.CopyState.TotalBytes); Assert.AreEqual(data.Length, copy.CopyState.BytesCopied); Assert.AreEqual(copyId, copy.CopyState.CopyId); Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); TestHelper.ExpectedExceptionTask( copy.AbortCopyAsync(copyId), "Aborting a copy operation after completion should fail", HttpStatusCode.Conflict, "NoPendingCopyOperation"); source.FetchAttributesAsync().Wait(); Assert.IsNotNull(copy.Properties.ETag); Assert.AreNotEqual(source.Properties.ETag, copy.Properties.ETag); Assert.IsTrue(copy.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); string copyData = DownloadTextTask(copy, Encoding.UTF8); Assert.AreEqual(data, copyData, "Data inside copy of blob not similar"); copy.FetchAttributesAsync().Wait(); BlobProperties prop1 = copy.Properties; BlobProperties prop2 = source.Properties; Assert.AreEqual(prop1.CacheControl, prop2.CacheControl); Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding); Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage); Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5); Assert.AreEqual(prop1.ContentType, prop2.ContentType); Assert.AreEqual("value", copy.Metadata["Test"], false, "Copied metadata not same"); copy.DeleteAsync().Wait(); } finally { container.DeleteIfExistsAsync().Wait(); } }
public async Task CloudPageBlobCopyTestWithMetadataOverrideAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob source = container.GetPageBlobReference("source"); string data = new string('a', 512); await UploadTextAsync(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; await source.SetMetadataAsync(); CloudPageBlob copy = container.GetPageBlobReference("copy"); copy.Metadata["Test2"] = "value2"; string copyId = await copy.StartCopyFromBlobAsync(TestHelper.Defiddler(source)); await WaitForCopyAsync(copy); Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status); Assert.AreEqual(source.Uri.AbsolutePath, copy.CopyState.Source.AbsolutePath); Assert.AreEqual(data.Length, copy.CopyState.TotalBytes); Assert.AreEqual(data.Length, copy.CopyState.BytesCopied); Assert.AreEqual(copyId, copy.CopyState.CopyId); Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); string copyData = await DownloadTextAsync(copy, Encoding.UTF8); Assert.AreEqual(data, copyData, "Data inside copy of blob not similar"); await copy.FetchAttributesAsync(); await source.FetchAttributesAsync(); BlobProperties prop1 = copy.Properties; BlobProperties prop2 = source.Properties; Assert.AreEqual(prop1.CacheControl, prop2.CacheControl); Assert.AreEqual(prop1.ContentDisposition, prop2.ContentDisposition); Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding); Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage); Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5); Assert.AreEqual(prop1.ContentType, prop2.ContentType); Assert.AreEqual("value2", copy.Metadata["Test2"], false, "Copied metadata not same"); Assert.IsFalse(copy.Metadata.ContainsKey("Test"), "Source Metadata should not appear in destination blob"); await copy.DeleteAsync(); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
public async Task CloudPageBlobSnapshotMetadataAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); await blob.CreateAsync(1024); blob.Metadata["Hello"] = "World"; blob.Metadata["Marco"] = "Polo"; await blob.SetMetadataAsync(); IDictionary <string, string> snapshotMetadata = new Dictionary <string, string>(); snapshotMetadata["Hello"] = "Dolly"; snapshotMetadata["Yoyo"] = "Ma"; CloudPageBlob snapshot = await blob.CreateSnapshotAsync(snapshotMetadata, null, null, null); // Test the client view against the expected metadata // None of the original metadata should be present Assert.AreEqual("Dolly", snapshot.Metadata["Hello"]); Assert.AreEqual("Ma", snapshot.Metadata["Yoyo"]); Assert.IsFalse(snapshot.Metadata.ContainsKey("Marco")); // Test the server view against the expected metadata await snapshot.FetchAttributesAsync(); Assert.AreEqual("Dolly", snapshot.Metadata["Hello"]); Assert.AreEqual("Ma", snapshot.Metadata["Yoyo"]); Assert.IsFalse(snapshot.Metadata.ContainsKey("Marco")); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
public async Task CloudPageBlobCopyFromSnapshotTestAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob source = container.GetPageBlobReference("source"); string data = new string('a', 512); await UploadTextAsync(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; await source.SetMetadataAsync(); CloudPageBlob snapshot = await source.CreateSnapshotAsync(); //Modify source string newData = new string('b', 512); source.Metadata["Test"] = "newvalue"; await source.SetMetadataAsync(); source.Properties.ContentMD5 = null; await UploadTextAsync(source, newData, Encoding.UTF8); Assert.AreEqual(newData, await DownloadTextAsync(source, Encoding.UTF8), "Source is modified correctly"); Assert.AreEqual(data, await DownloadTextAsync(snapshot, Encoding.UTF8), "Modifying source blob should not modify snapshot"); await source.FetchAttributesAsync(); await snapshot.FetchAttributesAsync(); Assert.AreNotEqual(source.Metadata["Test"], snapshot.Metadata["Test"], "Source and snapshot metadata should be independent"); CloudPageBlob copy = container.GetPageBlobReference("copy"); await copy.StartCopyFromBlobAsync(TestHelper.Defiddler(snapshot)); await WaitForCopyAsync(copy); Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status); Assert.AreEqual(data, await DownloadTextAsync(copy, Encoding.UTF8), "Data inside copy of blob not similar"); await copy.FetchAttributesAsync(); BlobProperties prop1 = copy.Properties; BlobProperties prop2 = snapshot.Properties; Assert.AreEqual(prop1.CacheControl, prop2.CacheControl); Assert.AreEqual(prop1.ContentDisposition, prop2.ContentDisposition); Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding); Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage); Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5); Assert.AreEqual(prop1.ContentType, prop2.ContentType); Assert.AreEqual("value", copy.Metadata["Test"], false, "Copied metadata not same"); await copy.DeleteAsync(); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
public async Task PageBlobReadLockToETagTestAsync() { byte[] outBuffer = new byte[1 * 1024 * 1024]; byte[] buffer = GetRandomBuffer(2 * outBuffer.Length); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); blob.StreamMinimumReadSizeInBytes = outBuffer.Length; using (MemoryStream wholeBlob = new MemoryStream(buffer)) { await blob.UploadFromStreamAsync(wholeBlob.AsInputStream()); } OperationContext opContext = new OperationContext(); using (IRandomAccessStreamWithContentType blobStream = await blob.OpenReadAsync(null, null, opContext)) { Stream blobStreamForRead = blobStream.AsStreamForRead(); await blob.SetMetadataAsync(); await blobStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length); await blob.SetMetadataAsync(); await TestHelper.ExpectedExceptionAsync( async() => await blobStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length), opContext, "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } opContext = new OperationContext(); using (IRandomAccessStreamWithContentType blobStream = await blob.OpenReadAsync(null, null, opContext)) { Stream blobStreamForRead = blobStream.AsStreamForRead(); await blob.SetMetadataAsync(); long length = blobStreamForRead.Length; await blob.SetMetadataAsync(); await TestHelper.ExpectedExceptionAsync( async() => await blobStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length), opContext, "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } opContext = new OperationContext(); AccessCondition accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(DateTimeOffset.Now); using (IRandomAccessStreamWithContentType blobStream = await blob.OpenReadAsync(accessCondition, null, opContext)) { Stream blobStreamForRead = blobStream.AsStreamForRead(); await blob.SetMetadataAsync(); await blobStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length); await blob.SetMetadataAsync(); await TestHelper.ExpectedExceptionAsync( async() => await blobStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length), opContext, "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
public async Task CloudPageBlobConditionalAccessAsync() { OperationContext operationContext = new OperationContext(); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); await blob.CreateAsync(1024); await blob.FetchAttributesAsync(); string currentETag = blob.Properties.ETag; DateTimeOffset currentModifiedTime = blob.Properties.LastModified.Value; // ETag conditional tests blob.Metadata["ETagConditionalName"] = "ETagConditionalValue"; await blob.SetMetadataAsync(AccessCondition.GenerateIfMatchCondition(currentETag), null, null); await blob.FetchAttributesAsync(); string newETag = blob.Properties.ETag; Assert.AreNotEqual(newETag, currentETag, "ETage should be modified on write metadata"); blob.Metadata["ETagConditionalName"] = "ETagConditionalValue2"; await TestHelper.ExpectedExceptionAsync( async() => await blob.SetMetadataAsync(AccessCondition.GenerateIfNoneMatchCondition(newETag), null, operationContext), operationContext, "If none match on conditional test should throw", HttpStatusCode.PreconditionFailed, "ConditionNotMet"); string invalidETag = "\"0x10101010\""; await TestHelper.ExpectedExceptionAsync( async() => await blob.SetMetadataAsync(AccessCondition.GenerateIfMatchCondition(invalidETag), null, operationContext), operationContext, "Invalid ETag on conditional test should throw", HttpStatusCode.PreconditionFailed, "ConditionNotMet"); currentETag = blob.Properties.ETag; await blob.SetMetadataAsync(AccessCondition.GenerateIfNoneMatchCondition(invalidETag), null, null); await blob.FetchAttributesAsync(); newETag = blob.Properties.ETag; // LastModifiedTime tests currentModifiedTime = blob.Properties.LastModified.Value; blob.Metadata["DateConditionalName"] = "DateConditionalValue"; await TestHelper.ExpectedExceptionAsync( async() => await blob.SetMetadataAsync(AccessCondition.GenerateIfModifiedSinceCondition(currentModifiedTime), null, operationContext), operationContext, "IfModifiedSince conditional on current modified time should throw", HttpStatusCode.PreconditionFailed, "ConditionNotMet"); DateTimeOffset pastTime = currentModifiedTime.Subtract(TimeSpan.FromMinutes(5)); await blob.SetMetadataAsync(AccessCondition.GenerateIfModifiedSinceCondition(pastTime), null, null); pastTime = currentModifiedTime.Subtract(TimeSpan.FromHours(5)); await blob.SetMetadataAsync(AccessCondition.GenerateIfModifiedSinceCondition(pastTime), null, null); pastTime = currentModifiedTime.Subtract(TimeSpan.FromDays(5)); await blob.SetMetadataAsync(AccessCondition.GenerateIfModifiedSinceCondition(pastTime), null, null); currentModifiedTime = blob.Properties.LastModified.Value; pastTime = currentModifiedTime.Subtract(TimeSpan.FromMinutes(5)); await TestHelper.ExpectedExceptionAsync( async() => await blob.SetMetadataAsync(AccessCondition.GenerateIfNotModifiedSinceCondition(pastTime), null, operationContext), operationContext, "IfNotModifiedSince conditional on past time should throw", HttpStatusCode.PreconditionFailed, "ConditionNotMet"); pastTime = currentModifiedTime.Subtract(TimeSpan.FromHours(5)); await TestHelper.ExpectedExceptionAsync( async() => await blob.SetMetadataAsync(AccessCondition.GenerateIfNotModifiedSinceCondition(pastTime), null, operationContext), operationContext, "IfNotModifiedSince conditional on past time should throw", HttpStatusCode.PreconditionFailed, "ConditionNotMet"); pastTime = currentModifiedTime.Subtract(TimeSpan.FromDays(5)); await TestHelper.ExpectedExceptionAsync( async() => await blob.SetMetadataAsync(AccessCondition.GenerateIfNotModifiedSinceCondition(pastTime), null, operationContext), operationContext, "IfNotModifiedSince conditional on past time should throw", HttpStatusCode.PreconditionFailed, "ConditionNotMet"); blob.Metadata["DateConditionalName"] = "DateConditionalValue2"; currentETag = blob.Properties.ETag; await blob.SetMetadataAsync(AccessCondition.GenerateIfNotModifiedSinceCondition(currentModifiedTime), null, null); await blob.FetchAttributesAsync(); newETag = blob.Properties.ETag; Assert.AreNotEqual(newETag, currentETag, "ETage should be modified on write metadata"); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
async Task Commit_Length_For(CloudPageBlob blob, long length) { blob.Metadata[Metadata_Size] = length + ""; await blob.SetMetadataAsync(); }