public async Task CloudFileDirectoryWithFilesDeleteAsync() { CloudFileClient client = GenerateCloudFileClient(); string name = GetRandomShareName(); CloudFileShare share = client.GetShareReference(name); try { await share.CreateAsync(); if (await CloudFileDirectorySetupAsync(share)) { CloudFileDirectory dir1 = share.GetRootDirectoryReference().GetDirectoryReference("TopDir1/MidDir1/EndDir1"); CloudFile file1 = dir1.GetFileReference("EndFile1"); OperationContext context = new OperationContext(); await TestHelper.ExpectedExceptionAsync( async() => await dir1.DeleteAsync(null, null, context), context, "Delete a non-empty directory", HttpStatusCode.Conflict); await file1.DeleteAsync(); await dir1.DeleteAsync(); Assert.IsFalse(await file1.ExistsAsync()); Assert.IsFalse(await dir1.ExistsAsync()); } } finally { share.DeleteAsync().Wait(); } }
public void CloudFileCopyTestTask() { CloudFileShare share = GetRandomShareReference(); try { share.CreateAsync().Wait(); CloudFile source = share.GetRootDirectoryReference().GetFileReference("source"); string data = "String data"; UploadTextTask(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; source.SetMetadataAsync().Wait(); CloudFile copy = share.GetRootDirectoryReference().GetFileReference("copy"); string copyId = copy.StartCopyAsync(TestHelper.Defiddler(source)).Result; 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 file not similar"); copy.FetchAttributesAsync().Wait(); FileProperties prop1 = copy.Properties; FileProperties 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 { share.DeleteIfExistsAsync().Wait(); } }
public async Task CloudFileCopyTestWithMetadataOverrideAsync() { CloudFileShare share = GetRandomShareReference(); try { await share.CreateAsync(); CloudFile source = share.GetRootDirectoryReference().GetFileReference("source"); string data = "String data"; await UploadTextAsync(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; await source.SetMetadataAsync(); CloudFile copy = share.GetRootDirectoryReference().GetFileReference("copy"); copy.Metadata["Test2"] = "value2"; string copyId = await copy.StartCopyAsync(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 file not similar"); await copy.FetchAttributesAsync(); await source.FetchAttributesAsync(); FileProperties prop1 = copy.Properties; FileProperties prop2 = source.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("value2", copy.Metadata["Test2"], false, "Copied metadata not same"); Assert.IsFalse(copy.Metadata.ContainsKey("Test"), "Source Metadata should not appear in destination file"); await copy.DeleteAsync(); } finally { await share.DeleteAsync(); } }
private static async Task CloudFileCopyAsync(bool sourceIsSas, bool destinationIsSas) { CloudFileShare share = GetRandomShareReference(); try { await share.CreateAsync(); // Create Source on server CloudFile source = share.GetRootDirectoryReference().GetFileReference("source"); string data = "String data"; await UploadTextAsync(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; await source.SetMetadataAsync(); // Create Destination on server CloudFile destination = share.GetRootDirectoryReference().GetFileReference("destination"); await destination.CreateAsync(1); CloudFile copySource = source; CloudFile copyDestination = destination; if (sourceIsSas) { // Source SAS must have read permissions SharedAccessFilePermissions permissions = SharedAccessFilePermissions.Read; SharedAccessFilePolicy policy = new SharedAccessFilePolicy() { SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), Permissions = permissions, }; string sasToken = source.GetSharedAccessSignature(policy); // Get source StorageCredentials credentials = new StorageCredentials(sasToken); copySource = new CloudFile(credentials.TransformUri(source.Uri)); } if (destinationIsSas) { Assert.IsTrue(sourceIsSas); // Destination SAS must have write permissions SharedAccessFilePermissions permissions = SharedAccessFilePermissions.Write; SharedAccessFilePolicy policy = new SharedAccessFilePolicy() { SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), Permissions = permissions, }; string sasToken = destination.GetSharedAccessSignature(policy); // Get destination StorageCredentials credentials = new StorageCredentials(sasToken); copyDestination = new CloudFile(credentials.TransformUri(destination.Uri)); } // Start copy and wait for completion string copyId = await copyDestination.StartCopyAsync(TestHelper.Defiddler(copySource)); await WaitForCopyAsync(destination); // Check original file references for equality Assert.AreEqual(CopyStatus.Success, destination.CopyState.Status); Assert.AreEqual(source.Uri.AbsolutePath, destination.CopyState.Source.AbsolutePath); Assert.AreEqual(data.Length, destination.CopyState.TotalBytes); Assert.AreEqual(data.Length, destination.CopyState.BytesCopied); Assert.AreEqual(copyId, destination.CopyState.CopyId); Assert.IsTrue(destination.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); if (!destinationIsSas) { // Abort Copy is not supported for SAS destination OperationContext context = new OperationContext(); await TestHelper.ExpectedExceptionAsync( async() => await copyDestination.AbortCopyAsync(copyId, null, null, context), context, "Aborting a copy operation after completion should fail", HttpStatusCode.Conflict, "NoPendingCopyOperation"); } await source.FetchAttributesAsync(); Assert.IsNotNull(destination.Properties.ETag); Assert.AreNotEqual(source.Properties.ETag, destination.Properties.ETag); Assert.IsTrue(destination.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); string copyData = await DownloadTextAsync(destination, Encoding.UTF8); Assert.AreEqual(data, copyData, "Data inside copy of file not equal."); await destination.FetchAttributesAsync(); FileProperties prop1 = destination.Properties; FileProperties 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", destination.Metadata["Test"], false, "Copied metadata not same"); await destination.DeleteAsync(); await source.DeleteAsync(); } finally { await share.DeleteAsync(); } }