Created
April 28, 2018 15:29
-
-
Save andrewthauer/ec2084bb79767bbd59d8ffb4b19e1d5b to your computer and use it in GitHub Desktop.
Copy Files to S3
This file contains hidden or 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
# PushTo-S3 script | |
# | |
# Description: | |
# Copies the source files to an S3 bucket | |
# | |
# Usage: | |
# PushTo-S3 -SourcePath [Path] -AwsAccessKey [KEY] -AwsSecretKey [SECRET] -AwsRegion [REGION] -S3BucketName [BUCKET] | |
# | |
# Example: | |
# ./PushTo-S3 -SourcePath "./dist" -AwsAccessKey "KEY" -AwsSecretKey "SECRET" -AwsRegion "REGION" -S3BucketName "BUCKET" | |
# | |
param( | |
[String]$sourcePath, | |
[String]$awsAccessKey, | |
[String]$awsSecretKey, | |
[String]$awsRegion, | |
[String]$s3BucketName | |
) | |
function LoadAwsModule() { | |
try { | |
Write-Output "Attempting To Load AWSPowerShell Module ..." | |
Import-Module AWSPowerShell -ErrorAction Stop | |
return | |
} catch {} | |
try { | |
Write-Output "Trying To Load AWSPowerShell.NetCore Module ..." | |
Import-Module AWSPowerShell.NetCore -ErrorAction Stop | |
return | |
} catch {} | |
try { | |
$modulePath = "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1" | |
Write-Output "Trying to Load AWSPowerShell Module: $modulePath" | |
Import-Module $modulePath | |
Write-Output "Found AWSPowerShell Module: $modulePath" | |
return | |
} catch {} | |
throw "AWS PowerShell not found! Please make sure to install them from https://aws.amazon.com/powershell/" | |
} | |
function PushToS3($path) | |
{ | |
# Additional s3 params | |
$params = @{} | |
$params.add("CannedACLName", "public-read") | |
# Log info | |
Write-Output "Pushing to '$path' S3 -> '$s3bucketName'" | |
# Gets all files and child folders within the given directory | |
foreach ($item in Get-ChildItem $path) { | |
# Checks if the item is a folder | |
if ($item -is [System.IO.DirectoryInfo]) { | |
Write-Output $item.FullName | |
# Inserts all files within a folder to AWS | |
Write-S3Object -AccessKey $awsAccessKey -SecretKey $awsSecretKey -Region $awsRegion -BucketName $s3bucketName -KeyPrefix $item.Name -Folder $item.FullName -Recurse @params | |
} else { | |
# Inserts file to AWS | |
Write-S3Object -AccessKey $awsAccessKey -SecretKey $awsSecretKey -Region $awsRegion -BucketName $s3bucketName -Key $item.Name -File $item.FullName @params | |
} | |
} | |
} | |
# Load the AWS PowerShell module | |
LoadAwsModule | |
# Push to S3 | |
PushToS3($sourcePath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment