//Copies a directory of files to Blob, with encryption. The full origin path is preserved. public void WriteDirectory(string DirectoryPath, bool Recurse, string ContainerName, Microsoft.Azure.KeyVault.Core.IKey Key) { string[] files; CreateContainer(ContainerName); if (Recurse) { files = Directory.GetFiles(DirectoryPath, "*.*", SearchOption.AllDirectories); } else { files = Directory.GetFiles(DirectoryPath, "*.*", SearchOption.TopDirectoryOnly); } Parallel.ForEach(files, (currentFile) => { WriteBlobFile(currentFile, GetFileNameWithoutDriveLetter(currentFile), ContainerName, Key); }); }
private Mock <Microsoft.Azure.KeyVault.Core.IKeyResolver> GetTrackOneIKeyResolver(Microsoft.Azure.KeyVault.Core.IKey iKey) { var resolverMock = new Mock <Microsoft.Azure.KeyVault.Core.IKeyResolver>(MockBehavior.Strict); resolverMock.Setup(r => r.ResolveKeyAsync(IsNotNull <string>(), IsNotNull <CancellationToken>())) // track 1 doesn't pass in the same cancellation token? .Returns <string, CancellationToken>((keyId, cancellationToken) => iKey?.Kid == keyId ? Task.FromResult(iKey) : throw new Exception("Mock resolver couldn't resolve key id.")); return(resolverMock); }
//Upload a blob from a stream to a named container, using a KeyVault Key to dynamically encryprt the file public void WriteBlobFile(string FileName, string BlobName, string ContainerName, Microsoft.Azure.KeyVault.Core.IKey Key) { CloudBlobContainer container = GetContainerRefference(ContainerName); // Now you simply use the RSA key to encrypt by setting it in the BlobEncryptionPolicy. BlobEncryptionPolicy policy = new BlobEncryptionPolicy(Key, null); BlobRequestOptions options = new BlobRequestOptions() { EncryptionPolicy = policy }; // Retrieve reference to a blob named "myblob". CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName); // Create or overwrite the "FileName" blob with contents from FileStream, using encryption option blockBlob.UploadFromFile(FileName, null, options, null); }