/// <summary> /// Gets the specified <see cref="NodeFile"/> from the <see cref="CloudTask"/>'s directory on its compute node. /// </summary> /// <param name="filePath">The path of the file to retrieve.</param> /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are applied to the Batch service request after the <see cref="CustomBehaviors"/>.</param> /// <returns>A <see cref="NodeFile"/> representing the specified file.</returns> /// <remarks>This is a blocking operation. For a non-blocking equivalent, see <see cref="GetNodeFileAsync"/>.</remarks> public NodeFile GetNodeFile(string filePath, IEnumerable <BatchClientBehavior> additionalBehaviors = null) { Task <NodeFile> asyncTask = this.GetNodeFileAsync(filePath, additionalBehaviors); NodeFile file = asyncTask.WaitAndUnaggregateException(this.CustomBehaviors, additionalBehaviors); return(file); }
internal PSNodeFile(Microsoft.Azure.Batch.NodeFile omObject) { if ((omObject == null)) { throw new System.ArgumentNullException("omObject"); } this.omObject = omObject; }
/// <summary> /// Gets the specified <see cref="NodeFile"/> from the <see cref="CloudTask"/>'s directory on its compute node. /// </summary> /// <param name="fileName">The name of the file to retrieve.</param> /// <param name="additionalBehaviors">A collection of <see cref="BatchClientBehavior"/> instances that are applied to the Batch service request after the <see cref="CustomBehaviors"/>.</param> /// <returns>A <see cref="NodeFile"/> representing the specified file.</returns> /// <remarks>This is a blocking operation. For a non-blocking equivalent, see <see cref="GetNodeFileAsync"/>.</remarks> public NodeFile GetNodeFile(string fileName, IEnumerable <BatchClientBehavior> additionalBehaviors = null) { using (System.Threading.Tasks.Task <NodeFile> asyncTask = this.GetNodeFileAsync(fileName, additionalBehaviors)) { NodeFile file = asyncTask.WaitAndUnaggregateException(this.CustomBehaviors, additionalBehaviors); return(file); } }
// Downloads the file represented by an NodeFile instance to the specified path. private void DownloadNodeFileByInstance(NodeFile file, string destinationPath, Stream stream, IEnumerable <BatchClientBehavior> additionalBehaviors = null) { if (stream != null) { // Don't dispose supplied Stream file.CopyToStream(stream, additionalBehaviors); } else { WriteVerbose(string.Format(Resources.DownloadingNodeFile, file.Name, destinationPath)); using (FileStream fs = new FileStream(destinationPath, FileMode.Create)) { file.CopyToStream(fs, additionalBehaviors); } } }
private async Task VerifyOcpRangeSetWhenDownloadingFileAsync < TPropertiesRequest, TPropertiesOptions, TPropertiesHeaders, TDownloadRequest, TDownloadOptions, TDownloadHeaders>( Func <TDownloadOptions, string> getOcpRangeFunc, Func <BatchClient, Task <Microsoft.Azure.Batch.NodeFile> > getNodeFilePropertiesFunc, Func <Microsoft.Azure.Batch.NodeFile, GetFileRequestByteRange, Task> downloadFileFunc) where TPropertiesRequest : Protocol.BatchRequest <TPropertiesOptions, AzureOperationHeaderResponse <TPropertiesHeaders> > where TPropertiesOptions : IOptions, new() where TPropertiesHeaders : IProtocolNodeFile, new() where TDownloadRequest : Protocol.BatchRequest <TDownloadOptions, AzureOperationResponse <Stream, TDownloadHeaders> > where TDownloadOptions : IOptions, new() where TDownloadHeaders : IProtocolNodeFile, new() { const int startRange = 100; const int endRange = 200; string expectedOcpRange = "bytes=100-200"; GetFileRequestByteRange byteRange = new GetFileRequestByteRange(startRange, endRange); // This interceptor verifies that the OcpRange header was properly set according to the // GetFileRequestByteRange object defined above. InvocationTracker invocationTracker = new InvocationTracker(); BatchClientBehavior confirmByteRangeIsSet = CreateOcpRangeConfirmationInterceptor < TDownloadRequest, TDownloadOptions, TDownloadHeaders>( expectedOcpRange, getOcpRangeFunc, invocationTracker); // In order to perform the "get file" API to verify that the OcpRange was set, we need to invoke the // "get file properties" API first to get a NodeFile instance. This interceptor skips the "get file // properties" call to the Batch service and instead builds a fake NodeFile. BatchClientBehavior getFakeNodeFile = CreateFakeNodeFileInterceptor <TPropertiesRequest, TPropertiesOptions, TPropertiesHeaders>(); using (BatchClient client = ClientUnitTestCommon.CreateDummyClient()) { client.CustomBehaviors.Add(confirmByteRangeIsSet); client.CustomBehaviors.Add(getFakeNodeFile); // Get a NodeFile object by invoking the "get file properties" API. Microsoft.Azure.Batch.NodeFile nodeFile = await getNodeFilePropertiesFunc(client); // The download func invokes the "get file" API where the OcpRange header is actually set await downloadFileFunc(nodeFile, byteRange); // Verify the OcpRange validation interceptor was actually invoked Assert.True(invocationTracker.WasInvoked); } }
// Downloads the file represented by an NodeFile instance to the specified path. private void DownloadNodeFileByInstance(NodeFile file, string destinationPath, Stream stream, DownloadNodeFileOptions.ByteRange byteRange, IEnumerable <BatchClientBehavior> additionalBehaviors = null) { // TODO: Update this to use the new built in support in the C# SDK when we update the C# SDK to 6.x or later Protocol.RequestInterceptor interceptor = new Protocol.RequestInterceptor(baseRequest => { var fromTaskRequest = baseRequest as BatchRequests.FileGetFromTaskBatchRequest; if (fromTaskRequest != null && byteRange != null) { fromTaskRequest.Options.OcpRange = $"bytes={byteRange.Start}-{byteRange.End}"; } var fromNodeRequest = baseRequest as BatchRequests.FileGetFromComputeNodeBatchRequest; if (fromNodeRequest != null && byteRange != null) { fromNodeRequest.Options.OcpRange = $"bytes={byteRange.Start}-{byteRange.End}"; } }); additionalBehaviors = additionalBehaviors != null ? new List <BatchClientBehavior> { interceptor }.Union(additionalBehaviors) : new List <BatchClientBehavior>() { interceptor }; if (byteRange != null) { WriteVerbose(string.Format(Resources.DownloadingNodeFileByteRange, byteRange.Start, byteRange.End)); } if (stream != null) { // Don't dispose supplied Stream file.CopyToStream(stream, additionalBehaviors: additionalBehaviors); } else { WriteVerbose(string.Format(Resources.DownloadingNodeFile, file.Path, destinationPath)); using (FileStream fs = new FileStream(destinationPath, FileMode.Create)) { file.CopyToStream(fs, additionalBehaviors: additionalBehaviors); } } }
// Lists the node files under a task. private IEnumerable <PSNodeFile> ListNodeFilesByTask(ListNodeFileOptions options) { // Get the single node file matching the specified name if (!string.IsNullOrEmpty(options.NodeFileName)) { WriteVerbose(string.Format(Resources.GetNodeFileByTaskByName, options.NodeFileName, options.TaskId)); JobOperations jobOperations = options.Context.BatchOMClient.JobOperations; NodeFile nodeFile = jobOperations.GetNodeFile(options.JobId, options.TaskId, options.NodeFileName, options.AdditionalBehaviors); PSNodeFile psNodeFile = new PSNodeFile(nodeFile); return(new PSNodeFile[] { psNodeFile }); } // List node files using the specified filter else { string taskId = options.Task == null ? options.TaskId : options.Task.Id; ODATADetailLevel odata = null; string verboseLogString = null; if (!string.IsNullOrEmpty(options.Filter)) { verboseLogString = string.Format(Resources.GetNodeFileByTaskByOData, taskId); odata = new ODATADetailLevel(filterClause: options.Filter); } else { verboseLogString = string.Format(Resources.GetNodeFileByTaskNoFilter, taskId); } WriteVerbose(verboseLogString); IPagedEnumerable <NodeFile> nodeFiles = null; if (options.Task != null) { nodeFiles = options.Task.omObject.ListNodeFiles(options.Recursive, odata, options.AdditionalBehaviors); } else { JobOperations jobOperations = options.Context.BatchOMClient.JobOperations; nodeFiles = jobOperations.ListNodeFiles(options.JobId, options.TaskId, options.Recursive, odata, options.AdditionalBehaviors); } Func <NodeFile, PSNodeFile> mappingFunction = f => { return(new PSNodeFile(f)); }; return(PSPagedEnumerable <PSNodeFile, NodeFile> .CreateWithMaxCount( nodeFiles, mappingFunction, options.MaxCount, () => WriteVerbose(string.Format(Resources.MaxCount, options.MaxCount)))); } }
/// <summary> /// Downloads a node file using the specified options. /// </summary> /// <param name="options">The download options.</param> public void DownloadNodeFile(DownloadNodeFileOptions options) { if (options == null) { throw new ArgumentNullException("options"); } NodeFile nodeFile = null; switch (options.NodeFileType) { case PSNodeFileType.Task: { JobOperations jobOperations = options.Context.BatchOMClient.JobOperations; nodeFile = jobOperations.GetNodeFile(options.JobId, options.TaskId, options.NodeFileName, options.AdditionalBehaviors); break; } case PSNodeFileType.ComputeNode: { PoolOperations poolOperations = options.Context.BatchOMClient.PoolOperations; nodeFile = poolOperations.GetNodeFile(options.PoolId, options.ComputeNodeId, options.NodeFileName, options.AdditionalBehaviors); break; } case PSNodeFileType.PSNodeFileInstance: { nodeFile = options.NodeFile.omObject; break; } default: { throw new ArgumentException(Resources.NoNodeFile); } } DownloadNodeFileByInstance(nodeFile, options.DestinationPath, options.Stream, options.AdditionalBehaviors); }
// Downloads the file represented by an NodeFile instance to the specified path. private void DownloadNodeFileByInstance(NodeFile file, string destinationPath, Stream stream, IEnumerable<BatchClientBehavior> additionalBehaviors = null) { if (stream != null) { // Don't dispose supplied Stream file.CopyToStream(stream, additionalBehaviors); } else { WriteVerbose(string.Format(Resources.GBNFC_Downloading, file.Name, destinationPath)); using (FileStream fs = new FileStream(destinationPath, FileMode.Create)) { file.CopyToStream(fs, additionalBehaviors); } } }