Public/New-B2Bucket.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 |
function New-B2Bucket { <# .SYNOPSIS New-B2Bucket will create a new private or public bucket and requires a globally unique name. .DESCRIPTION New-B2Bucket will create a new private or public bucket and requires a globally unique name. An API key is required to use this cmdlet. .EXAMPLE New-B2Bucket -BucketName stoic-barbarian-lemur -BucketType allPublic BucketName BucketID BucketType AccountID ---------- -------- ---------- --------- stoic-barbarian-lemur 4a48fe8875c6214145260818 allPublic 010203040506 The cmdlet above will create a public bucket with the name of stoic-barbarian-lemur. .EXAMPLE PS C:\>New-B2Bucket -BucketName stoic-barbarian-lemur, frisky-navigator-lion -BucketType allPrivate BucketName BucketID BucketType AccountID ---------- -------- ---------- --------- stoic-barbarian-lemur 4a48fe8875c6214145260818 allPrivate 010203040506 frisky-navigator-lion 4a48fe8875c6214145260819 allPrivate 010203040506 The cmdlet above will create a public bucket with the name of stoic-barbarian-lemur and frisky-navigator-lion. .INPUTS System.String This cmdlet takes the AccountID and ApplicationKey as strings. .OUTPUTS PS.B2.Bucket The cmdlet will output a PS.B2.Bucket object holding the bucket info. System.Uri This cmdlet takes the ApiUri as a uri. .LINK https://www.backblaze.com/b2/docs/ .ROLE PS.B2 .FUNCTIONALITY PS.B2 #> [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Low')] [Alias('nb2b')] [OutputType('PS.B2.Bucket')] Param ( # The name of the new B2 bucket. [Parameter(Mandatory=$true, Position=0)] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [ValidateLength(1,50)] [String[]]$BucketName, # What type of bucket, public or private, to create. [Parameter(Mandatory=$true, Position=1)] [ValidateSet('allPublic','allPrivate')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [String]$BucketType, # Used to bypass confirmation prompts. [Parameter(Mandatory=$false, Position=2)] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [Switch]$Force, # The Uri for the B2 Api query. [Parameter(Mandatory=$false, Position=3)] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [Uri]$ApiUri = $script:SavedB2ApiUri, # The authorization token for the B2 account. [Parameter(Mandatory=$false, Position=4)] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [String]$AccountID = $script:SavedB2AccountID, # The authorization token for the B2 account. [Parameter(Mandatory=$false, Position=5)] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [String]$ApiToken = $script:SavedB2ApiToken ) Begin { [Hashtable]$sessionHeaders = @{'Authorization'=$ApiToken} } Process { foreach($bucket in $BucketName) { if($Force -or $PSCmdlet.ShouldProcess($bucket, "Creating new $BucketType bucket.")) { try { [Uri]$b2ApiUri = "$ApiUri/b2api/v1/b2_create_bucket?accountId=$AccountID&bucketName=$bucket&bucketType=$BucketType" $bbInfo = Invoke-RestMethod -Method Get -Uri $b2ApiUri -Headers $sessionHeaders $bbReturnInfo = [PSCustomObject]@{ 'BucketName' = $bbInfo.bucketName 'BucketID' = $bbInfo.bucketId 'BucketType' = $bbInfo.bucketType 'AccountID' = $bbInfo.accountId } # bbReturnInfo is returned after Add-ObjectDetail is processed. Add-ObjectDetail -InputObject $bbReturnInfo -TypeName 'PS.B2.Bucket' } catch { $errorDetail = $_.Exception.Message Write-Error -Exception "Unable to create the new bucket.`n`r$errorDetail" ` -Message "Unable to create the new bucket.`n`r$errorDetail" -Category InvalidOperation } } } } } |