private static async Task CloudBlockBlobCopyFromCloudFileImpl(Func <CloudFile, CloudBlockBlob, string> copyFunc) { CloudFileClient fileClient = GenerateCloudFileClient(); string name = GetRandomContainerName(); CloudFileShare share = fileClient.GetShareReference(name); CloudBlobContainer container = GetRandomContainerReference(); try { try { await container.CreateAsync(); await share.CreateAsync(); CloudFile source = share.GetRootDirectoryReference().GetFileReference("source"); byte[] data = GetRandomBuffer(1024); await source.UploadFromByteArrayAsync(data, 0, data.Length); source.Metadata["Test"] = "value"; source.SetMetadataAsync().Wait(); var sasToken = source.GetSharedAccessSignature(new SharedAccessFilePolicy { Permissions = SharedAccessFilePermissions.Read, SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24) }); Uri fileSasUri = new Uri(source.StorageUri.PrimaryUri.ToString() + sasToken); source = new CloudFile(fileSasUri); CloudBlockBlob copy = container.GetBlockBlobReference("copy"); string copyId = copyFunc(source, copy); Assert.AreEqual(BlobType.BlockBlob, copy.BlobType); try { 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.IsFalse(copy.Properties.IsIncrementalCopy); Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); } catch (NullReferenceException) { // potential null ref in the WaitForCopyTask and CopyState check implementation } OperationContext opContext = new OperationContext(); await TestHelper.ExpectedExceptionAsync( async() => await copy.AbortCopyAsync(copyId, null, null, opContext), opContext, "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))); byte[] copyData = new byte[source.Properties.Length]; await copy.DownloadToByteArrayAsync(copyData, 0); Assert.IsTrue(data.SequenceEqual(copyData), "Data inside copy of blob not similar"); copy.FetchAttributesAsync().Wait(); BlobProperties 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.DeleteIfExistsAsync().Wait(); } finally { share.DeleteIfExistsAsync().Wait(); } } finally { container.DeleteIfExistsAsync().Wait(); } }
public static void Create(this CloudFileShare share, FileRequestOptions requestOptions = null, OperationContext operationContext = null) { share.CreateAsync(requestOptions, operationContext).GetAwaiter().GetResult(); }
public Task CreateShareAsync(CloudFileShare share, FileRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken) { return(share.CreateAsync(options, operationContext, cancellationToken)); }
public async Task RunPermissionsTestFiles(SharedAccessAccountPolicy policy) { CloudFileClient fileClient = GenerateCloudFileClient(); string shareName = "s" + Guid.NewGuid().ToString("N"); try { CloudStorageAccount account = new CloudStorageAccount(fileClient.Credentials, false); string accountSASToken = account.GetSharedAccessSignature(policy); StorageCredentials accountSAS = new StorageCredentials(accountSASToken); CloudStorageAccount accountWithSAS = new CloudStorageAccount(accountSAS, null, null, null, fileClient.StorageUri); CloudFileClient fileClientWithSAS = accountWithSAS.CreateCloudFileClient(); CloudFileShare shareWithSAS = fileClientWithSAS.GetShareReference(shareName); CloudFileShare share = fileClient.GetShareReference(shareName); // General pattern - If current perms support doing a thing with SAS, do the thing with SAS and validate with shared // Otherwise, make sure SAS fails and then do the thing with shared key. // Things to do: // Create the share (Create / Write perms, Container RT) // List shares with prefix (List perms, Service RT) // Create a new file (Create / Write, Object RT) // Add a range to the file (Write, Object RT) // Read the data from the file (Read, Object RT) // Overwrite a file (Write, Object RT) // Delete the file (Delete perms, Object RT) if ((((policy.Permissions & SharedAccessAccountPermissions.Create) == SharedAccessAccountPermissions.Create) || ((policy.Permissions & SharedAccessAccountPermissions.Write) == SharedAccessAccountPermissions.Write)) && ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Container) == SharedAccessAccountResourceTypes.Container)) { await shareWithSAS.CreateAsync(); } else { await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await shareWithSAS.CreateAsync(), "Creating a share with SAS should fail without Create or Write and Container-level perms."); await share.CreateAsync(); } Assert.IsTrue(await share.ExistsAsync()); if (((policy.Permissions & SharedAccessAccountPermissions.List) == SharedAccessAccountPermissions.List) && ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Service) == SharedAccessAccountResourceTypes.Service)) { Assert.AreEqual(shareName, (await fileClientWithSAS.ListSharesSegmentedAsync(shareName, null)).Results.First().Name); } else { await TestHelper.ExpectedExceptionAsync <StorageException>(async() => (await fileClientWithSAS.ListSharesSegmentedAsync(shareName, null)).Results.First(), "Listing shared with SAS should fail without List and Service-level perms."); } string filename = "fileName"; CloudFile fileWithSAS = shareWithSAS.GetRootDirectoryReference().GetFileReference(filename); CloudFile file = share.GetRootDirectoryReference().GetFileReference(filename); //Try creating credentials using SAS Uri directly CloudFile fileWithSASUri = new CloudFile(new Uri(share.Uri + accountSASToken)); byte[] content = new byte[] { 0x1, 0x2, 0x3, 0x4 }; if ((((policy.Permissions & SharedAccessAccountPermissions.Create) == SharedAccessAccountPermissions.Create) || ((policy.Permissions & SharedAccessAccountPermissions.Write) == SharedAccessAccountPermissions.Write)) && ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object)) { await fileWithSAS.CreateAsync(content.Length); } else { await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await fileWithSAS.CreateAsync(content.Length), "Creating a file with SAS should fail without Create or Write and Object-level perms."); await file.CreateAsync(content.Length); } Assert.IsTrue(await file.ExistsAsync()); using (MemoryStream stream = new MemoryStream(content)) { if (((policy.Permissions & SharedAccessAccountPermissions.Write) == SharedAccessAccountPermissions.Write) && ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object)) { await fileWithSAS.WriteRangeAsync(stream, 0, null); } else { await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await fileWithSAS.WriteRangeAsync(stream, 0, null), "Writing a range to a file with SAS should fail without Write and Object-level perms."); stream.Seek(0, SeekOrigin.Begin); await file.WriteRangeAsync(stream, 0, null); } } byte[] result = new byte[content.Length]; await file.DownloadRangeToByteArrayAsync(result, 0, 0, content.Length); for (int i = 0; i < content.Length; i++) { Assert.AreEqual(content[i], result[i]); } if (((policy.Permissions & SharedAccessAccountPermissions.Read) == SharedAccessAccountPermissions.Read) && ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object)) { result = new byte[content.Length]; await fileWithSAS.DownloadRangeToByteArrayAsync(result, 0, 0, content.Length); for (int i = 0; i < content.Length; i++) { Assert.AreEqual(content[i], result[i]); } } else { await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await fileWithSAS.DownloadRangeToByteArrayAsync(result, 0, 0, content.Length), "Reading a file with SAS should fail without Read and Object-level perms."); } if (((policy.Permissions & SharedAccessAccountPermissions.Write) == SharedAccessAccountPermissions.Write) && ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object)) { await fileWithSAS.CreateAsync(2); } else { await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await fileWithSAS.CreateAsync(2), "Overwriting a file with SAS should fail without Write and Object-level perms."); await file.CreateAsync(2); } result = new byte[content.Length]; await file.DownloadRangeToByteArrayAsync(result, 0, 0, content.Length); for (int i = 0; i < content.Length; i++) { Assert.AreEqual(0, result[i]); } if (((policy.Permissions & SharedAccessAccountPermissions.Delete) == SharedAccessAccountPermissions.Delete) && ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object)) { await fileWithSAS.DeleteAsync(); } else { await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await fileWithSAS.DeleteAsync(), "Deleting a file with SAS should fail without Delete and Object-level perms."); await file.DeleteAsync(); } Assert.IsFalse(await file.ExistsAsync()); } finally { fileClient.GetShareReference(shareName).DeleteIfExistsAsync().Wait(); } }