Framework/Helpers/AzHelper.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class AzHelper
{
static [void] UploadStorageBlobContent([string] $fileName, [string] $blobName, [string] $containerName, [object] $stgCtx)
    {
        if([FeatureFlightingManager]::GetFeatureStatus("IsSetAzStorageBlobAvailable","*") -eq $true)
        {
            Set-AzStorageBlobContent -Blob $blobName -Container $containerName -File $fileName -Context $stgCtx -Force | Out-Null
        }
        else {
            $blob = $stgCtx.StorageAccount.CreateCloudBlobClient().GetContainerReference($containerName).GetBlockBlobReference($blobName)
            $task = $blob.UploadFromFileAsync($fileName)
            $task.Wait()
            if (-not ($task.IsCompleted -and !$task.IsFaulted))
            {
                #Need to change write method
                Write-Debug "Transferring file" + $fileName + "to storage has failed!!"
            }
        }
    }

    static [object] GetStorageBlobContent([string] $folderName, [string] $fileName, [string] $blobName, [string] $containerName, [object] $stgCtx)
    {
        $folderName = $folderName.TrimEnd("\")
        $folderName = $folderName.TrimEnd("/")
        $fileName = Join-Path $folderName $fileName
        return [AzHelper]::GetStorageBlobContent($fileName, $blobName, $containerName, $stgCtx)
    }

    static [object] GetStorageBlobContent([string] $fileName, [string] $blobName, [string] $containerName, [object] $stgCtx)
    {
        if([FeatureFlightingManager]::GetFeatureStatus("IsSetAzStorageBlobAvailable","*") -eq $true)
        {
            $result = Get-AzStorageBlobContent -Blob $blobName -Container $containerName -Destination $fileName -Context $stgCtx -Force 
        }
        else {
            $blob = Get-AzStorageBlob -Container $containerName -Blob $blobName -Context $stgCtx
            $task = $blob.ICloudBlob.DownloadToFileAsync($fileName,[System.IO.FileMode]::Create)
            $task.Wait()
            if (-not ($task.IsCompleted -and !$task.IsFaulted))
            {
                #Need to change write method
                Write-Debug "Downloading file from" + $blobName + " has failed!!"
            }
            $result = $blob 
        }
        return $result
    }
}