Public/Invoke-B2ItemDownload.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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
function Invoke-B2ItemDownload { <# .SYNOPSIS The Invoke-B2ItemDownload cmdlet downloads files by either the file ID or file name. .DESCRIPTION The Invoke-B2ItemDownload cmdlet downloads files by either the file ID or file name. When downloading by the file name the bucket ID the file resides in must be specified. An API key is reuqired to use this cmdlet. .EXAMPLE Invoke-B2ItemDownload -FileID 4_h4a48fe8875c6214145260818_f000000000000472a_d20140104_m032022_c001_v0000123_t0104 -OutFile C:\hello.txt The cmdlet above will download the file with the given ID and place it at the root of the C: drive with the name hello.txt. .EXAMPLE PS C:\>Invoke-B2ItemDownload -FileName hello.txt -BucketName text -OutFile C:\hello.txt The cmdlet above will download the file with the given name from the given bucket name and place it at the root of the C: drive with the name hello.txt. .EXAMPLE PS C:\>Get-B2Bucket | Get-B2ChildItem | ForEach-Object {Invoke-B2ItemDownload -FileID $_.ID -OutFile .\$_.Name} The cmdlet above will download the first 1000 files in all buckets and place them in the current directory. .INPUTS System.String This cmdlet takes the FileID, FileName, BucketName, OutFile, and ApiToken as strings. System.Uri This cmdlet takes the ApiUri as a uri. .OUTPUTS None The cmdlet has no outputs. .LINK https://www.backblaze.com/b2/docs/ .ROLE PS.B2 .FUNCTIONALITY PS.B2 #> [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Low')] [Alias('ib2id')] [OutputType()] Param ( # The Uri for the B2 Api query. [Parameter(ParameterSetName='ID', Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [String]$ID, # The Uri for the B2 Api query. [Parameter(ParameterSetName='Name', Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [String]$Name, # The Uri for the B2 Api query. [Parameter(ParameterSetName='Name', Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=1)] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [String]$BucketName, # The Uri for the B2 Api query. [Parameter(ParameterSetName='Name', Mandatory=$true, Position=3)] [Parameter(ParameterSetName='ID', Mandatory=$true, Position=1)] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [String]$OutFile, # The Uri for the B2 Api query. [Parameter(Mandatory=$false,ParameterSetName='Name')] [Parameter(Mandatory=$false,ParameterSetName='ID')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [Uri]$ApiDownloadUri = $script:SavedB2DownloadUri, # The authorization token for the B2 account. [Parameter(Mandatory=$false,ParameterSetName='Name')] [Parameter(Mandatory=$false,ParameterSetName='ID')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [String]$ApiToken = $script:SavedB2ApiToken ) Begin { if(-not (Test-Path -Path $OutFile -IsValid)) { throw 'The file path given is not valid.`n`rThe file cannot be saved.' } [Hashtable]$sessionHeaders = @{'Authorization'=$ApiToken} } Process { # The process context will change based on the name of the paramter set used. switch($PSCmdlet.ParameterSetName) { 'ID' { [Uri]$b2ApiUri = "${ApiDownloadUri}b2api/v1/b2_download_file_by_id?fileId=$ID" if($PSCmdlet.ShouldProcess($ID, "Download to the path $OutFile.")) { try { Invoke-RestMethod -Method Get -Uri $b2ApiUri -Headers $sessionHeaders -OutFile $OutFile } catch { $errorDetail = $_.Exception.Message Write-Error -Exception "Unable to upload the file.`n`r$errorDetail" ` -Message "Unable to upload the file.`n`r$errorDetail" -Category InvalidOperation } } } 'Name' { [Uri]$b2ApiUri = "${ApiDownloadUri}file/$BucketName/$Name" if($PSCmdlet.ShouldProcess($Name, "Download to the path $OutFile.")) { try { Invoke-RestMethod -Method Get -Uri $b2ApiUri -Headers $sessionHeaders -OutFile $OutFile } catch { $errorDetail = $_.Exception.Message Write-Error -Exception "Unable to upload the file.`n`r$errorDetail" ` -Message "Unable to upload the file.`n`r$errorDetail" -Category InvalidOperation } } } } } } |