| | 1 | | // Copyright (c) Microsoft and contributors. All rights reserved. |
| | 2 | | // |
| | 3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
| | 4 | | // you may not use this file except in compliance with the License. |
| | 5 | | // You may obtain a copy of the License at |
| | 6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
| | 7 | | // |
| | 8 | | // Unless required by applicable law or agreed to in writing, software |
| | 9 | | // distributed under the License is distributed on an "AS IS" BASIS, |
| | 10 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| | 11 | | // |
| | 12 | | // See the License for the specific language governing permissions and |
| | 13 | | // limitations under the License. |
| | 14 | |
|
| | 15 | | using Microsoft.Azure.Batch.Conventions.Files.Utilities; |
| | 16 | | using Microsoft.WindowsAzure.Storage; |
| | 17 | | using Microsoft.WindowsAzure.Storage.Blob; |
| | 18 | | using Microsoft.WindowsAzure.Storage.RetryPolicies; |
| | 19 | | using System; |
| | 20 | | using System.Collections.Generic; |
| | 21 | | using System.IO; |
| | 22 | | using System.Linq; |
| | 23 | | using System.Text; |
| | 24 | | using System.Threading; |
| | 25 | | using System.Threading.Tasks; |
| | 26 | |
|
| | 27 | | namespace Microsoft.Azure.Batch.Conventions.Files |
| | 28 | | { |
| | 29 | | /// <summary> |
| | 30 | | /// Represents persistent storage for the outputs of an Azure Batch job. |
| | 31 | | /// </summary> |
| | 32 | | /// <remarks> |
| | 33 | | /// Job outputs refer to output data logically associated with the entire job, rather than |
| | 34 | | /// a particular task. For example, in a movie rendering job, if a task combined all the frames |
| | 35 | | /// into a movie, that would logically be a job output. The purpose of categorising an |
| | 36 | | /// output as a 'job' output is to save the client from having to know which task produced it. |
| | 37 | | /// </remarks> |
| | 38 | | public class JobOutputStorage |
| | 39 | | { |
| | 40 | | private readonly StoragePath _storagePath; |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Initializes a new instance of the <see cref="JobOutputStorage"/> class from a URL representing |
| | 44 | | /// the job output container. |
| | 45 | | /// </summary> |
| | 46 | | /// <param name="jobOutputContainerUri">The URL in Azure storage of the blob container to |
| | 47 | | /// use for job outputs. This URL must contain a SAS (Shared Access Signature) granting |
| | 48 | | /// access to the container, or the container must be public.</param> |
| | 49 | | /// <remarks>The container must already exist; the JobOutputStorage class does not create |
| | 50 | | /// it for you.</remarks> |
| | 51 | | public JobOutputStorage(Uri jobOutputContainerUri) |
| 14 | 52 | | : this(CloudBlobContainerUtils.GetContainerReference(jobOutputContainerUri), null) |
| | 53 | | { |
| 14 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Initializes a new instance of the <see cref="JobOutputStorage"/> class from a storage account |
| | 58 | | /// and job id. |
| | 59 | | /// </summary> |
| | 60 | | /// <param name="storageAccount">The storage account linked to the Azure Batch account.</param> |
| | 61 | | /// <param name="jobId">The id of the Azure Batch job.</param> |
| | 62 | | /// <remarks>The job output container must already exist; the JobOutputStorage class does not create |
| | 63 | | /// it for you.</remarks> |
| | 64 | | public JobOutputStorage(CloudStorageAccount storageAccount, string jobId) |
| 0 | 65 | | : this(CloudBlobContainerUtils.GetContainerReference(storageAccount, jobId), null) |
| | 66 | | { |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// Initializes a new instance of the <see cref="JobOutputStorage"/> class from a URL representing |
| | 71 | | /// the job output container. |
| | 72 | | /// </summary> |
| | 73 | | /// <param name="jobOutputContainerUri">The URL in Azure storage of the blob container to |
| | 74 | | /// use for job outputs. This URL must contain a SAS (Shared Access Signature) granting |
| | 75 | | /// access to the container, or the container must be public.</param> |
| | 76 | | /// <param name="storageRetryPolicy">The retry policy for storage requests.</param> |
| | 77 | | /// <remarks>The container must already exist; the JobOutputStorage class does not create |
| | 78 | | /// it for you.</remarks> |
| | 79 | | public JobOutputStorage(Uri jobOutputContainerUri, IRetryPolicy storageRetryPolicy) |
| 0 | 80 | | : this(CloudBlobContainerUtils.GetContainerReference(jobOutputContainerUri), storageRetryPolicy) |
| | 81 | | { |
| 0 | 82 | | } |
| | 83 | |
|
| | 84 | | /// <summary> |
| | 85 | | /// Initializes a new instance of the <see cref="JobOutputStorage"/> class from a storage account |
| | 86 | | /// and job id. |
| | 87 | | /// </summary> |
| | 88 | | /// <param name="storageAccount">The storage account linked to the Azure Batch account.</param> |
| | 89 | | /// <param name="jobId">The id of the Azure Batch job.</param> |
| | 90 | | /// <param name="storageRetryPolicy">The retry policy for storage requests.</param> |
| | 91 | | /// <remarks>The job output container must already exist; the JobOutputStorage class does not create |
| | 92 | | /// it for you.</remarks> |
| | 93 | | public JobOutputStorage(CloudStorageAccount storageAccount, string jobId, IRetryPolicy storageRetryPolicy) |
| 0 | 94 | | : this(CloudBlobContainerUtils.GetContainerReference(storageAccount, jobId), storageRetryPolicy) |
| | 95 | | { |
| 0 | 96 | | } |
| | 97 | |
|
| 14 | 98 | | private JobOutputStorage(CloudBlobContainer jobOutputContainer, IRetryPolicy storageRetryPolicy) |
| | 99 | | { |
| 14 | 100 | | if (jobOutputContainer == null) |
| | 101 | | { |
| 0 | 102 | | throw new ArgumentNullException(nameof(jobOutputContainer)); |
| | 103 | | } |
| | 104 | |
|
| 14 | 105 | | if (storageRetryPolicy != null) |
| | 106 | | { |
| 0 | 107 | | jobOutputContainer.ServiceClient.DefaultRequestOptions.RetryPolicy = storageRetryPolicy; |
| | 108 | | } |
| | 109 | |
|
| 14 | 110 | | _storagePath = new StoragePath.JobStoragePath(jobOutputContainer); |
| 14 | 111 | | } |
| | 112 | |
|
| | 113 | | /// <summary> |
| | 114 | | /// Saves the specified file to persistent storage. |
| | 115 | | /// </summary> |
| | 116 | | /// <param name="kind">A <see cref="JobOutputKind"/> representing the category under which to |
| | 117 | | /// store this file, for example <see cref="JobOutputKind.JobOutput"/> or <see cref="JobOutputKind.JobPreview"/> |
| | 118 | | /// <param name="relativePath">The path of the file to save, relative to the current directory. |
| | 119 | | /// If the file is in a subdirectory of the current directory, the relative path will be preserved |
| | 120 | | /// in blob storage.</param> |
| | 121 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 122 | | /// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns> |
| | 123 | | /// <remarks>If the file is outside the current directory, traversals up the directory tree are removed. |
| | 124 | | /// For example, a <paramref name="relativePath"/> of "..\ProcessEnv.cmd" would be treated as "ProcessEnv.cmd" |
| | 125 | | /// for the purposes of creating a blob name.</remarks> |
| | 126 | | /// <exception cref="ArgumentNullException">The <paramref name="kind"/> or <paramref name="relativePath"/> argum |
| | 127 | | /// <exception cref="ArgumentException">The <paramref name="relativePath"/> argument is an absolute path, or is |
| | 128 | | public async Task SaveAsync( |
| | 129 | | JobOutputKind kind, |
| | 130 | | string relativePath, |
| | 131 | | CancellationToken cancellationToken = default(CancellationToken) |
| | 132 | | ) |
| 3 | 133 | | => await SaveAsyncImpl(kind, new DirectoryInfo(Directory.GetCurrentDirectory()), relativePath, cancellationT |
| | 134 | |
|
| | 135 | | internal async Task SaveAsyncImpl( |
| | 136 | | JobOutputKind kind, |
| | 137 | | DirectoryInfo baseFolder, |
| | 138 | | string relativePath, |
| | 139 | | CancellationToken cancellationToken = default(CancellationToken) |
| | 140 | | ) |
| 3 | 141 | | => await _storagePath.SaveAsync(kind, baseFolder, relativePath, cancellationToken); |
| | 142 | |
|
| | 143 | | /// <summary> |
| | 144 | | /// Saves the specified file to persistent storage. |
| | 145 | | /// </summary> |
| | 146 | | /// <param name="kind">A <see cref="JobOutputKind"/> representing the category under which to |
| | 147 | | /// store this file, for example <see cref="JobOutputKind.JobOutput"/> or <see cref="JobOutputKind.JobPreview"/> |
| | 148 | | /// <param name="sourcePath">The path of the file to save.</param> |
| | 149 | | /// <param name="destinationRelativePath">The blob name under which to save the file. This may include a |
| | 150 | | /// relative component, such as "pointclouds/pointcloud_0001.txt".</param> |
| | 151 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 152 | | /// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns> |
| | 153 | | /// <exception cref="ArgumentNullException">The <paramref name="kind"/>, <paramref name="sourcePath"/>, or <para |
| | 154 | | /// <exception cref="ArgumentException">The <paramref name="sourcePath"/> or <paramref name="destinationRelative |
| | 155 | | public async Task SaveAsync( |
| | 156 | | JobOutputKind kind, |
| | 157 | | string sourcePath, |
| | 158 | | string destinationRelativePath, |
| | 159 | | CancellationToken cancellationToken = default(CancellationToken) |
| | 160 | | ) |
| 5 | 161 | | => await _storagePath.SaveAsync(kind, sourcePath, destinationRelativePath, cancellationToken).ConfigureAwait |
| | 162 | |
|
| | 163 | | /// <summary> |
| | 164 | | /// Lists the job outputs of the specified kind. |
| | 165 | | /// </summary> |
| | 166 | | /// <param name="kind">A <see cref="JobOutputKind"/> representing the category of outputs to |
| | 167 | | /// list, for example <see cref="JobOutputKind.JobOutput"/> or <see cref="JobOutputKind.JobPreview"/>.</param> |
| | 168 | | /// <returns>A list of persisted job outputs of the specified kind.</returns> |
| | 169 | | /// <remarks>The list is retrieved lazily from Azure blob storage when it is enumerated.</remarks> |
| | 170 | | public IEnumerable<OutputFileReference> ListOutputs(JobOutputKind kind) |
| 1 | 171 | | => _storagePath.List(kind); |
| | 172 | |
|
| | 173 | | /// <summary> |
| | 174 | | /// Retrieves a job output from Azure blob storage by kind and path. |
| | 175 | | /// </summary> |
| | 176 | | /// <param name="kind">A <see cref="JobOutputKind"/> representing the category of the output to |
| | 177 | | /// retrieve, for example <see cref="JobOutputKind.JobOutput"/> or <see cref="JobOutputKind.JobPreview"/>.</para |
| | 178 | | /// <param name="filePath">The path under which the output was persisted in blob storage.</param> |
| | 179 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 180 | | /// <returns>A reference to the requested file in Azure blob storage.</returns> |
| | 181 | | public async Task<OutputFileReference> GetOutputAsync( |
| | 182 | | JobOutputKind kind, |
| | 183 | | string filePath, |
| | 184 | | CancellationToken cancellationToken = default(CancellationToken) |
| | 185 | | ) |
| 3 | 186 | | => await _storagePath.GetOutputAsync(kind, filePath, cancellationToken).ConfigureAwait(false); |
| | 187 | |
|
| | 188 | | /// <summary> |
| | 189 | | /// Gets the Blob name prefix/folder where files of the given kind are stored |
| | 190 | | /// </summary> |
| | 191 | | /// <param name="kind">The output kind.</param> |
| | 192 | | /// <returns>The Blob name prefix/folder where files of the given kind are stored.</returns> |
| 3 | 193 | | public string GetOutputStoragePath(JobOutputKind kind) => _storagePath.BlobNamePrefix(kind); |
| | 194 | | } |
| | 195 | | } |