Private/S3/Get-WorkspaceBucket.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
function Get-WorkspaceBucket
{
    <#
    .SYNOPSIS
        Gets, creating if necessary, a bucket for use by tools in this module

    .OUTPUTS
        [PSObject] with the following fields
        - BucketName
        - BucketUrl
#>

    $bucketName = "aws-toolbox-workspace-$(Get-CurrentRegion)-$((Get-STSCallerIdentity).Account)"

    try
    {
        $location = Get-BucketLocation -BucketName $bucketName

        Write-WorkspaceBucketTagging -BucketName $bucketName

        return New-Object psobject -Property @{
            BucketName = $bucketName
            BucketUrl  = [uri]"https://s3.$($location).amazonaws.com/$bucketName"
        }
    }
    catch
    {
        # Bucket not found
    }

    # Try to create it
    $response = New-S3Bucket -BucketName $bucketName

    if ($response)
    {
        Write-WorkspaceBucketTagging -BucketName $bucketName

        $location = Get-BucketLocation -BucketName $bucketName

        return New-Object psobject -Property @{
            BucketName = $bucketName
            BucketUrl  = [uri]"https://s3.$($location).amazonaws.com/$bucketName"
        }
    }

    throw "Unable to create S3 bucket $bucketName"
}