| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | namespace Microsoft.Azure.HDInsight.Job.Models |
| | 6 | | { |
| | 7 | | using System; |
| | 8 | | using System.IO; |
| | 9 | | using Microsoft.Rest.Azure; |
| | 10 | | using Microsoft.WindowsAzure.Storage; |
| | 11 | | using Microsoft.WindowsAzure.Storage.Auth; |
| | 12 | | using Microsoft.WindowsAzure.Storage.Blob; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Manages storage access details for job operations against HDInsight clusters. |
| | 16 | | /// </summary> |
| | 17 | | public class AzureStorageAccess : IStorageAccess |
| | 18 | | { |
| 0 | 19 | | private string StorageAccountName { get; set; } |
| | 20 | |
|
| 0 | 21 | | private string StorageAccountKey { get; set; } |
| | 22 | |
|
| 0 | 23 | | private string DefaultStorageContainer { get; set; } |
| | 24 | |
|
| 0 | 25 | | private string storageAccountSuffix { get; set; } |
| | 26 | | /// <summary> |
| | 27 | | /// Initializes a new instance of the AzureStorageAccess class. |
| | 28 | | /// </summary> |
| | 29 | | /// <param name='storageAccountName'> |
| | 30 | | /// Required. The storage account name. |
| | 31 | | /// </param> |
| | 32 | | /// <param name='storageAccountKey'> |
| | 33 | | /// Required. The storage account key. |
| | 34 | | /// </param> |
| | 35 | | /// <param name='defaultStorageContainer'> |
| | 36 | | /// Required. The default storage container name. |
| | 37 | | /// </param> |
| | 38 | | /// <param name='storageAccountSuffix'> |
| | 39 | | /// Optional. The storage account URI suffix. For example, "core.chinacloudapi.cn". |
| | 40 | | /// </param> |
| | 41 | |
|
| 26 | 42 | | public AzureStorageAccess(string storageAccountName, string storageAccountKey, string defaultStorageContainer, s |
| | 43 | | { |
| 26 | 44 | | this.StorageAccountName = storageAccountName; |
| 26 | 45 | | this.StorageAccountKey = storageAccountKey; |
| 26 | 46 | | this.DefaultStorageContainer = defaultStorageContainer; |
| 26 | 47 | | this.storageAccountSuffix = storageAccountSuffix; |
| 26 | 48 | | } |
| | 49 | |
|
| | 50 | | private CloudBlobClient GetStorageClient() |
| | 51 | | { |
| 0 | 52 | | var accountName = StorageAccountName.Contains(".") ? StorageAccountName.Substring(0, StorageAccountName.Inde |
| 0 | 53 | | var storageCredentials = new StorageCredentials(accountName, StorageAccountKey); |
| 0 | 54 | | var storageAccount = new CloudStorageAccount(storageCredentials, storageAccountSuffix, true); |
| 0 | 55 | | return storageAccount.CreateCloudBlobClient(); |
| | 56 | | } |
| | 57 | |
|
| | 58 | | private ICloudBlob GetBlobReference(string defaultContainer, string blobReferencePath) |
| | 59 | | { |
| | 60 | | ICloudBlob blobReference; |
| | 61 | | try |
| | 62 | | { |
| 0 | 63 | | var client = GetStorageClient(); |
| 0 | 64 | | var container = client.GetContainerReference(defaultContainer); |
| 0 | 65 | | blobReference = container.GetBlobReferenceFromServerAsync(blobReferencePath).GetAwaiter().GetResult(); |
| 0 | 66 | | } |
| | 67 | | catch (Exception blobNotFoundException) |
| | 68 | | { |
| 0 | 69 | | throw new CloudException(blobNotFoundException.Message); |
| | 70 | | } |
| | 71 | |
|
| 0 | 72 | | return blobReference; |
| | 73 | | } |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Gets the file content from blob reference path. |
| | 77 | | /// </summary> |
| | 78 | | /// <param name='blobReferencePath'> |
| | 79 | | /// Required. Blob reference path for which url to be generated. |
| | 80 | | /// </param> |
| | 81 | | /// <returns> |
| | 82 | | /// Content of the file for the input blob reference path. |
| | 83 | | /// </returns> |
| | 84 | | public Stream GetFileContent(string blobReferencePath) |
| | 85 | | { |
| 0 | 86 | | if (string.IsNullOrEmpty(blobReferencePath)) |
| | 87 | | { |
| 0 | 88 | | return null; |
| | 89 | | } |
| | 90 | |
|
| 0 | 91 | | var blobReference = GetBlobReference(DefaultStorageContainer, blobReferencePath); |
| | 92 | |
|
| 0 | 93 | | var blobStream = new MemoryStream(); |
| | 94 | |
|
| | 95 | | try |
| | 96 | | { |
| 0 | 97 | | blobReference.DownloadToStreamAsync(blobStream).Wait(); |
| 0 | 98 | | } |
| | 99 | | catch (Exception blobWebException) |
| | 100 | | { |
| 0 | 101 | | throw new CloudException(blobWebException.Message); |
| | 102 | | } |
| | 103 | |
|
| 0 | 104 | | blobStream.Seek(0, SeekOrigin.Begin); |
| | 105 | |
|
| 0 | 106 | | return blobStream; |
| | 107 | | } |
| | 108 | | } |
| | 109 | | } |