private async Task IncrementalCopyAsyncImpl(int overload) { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob source = container.GetPageBlobReference("source"); await source.CreateAsync(1024); string data = new string('a', 512); await UploadTextAsync(source, data, Encoding.UTF8); CloudPageBlob sourceSnapshot = await source.CreateSnapshotAsync(null, null, null, null); SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write, }; string sasToken = sourceSnapshot.GetSharedAccessSignature(policy); StorageCredentials blobSAS = new StorageCredentials(sasToken); Uri sourceSnapshotUri = blobSAS.TransformUri(TestHelper.Defiddler(sourceSnapshot).SnapshotQualifiedUri); StorageCredentials accountSAS = new StorageCredentials(sasToken); CloudStorageAccount accountWithSAS = new CloudStorageAccount(accountSAS, source.ServiceClient.StorageUri, null, null, null); CloudPageBlob snapshotWithSas = await accountWithSAS.CreateCloudBlobClient().GetBlobReferenceFromServerAsync(sourceSnapshot.SnapshotQualifiedUri) as CloudPageBlob; CloudPageBlob copy = container.GetPageBlobReference("copy"); string copyId = null; if (overload == 0) { #if !FACADE_NETCORE copyId = await copy.StartIncrementalCopyAsync(accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri)); #else Uri snapShotQualifiedUri = accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri); copyId = await copy.StartIncrementalCopyAsync(new CloudPageBlob(new StorageUri(snapShotQualifiedUri), null, null)); #endif } else if (overload == 1) { #if !FACADE_NETCORE CloudPageBlob blob = new CloudPageBlob(accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri)); #else Uri snapShotQualifiedUri = accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri); CloudPageBlob blob = new CloudPageBlob(new StorageUri(snapShotQualifiedUri), null, null); #endif copyId = await copy.StartIncrementalCopyAsync(blob); } else if (overload == 2) { #if !FACADE_NETCORE CloudPageBlob blob = new CloudPageBlob(accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri)); #else Uri snapShotQualifiedUri = accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri); CloudPageBlob blob = new CloudPageBlob(new StorageUri(snapShotQualifiedUri), null, null); #endif copyId = await copy.StartIncrementalCopyAsync(blob, null, null, null, CancellationToken.None); } else { #if !FACADE_NETCORE copyId = await copy.StartIncrementalCopyAsync(accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri), null, null, null, CancellationToken.None); #else Uri snapShotQualifiedUri = accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri); CloudPageBlob blob = new CloudPageBlob(new StorageUri(snapShotQualifiedUri), null, null); copyId = await copy.StartIncrementalCopyAsync(blob, null, null, null, CancellationToken.None); #endif } await WaitForCopyAsync(copy); Assert.AreEqual(BlobType.PageBlob, copy.BlobType); 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.Properties.IsIncrementalCopy); Assert.IsTrue(copy.CopyState.DestinationSnapshotTime.HasValue); Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); } finally { await container.DeleteAsync(); } }
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.StartCopyAsync(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 { await container.DeleteAsync(); } }