public async Task CloudBlockBlobSnapshotAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob blob = container.GetBlockBlobReference("blob1"); await CreateForTestAsync(blob, 2, 1024); CloudBlockBlob snapshot1 = await blob.CreateSnapshotAsync(); Assert.AreEqual(blob.Properties.ETag, snapshot1.Properties.ETag); Assert.AreEqual(blob.Properties.LastModified, snapshot1.Properties.LastModified); Assert.IsNotNull(snapshot1.SnapshotTime, "Snapshot does not have SnapshotTime set"); CloudBlockBlob snapshot2 = await blob.CreateSnapshotAsync(); Assert.IsTrue(snapshot2.SnapshotTime.Value > snapshot1.SnapshotTime.Value); await snapshot1.FetchAttributesAsync(); await snapshot2.FetchAttributesAsync(); await blob.FetchAttributesAsync(); AssertAreEqual(snapshot1.Properties, blob.Properties); CloudBlockBlob snapshotCopy = container.GetBlockBlobReference("blob2"); await snapshotCopy.StartCopyFromBlobAsync(TestHelper.Defiddler(snapshot1.Uri)); await WaitForCopyAsync(snapshotCopy); Assert.AreEqual(CopyStatus.Success, snapshotCopy.CopyState.Status); await TestHelper.ExpectedExceptionAsync <InvalidOperationException>( async() => await snapshot1.OpenWriteAsync(), "Trying to write to a blob snapshot should fail"); BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, null, null, null); List <IListBlobItem> blobs = resultSegment.Results.ToList(); Assert.AreEqual(4, blobs.Count); AssertAreEqual(snapshot1, (ICloudBlob)blobs[0]); AssertAreEqual(snapshot2, (ICloudBlob)blobs[1]); AssertAreEqual(blob, (ICloudBlob)blobs[2]); AssertAreEqual(snapshotCopy, (ICloudBlob)blobs[3]); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
public async Task CloudBlockBlobSnapshotMetadataAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob blob = container.GetBlockBlobReference("blob1"); await CreateForTestAsync(blob, 2, 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"; CloudBlockBlob 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 CloudBlockBlobCopyFromSnapshotTestAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob source = container.GetBlockBlobReference("source"); string data = "String data"; await UploadTextAsync(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; await source.SetMetadataAsync(); CloudBlockBlob snapshot = await source.CreateSnapshotAsync(); //Modify source string newData = "Hello"; 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"); CloudBlockBlob copy = container.GetBlockBlobReference("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.ContentEncoding, prop2.ContentEncoding); Assert.AreEqual(prop1.ContentDisposition, prop2.ContentDisposition); 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(); } }
/// <summary> /// Test blob reads, expecting success. /// </summary> /// <param name="testBlob">The blob to test.</param> /// <param name="targetBlob">The blob to use for the target of copy operations.</param> /// <param name="testAccessCondition">The access condition to use.</param> private async Task BlobReadExpectLeaseSuccessAsync(CloudBlockBlob testBlob, AccessCondition testAccessCondition) { await testBlob.FetchAttributesAsync(testAccessCondition, null /* options */, null); await (await testBlob.CreateSnapshotAsync(null /* metadata */, testAccessCondition, null /* options */, null)).DeleteAsync(); await DownloadTextAsync(testBlob, Encoding.UTF8, testAccessCondition, null /* options */, null); IRandomAccessStreamWithContentType readStream = await testBlob.OpenReadAsync(testAccessCondition, null /* options */, null); Stream stream = readStream.AsStreamForRead(); stream.ReadByte(); }
/// <summary> /// Test blob reads, expecting lease failure. /// </summary> /// <param name="testBlob">The blob to test.</param> /// <param name="targetBlob">The blob to use for the target of copy operations.</param> /// <param name="testAccessCondition">The failing access condition to use.</param> /// <param name="expectedErrorCode">The expected error code.</param> /// <param name="description">The reason why these calls should fail.</param> private async Task BlobReadExpectLeaseFailureAsync(CloudBlockBlob testBlob, CloudBlockBlob targetBlob, AccessCondition testAccessCondition, HttpStatusCode expectedStatusCode, string expectedErrorCode, string description) { OperationContext operationContext = new OperationContext(); // FetchAttributes is a HEAD request with no extended error info, so it returns with the generic ConditionFailed error code. await TestHelper.ExpectedExceptionAsync( async () => await testBlob.FetchAttributesAsync(testAccessCondition, null /* options */, operationContext), operationContext, description + "(Fetch Attributes)", HttpStatusCode.PreconditionFailed); await TestHelper.ExpectedExceptionAsync( async () => await testBlob.CreateSnapshotAsync(null /* metadata */, testAccessCondition, null /* options */, operationContext), operationContext, description + " (Create Snapshot)", expectedStatusCode, expectedErrorCode); await TestHelper.ExpectedExceptionAsync( async () => await DownloadTextAsync(testBlob, Encoding.UTF8, testAccessCondition, null /* options */, operationContext), operationContext, description + " (Download Text)", expectedStatusCode, expectedErrorCode); byte[] buffer = new byte[1]; IRandomAccessStreamWithContentType readStream = await testBlob.OpenReadAsync(testAccessCondition, null /* options */, operationContext); Stream stream = readStream.AsStreamForRead(); await TestHelper.ExpectedExceptionAsync( async () => await stream.ReadAsync(buffer, 0, 1), operationContext, description + " (Read Stream)", expectedStatusCode/*, expectedErrorCode*/); }
/// <summary> /// Test blob reads, expecting success. /// </summary> /// <param name="testBlob">The blob to test.</param> /// <param name="testAccessCondition">The access condition to use.</param> private void BlobReadExpectLeaseSuccessTask(CloudBlockBlob testBlob, AccessCondition testAccessCondition) { testBlob.FetchAttributesAsync(testAccessCondition, null /* options */, new OperationContext()); testBlob.CreateSnapshotAsync(null /* metadata */, testAccessCondition, null /* options */, new OperationContext()).Result.Delete(); DownloadTextTask(testBlob, Encoding.UTF8, testAccessCondition, null /* options */); Stream stream = testBlob.OpenReadAsync(testAccessCondition, null /* options */, new OperationContext()).Result; stream.ReadByte(); }
/// <summary> /// Test blob reads, expecting lease failure. /// </summary> /// <param name="testBlob">The blob to test.</param> /// <param name="targetBlob">The blob to use for the target of copy operations.</param> /// <param name="testAccessCondition">The failing access condition to use.</param> /// <param name="expectedErrorCode">The expected error code.</param> /// <param name="description">The reason why these calls should fail.</param> private void BlobReadExpectLeaseFailureTask(CloudBlockBlob testBlob, CloudBlockBlob targetBlob, AccessCondition testAccessCondition, HttpStatusCode expectedStatusCode, string expectedErrorCode, string description) { // FetchAttributes is a HEAD request with no extended error info, so it returns with the generic ConditionFailed error code. TestHelper.ExpectedExceptionTask( testBlob.FetchAttributesAsync(testAccessCondition, null /* options */, new OperationContext()), description + "(Fetch Attributes)", HttpStatusCode.PreconditionFailed); TestHelper.ExpectedExceptionTask( testBlob.CreateSnapshotAsync(null /* metadata */, testAccessCondition, null /* options */, new OperationContext()), description + " (Create Snapshot)", expectedStatusCode, expectedErrorCode); TestHelper.ExpectedException( () => DownloadTextTask(testBlob, Encoding.UTF8, testAccessCondition, null /* options */), description + " (Download Text)", expectedStatusCode, expectedErrorCode); TestHelper.ExpectedExceptionTask( testBlob.OpenReadAsync(testAccessCondition, null /* options */, new OperationContext()), description + " (Read Stream)", expectedStatusCode/*, expectedErrorCode*/); }