Created
February 29, 2016 01:52
-
-
Save mekuls/42830051b6a31e8eee53 to your computer and use it in GitHub Desktop.
Gist accompanying http://blog.skycoderlabs.com/2016/03/01/serving-websites-from-aws-s3/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module AWSPowershell | |
$access = "AAAAAAAAABBBBBBCCCCCCCCC" | |
$private = "NNNNNNNNN+OOOOOOOOPPPPPPPPP" | |
$bucketName = "mybucket.org" | |
Set-AWSCredentials -AccessKey $access -SecretKey $private | |
# It is imperative that the path end in a backslash | |
$siteRoot = "C:\path\to\jekyll\_site" | |
if ($siteRoot.EndsWith('\')) { | |
$siteRoot += "\" | |
} | |
# Get the full path of each of the files in the site | |
$siteRootPaths = Get-ChildItem $siteRoot -Recurse | | |
Where { $_.PsIsContainer -eq $false } | | |
Select -ExpandProperty FullName | |
$filesToUpload = @() # This will store the final uplaod definitions. | |
foreach ($path in $siteRootPaths) { | |
$o = New-Object PSObject | |
$o | Add-Member -MemberType NoteProperty -Name Source -Value $path | |
$keyPath = $path.Replace($siteRoot, "") # Make relative Path | |
$keyPath = $keyPath.Replace("\", "/") # Invert slashes | |
$o | Add-Member -MemberType NoteProperty -Name KeyPath -Value $keyPath | |
$filesToUpload += $o | |
} | |
$keyName = "/" # The root of the site. | |
$existingS3Objects = @(Get-S3Object -BucketName $bucketName -Key $keyName) | |
# Clobber all of the files in the bucket. | |
if ($existingS3Objects -ine $null) { | |
Write-Output "Deleting $($existingS3Objects.Count) objects from bucket $bucketName" | |
foreach ($o in $existingS3Objects) { | |
Write-Output "Deleting File: $($o.Key)" | |
if ($o -ine $null) { | |
Remove-S3Object -BucketName $bucketName -Key $o.Key -Force | Out-Null | |
} | |
} | |
} | |
# Write all files to the bucket. | |
Write-Output "Writing $($filesToUpload.Count) items to bucket $bucketName" | |
foreach ($item in $filesToUpload) { | |
Write-Output "Writing Object: $($item.KeyPath)" | |
Write-S3Object -File $item.Source -BucketName $bucketName -Key $item.KeyPath | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment