Skip to content

Instantly share code, notes, and snippets.

@heathen1878
Forked from rhysgodfrey/copy-all-files-from-s3.ps1
Created December 7, 2018 17:14
Show Gist options
  • Select an option

  • Save heathen1878/54734e4259d26405508024f4da71b427 to your computer and use it in GitHub Desktop.

Select an option

Save heathen1878/54734e4259d26405508024f4da71b427 to your computer and use it in GitHub Desktop.
Function Get-Objectsfroms3 {
Param
(
# Your account access key - must have read access to your S3 Bucket
[Parameter(Mandatory=$true,Position=1)]
[string]
$accessKey,
# Your account secret access key
[Parameter(Mandatory=$true,Position=2)]
[string]
$secretKey,
# The region associated with your bucket e.g. eu-west-1, us-east-1 etc. (see http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html#concepts-regions)
[Parameter(Mandatory=$true,Position=2)]
[string]
$region,
# The name of your S3 Bucket
[Parameter(Mandatory=$true,Position=2)]
[string]
$bucket,
# The folder in your bucket to copy, including trailing slash. Leave blank to copy the entire bucket
[Parameter(Mandatory=$false,Position=2)]
[string]
$keyprefix,
# The local file path where files should be copied
[Parameter(Mandatory=$true,Position=2)]
[string]
$localPath
)
# Install dependencies
Install-Module -Name AWSPowerShell -Scope CurrentUser -Force
# Get a list of objects in the bucket
If ($keyPrefix){
$objects = Get-S3Object -BucketName $bucket -KeyPrefix $keyPrefix -AccessKey $accessKey -SecretKey $secretKey -Region $region
}
Else {
$objects = Get-S3Object -BucketName $bucket -AccessKey $accessKey -SecretKey $secretKey -Region $region
}
$objects | Foreach-object {
If ($keyPrefix) {
$localFileName = $_.Key.Replace($keyPrefix,'')
}
Else {
$localFileName = $_.Key
Write-Verbose $localFileName -Verbose
}
If ($localFileName -ne '') {
$localFilePath = (-join($localPath,"\",$localFileName))
Write-Verbose "downloading $($_.Key) from $bucket" -Verbose
Write-Verbose "to $($localFilePath)" -Verbose
Copy-S3Object -BucketName $bucket -Key $_.Key -LocalFile $localFilePath -AccessKey $accessKey -SecretKey $secretKey -Region $region
}
}
}
Export-ModuleMember -Function Get-Objectsfroms3
# Copy modules.psm1 to Windows modules directory.
[string]$userPSModulePath = ($env:PSModulePath).Split(";")[0]
Write-Verbose "Copying Get-ObjectsFromS3.psm1 from $PSScriptRoot" -Verbose
# Create PowerShell modules directory if it doesn't exist.
If (!(Test-Path $userPSModulePath)){
New-Item -Path $userPSModulePath -ItemType Directory -Force
}
# Create the Windows Deployment modules directory if it doesn't exist.
If (!(Test-Path(-join($userPSModulePath,"\GetObjectsFromS3")))){
New-Item -Path $userPSModulePath -ItemType Directory -Force -Name "GetObjectsFromS3"
}
Copy-Item (-join($PSScriptRoot,"\GetObjectsFromS3.psm1")) -Destination (-join($userPSModulePath,"\GetObjectsFromS3")) -Force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment