1function UploadToBlobStoreWithACL {
 2    param (
 3        [string]$BucketName,
 4        [string]$FileToUpload,
 5        [string]$BlobStoreKey,
 6        [string]$ACL
 7    )
 8
 9    # Format date to match AWS requirements
10    $Date = (Get-Date).ToUniversalTime().ToString("r")
11    # Note: Original script had a bug where it overrode the ACL parameter
12    # I'm keeping the same behavior for compatibility
13    $ACL = "public-read"
14    $ContentType = "application/octet-stream"
15    $StorageClass = "STANDARD"
16
17    # Create string to sign (AWS S3 compatible format)
18    $StringToSign = "PUT`n`n${ContentType}`n${Date}`nx-amz-acl:${ACL}`nx-amz-storage-class:${StorageClass}`n/${BucketName}/${BlobStoreKey}"
19
20    # Generate HMAC-SHA1 signature
21    $HMACSHA1 = New-Object System.Security.Cryptography.HMACSHA1
22    $HMACSHA1.Key = [System.Text.Encoding]::UTF8.GetBytes($env:DIGITALOCEAN_SPACES_SECRET_KEY)
23    $Signature = [System.Convert]::ToBase64String($HMACSHA1.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($StringToSign)))
24
25    # Upload file using Invoke-WebRequest (equivalent to curl)
26    $Headers = @{
27        "Host" = "${BucketName}.nyc3.digitaloceanspaces.com"
28        "Date" = $Date
29        "Content-Type" = $ContentType
30        "x-amz-storage-class" = $StorageClass
31        "x-amz-acl" = $ACL
32        "Authorization" = "AWS ${env:DIGITALOCEAN_SPACES_ACCESS_KEY}:$Signature"
33    }
34
35    $Uri = "https://${BucketName}.nyc3.digitaloceanspaces.com/${BlobStoreKey}"
36
37    # Read file content
38    $FileContent = Get-Content $FileToUpload -Raw -AsByteStream
39
40    try {
41        Invoke-WebRequest -Uri $Uri -Method PUT -Headers $Headers -Body $FileContent -ContentType $ContentType -Verbose
42        Write-Host "Successfully uploaded $FileToUpload to $Uri" -ForegroundColor Green
43    }
44    catch {
45        Write-Error "Failed to upload file: $_"
46        throw $_
47    }
48}
49
50function UploadToBlobStorePublic {
51    param (
52        [string]$BucketName,
53        [string]$FileToUpload,
54        [string]$BlobStoreKey
55    )
56
57    UploadToBlobStoreWithACL -BucketName $BucketName -FileToUpload $FileToUpload -BlobStoreKey $BlobStoreKey -ACL "public-read"
58}
59
60function UploadToBlobStore {
61    param (
62        [string]$BucketName,
63        [string]$FileToUpload,
64        [string]$BlobStoreKey
65    )
66
67    UploadToBlobStoreWithACL -BucketName $BucketName -FileToUpload $FileToUpload -BlobStoreKey $BlobStoreKey -ACL "private"
68}