public void CanSaveDataFactoryRunLog()
        {
            Uri sharedAccessSignature =
                new Uri(@"https://fakeaccount.blob.core.windows.net/sascontainer?sv=2012-02-12");
            PSRunLogInfo runLogInfo = new PSRunLogInfo(sharedAccessSignature);
            Assert.Equal("?sv=2012-02-12", runLogInfo.SasToken);
            Assert.Equal(@"https://fakeaccount.blob.core.windows.net/sascontainer?sv=2012-02-12", runLogInfo.SasUri);
            Assert.Equal("fakeaccount", runLogInfo.StorageAccountName);
            Assert.Equal("sascontainer", runLogInfo.Container);

            dataFactoriesClientMock.Setup(
                f =>
                    f.CreatePSDataFactory(
                        It.Is<CreatePSDataFactoryParameters>(
                            parameters =>
                                parameters.ResourceGroupName == ResourceGroupName &&
                                parameters.DataFactoryName == DataFactoryName &&
                                parameters.Location == Location)))
                .CallBase()
                .Verifiable();

            // Arrange
            this.dataFactoriesClientMock.Setup(
                f =>
                f.GetDataSliceRunLogsSharedAccessSignature(ResourceGroupName, DataFactoryName, this._dataSliceRunId))
                .Returns(runLogInfo);

            this.dataFactoriesClientMock.Setup(
                f =>
                f.DownloadFileToBlob(
                    It.Is<BlobDownloadParameters>(
                            parameters =>
                                parameters.Credentials == new StorageCredentials(runLogInfo.SasToken) &&
                                parameters.SasUri == new Uri(runLogInfo.SasUri) &&
                                parameters.Directory == @"c:\")));

            // Action
            this._cmdlet.ExecuteCmdlet();

            // Assert
            this.dataFactoriesClientMock.Verify(
                f =>
                f.GetDataSliceRunLogsSharedAccessSignature(ResourceGroupName, DataFactoryName, this._dataSliceRunId),
                Times.Once());

            this.commandRuntimeMock.Verify(f => f.WriteObject(runLogInfo), Times.Once());
        }
        public virtual void DownloadFileToBlob(BlobDownloadParameters parameters)
        {
            if (parameters == null || string.IsNullOrWhiteSpace(parameters.SasUri.ToString()))
            {
                throw new ArgumentNullException(Resources.DownloadCredentialsNull);
            }

            PSRunLogInfo rli = new PSRunLogInfo(parameters.SasUri);
            StorageCredentials sc = new StorageCredentials(rli.SasToken);

            StorageUri suri = new StorageUri(new Uri("https://" + parameters.SasUri.Host));
            CloudBlobClient sourceClient = new CloudBlobClient(suri, sc);
            CloudBlobContainer sascontainer = sourceClient.GetContainerReference(rli.Container);
            CloudBlobDirectory sourceDirectory = sascontainer.GetDirectoryReference(rli.Directory);

            var bloblist = sourceDirectory.ListBlobs(true);
            string downloadFolderPath = parameters.Directory.Insert(parameters.Directory.Length, @"\");

            foreach (var blob in bloblist)
            {
                ICloudBlob destBlob = blob as ICloudBlob;
                int length = destBlob.Name.Split('/').Length;
                string blobFileName = destBlob.Name.Split('/')[length - 1];
                // the folder structure of run logs changed from flat listing to nesting under time directory
                string blobFolderPath = String.Empty;
                if (destBlob.Name.Length > blobFileName.Length)
                {
                    blobFolderPath = destBlob.Name.Substring(0, destBlob.Name.Length - blobFileName.Length - 1);

                    if (!string.IsNullOrEmpty(rli.Directory))
                    {
                        blobFolderPath = blobFolderPath.Remove(0, rli.Directory.Length - 1).Trim('/');
                    }
                }

                if (!Directory.Exists(downloadFolderPath + blobFolderPath))
                {
                    Directory.CreateDirectory(downloadFolderPath + blobFolderPath);
                }
                // adding _log suffix to differentiate between files and folders of the same name. Azure blob storage only knows about blob files. We could use nested folder structure
                // as part of the blob file name and thus it is possible to have a file and folder of the same name in the same location which is not acceptable for Windows file system
                string fileToDonwload = destBlob.Name.Remove(0, rli.Directory.Length);
                destBlob.DownloadToFile(downloadFolderPath + fileToDonwload + "_log", FileMode.Create);
            }
        }