| | 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.WindowsAzure.Storage.Blob; |
| | 16 | | using System; |
| | 17 | | using System.Collections.Generic; |
| | 18 | | using System.IO; |
| | 19 | | using System.Linq; |
| | 20 | | using System.Text; |
| | 21 | | using System.Threading; |
| | 22 | | using System.Threading.Tasks; |
| | 23 | |
|
| | 24 | | namespace Microsoft.Azure.Batch.Conventions.Files |
| | 25 | | { |
| | 26 | | /// <summary> |
| | 27 | | /// A reference to a task or job output file in persistent storage. |
| | 28 | | /// </summary> |
| | 29 | | public sealed class OutputFileReference |
| | 30 | | { |
| | 31 | | internal OutputFileReference(IListBlobItem blob) |
| 0 | 32 | | : this((ICloudBlob)blob) |
| | 33 | | { |
| 0 | 34 | | } |
| | 35 | |
|
| 10 | 36 | | internal OutputFileReference(ICloudBlob blob) |
| | 37 | | { |
| 10 | 38 | | CloudBlob = blob; |
| 10 | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Gets the path under which the file was stored. |
| | 43 | | /// </summary> |
| | 44 | | /// <remarks>If the file was stored under a directory path, the FilePath property uses the directory |
| | 45 | | /// separator of the underlying blob storage (for example, mydirectory/myfile.txt). Such paths may |
| | 46 | | /// not be directly usable as Windows paths.</remarks> |
| | 47 | | public string FilePath |
| | 48 | | { |
| | 49 | | get |
| | 50 | | { |
| 3 | 51 | | var storagePath = Uri.AbsolutePath; // /container/$kind/path or /container/taskid/$kind/path |
| 3 | 52 | | var pathFromContainer = storagePath.Substring(storagePath.IndexOf('/', 1) + 1); |
| 3 | 53 | | var kindAndRelativePath = pathFromContainer.Substring(pathFromContainer.IndexOf('$')); |
| 3 | 54 | | var relativePath = kindAndRelativePath.Substring(kindAndRelativePath.IndexOf('/') + 1); |
| 3 | 55 | | return relativePath; |
| | 56 | | } |
| | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Gets the URI of the file in persistent storage. |
| | 61 | | /// </summary> |
| 4 | 62 | | public Uri Uri => CloudBlob.Uri; |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Gets a reference to the underlying <see cref="ICloudBlob"/> object representing the |
| | 66 | | /// file in persistent storage. This can be used to invoke blob methods or overloads not surfaced |
| | 67 | | /// by the <see cref="OutputFileReference"/> abstraction. |
| | 68 | | /// </summary> |
| 10 | 69 | | public ICloudBlob CloudBlob { get; } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Downloads the contents of the file to a specified path. |
| | 73 | | /// </summary> |
| | 74 | | /// <param name="path">The path to which to download the file.</param> |
| | 75 | | /// <param name="mode">Specifies how to open or create the file.</param> |
| | 76 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 77 | | /// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns> |
| | 78 | | /// <seealso cref="ICloudBlob.DownloadToFileAsync(string, FileMode)"/> |
| | 79 | | public async Task DownloadToFileAsync(string path, FileMode mode, CancellationToken cancellationToken = default( |
| | 80 | | { |
| | 81 | | #if FullNetFx |
| | 82 | | await CloudBlob.DownloadToFileAsync(path, mode, cancellationToken).ConfigureAwait(false); |
| | 83 | | #else |
| 1 | 84 | | await CloudBlob.DownloadToFileAsync(path, mode).ConfigureAwait(false); |
| | 85 | | #endif |
| | 86 | |
|
| 1 | 87 | | } |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// Downloads the contents of the file to a stream. |
| | 91 | | /// </summary> |
| | 92 | | /// <param name="target">The target stream.</param> |
| | 93 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 94 | | /// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns> |
| | 95 | | /// <seealso cref="ICloudBlob.DownloadToStreamAsync(Stream)"/> |
| | 96 | | public async Task DownloadToStreamAsync(Stream target, CancellationToken cancellationToken = default(Cancellatio |
| | 97 | | { |
| | 98 | | #if FullNetFx |
| | 99 | | await CloudBlob.DownloadToStreamAsync(target, cancellationToken).ConfigureAwait(false); |
| | 100 | | #else |
| 1 | 101 | | await CloudBlob.DownloadToStreamAsync(target).ConfigureAwait(false); |
| | 102 | | #endif |
| 1 | 103 | | } |
| | 104 | |
|
| | 105 | | /// <summary> |
| | 106 | | /// Downloads the contents of the file to a byte array. |
| | 107 | | /// </summary> |
| | 108 | | /// <param name="target">The target byte array.</param> |
| | 109 | | /// <param name="index">The starting offset in the byte array</param> |
| | 110 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 111 | | /// <returns>The total number of bytes read into the buffer.</returns> |
| | 112 | | /// <seealso cref="ICloudBlob.DownloadToByteArrayAsync(byte[], int)"/> |
| | 113 | | public async Task<int> DownloadToByteArrayAsync(byte[] target, int index, CancellationToken cancellationToken = |
| | 114 | | { |
| | 115 | | #if FullNetFx |
| | 116 | | return await CloudBlob.DownloadToByteArrayAsync(target, index, cancellationToken).ConfigureAwait(false); |
| | 117 | | #else |
| 1 | 118 | | return await CloudBlob.DownloadToByteArrayAsync(target, index).ConfigureAwait(false); |
| | 119 | | #endif |
| 1 | 120 | | } |
| | 121 | |
|
| | 122 | | /// <summary> |
| | 123 | | /// Deletes the file from persistent storage. |
| | 124 | | /// </summary> |
| | 125 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 126 | | /// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns> |
| | 127 | | /// <seealso cref="ICloudBlob.DeleteAsync()"/> |
| | 128 | | public async Task DeleteAsync(CancellationToken cancellationToken = default(CancellationToken)) |
| | 129 | | { |
| | 130 | | #if FullNetFx |
| | 131 | | await CloudBlob.DeleteAsync(cancellationToken).ConfigureAwait(false); |
| | 132 | | #else |
| 1 | 133 | | await CloudBlob.DeleteAsync().ConfigureAwait(false); |
| | 134 | | #endif |
| 1 | 135 | | } |
| | 136 | |
|
| | 137 | | /// <summary> |
| | 138 | | /// Opens a stream for reading from the file in persistent storage. |
| | 139 | | /// </summary> |
| | 140 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 141 | | /// <returns>A stream to be used for reading from the blob.</returns> |
| | 142 | | /// <seealso cref="ICloudBlob.OpenReadAsync(WindowsAzure.Storage.AccessCondition, BlobRequestOptions, WindowsAzu |
| | 143 | | public async Task<Stream> OpenReadAsync(CancellationToken cancellationToken = default(CancellationToken)) |
| | 144 | | { |
| | 145 | | #if FullNetFx |
| | 146 | | return await CloudBlob.OpenReadAsync(cancellationToken).ConfigureAwait(false); |
| | 147 | | #else |
| 1 | 148 | | return await CloudBlob.OpenReadAsync(null, null, null).ConfigureAwait(false); |
| | 149 | | #endif |
| 1 | 150 | | } |
| | 151 | | } |
| | 152 | | } |