< Summary

Class:Microsoft.Azure.HDInsight.Job.Models.AzureStorageAccess
Assembly:Microsoft.Azure.HDInsight.Job
File(s):C:\Git\azure-sdk-for-net\sdk\hdinsight\Microsoft.Azure.HDInsight.Job\src\Customizations\Models\AzureStorageAccess.cs
Covered lines:6
Uncovered lines:23
Coverable lines:29
Total lines:109
Line coverage:20.6% (6 of 29)
Covered branches:0
Total branches:4
Branch coverage:0% (0 of 4)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_StorageAccountName()-0%100%
get_StorageAccountKey()-0%100%
get_DefaultStorageContainer()-0%100%
get_storageAccountSuffix()-0%100%
.ctor(...)-100%100%
GetStorageClient()-0%0%
GetBlobReference(...)-0%100%
GetFileContent(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\hdinsight\Microsoft.Azure.HDInsight.Job\src\Customizations\Models\AzureStorageAccess.cs

#LineLine coverage
 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
 5namespace 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    {
 019        private string StorageAccountName { get; set; }
 20
 021        private string StorageAccountKey { get; set; }
 22
 023        private string DefaultStorageContainer { get; set; }
 24
 025        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
 2642        public AzureStorageAccess(string storageAccountName, string storageAccountKey, string defaultStorageContainer, s
 43        {
 2644            this.StorageAccountName = storageAccountName;
 2645            this.StorageAccountKey = storageAccountKey;
 2646            this.DefaultStorageContainer = defaultStorageContainer;
 2647            this.storageAccountSuffix = storageAccountSuffix;
 2648        }
 49
 50        private CloudBlobClient GetStorageClient()
 51        {
 052            var accountName = StorageAccountName.Contains(".") ? StorageAccountName.Substring(0, StorageAccountName.Inde
 053            var storageCredentials = new StorageCredentials(accountName, StorageAccountKey);
 054            var storageAccount = new CloudStorageAccount(storageCredentials, storageAccountSuffix, true);
 055            return storageAccount.CreateCloudBlobClient();
 56        }
 57
 58        private ICloudBlob GetBlobReference(string defaultContainer, string blobReferencePath)
 59        {
 60            ICloudBlob blobReference;
 61            try
 62            {
 063                var client = GetStorageClient();
 064                var container = client.GetContainerReference(defaultContainer);
 065                blobReference = container.GetBlobReferenceFromServerAsync(blobReferencePath).GetAwaiter().GetResult();
 066            }
 67            catch (Exception blobNotFoundException)
 68            {
 069                throw new CloudException(blobNotFoundException.Message);
 70            }
 71
 072            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        {
 086            if (string.IsNullOrEmpty(blobReferencePath))
 87            {
 088                return null;
 89            }
 90
 091            var blobReference = GetBlobReference(DefaultStorageContainer, blobReferencePath);
 92
 093            var blobStream = new MemoryStream();
 94
 95            try
 96            {
 097                blobReference.DownloadToStreamAsync(blobStream).Wait();
 098            }
 99            catch (Exception blobWebException)
 100            {
 0101                throw new CloudException(blobWebException.Message);
 102            }
 103
 0104            blobStream.Seek(0, SeekOrigin.Begin);
 105
 0106            return blobStream;
 107        }
 108    }
 109}