Skip to content

Instantly share code, notes, and snippets.

@HammoTime
Last active May 30, 2018 06:23
Show Gist options
  • Save HammoTime/53c7efee144fcf5e1444e1cb299b48bf to your computer and use it in GitHub Desktop.
Save HammoTime/53c7efee144fcf5e1444e1cb299b48bf to your computer and use it in GitHub Desktop.
Clone the contents of an S3 bucket locally
Import-Module AWSPowerShell
# So our AWS Scripts work well with Proxies.
$Browser = New-Object System.Net.WebClient
$Browser.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials
$BucketName = 'TestBucket'
$Region = 'ex-ampleregion-1'
$TargetDirectory = 'C:\BucketOutput\'
$AllObjects = Get-S3Object $BucketName -KeyPrefix '/' -Region $Region
ForEach ($Object in $AllObjects) {
$ItemPathParts = $Object.Key.Split('/')
$CurrentPath = $TargetDirectory + '\'
For($i = 0; $i -lt ($ItemPathParts.Length - 1); $i++) {
$CurrentPath += $ItemPathParts[$i] + '\'
if(!(Test-Path $CurrentPath)) {
New-Item -ItemType Directory -Path $CurrentPath
}
}
# Let's not attempt to create empty directories.
# A bit of a funny one because there are really no "directories" in S3.
if($Object.Key.Substring($Object.Key.Length - 1, 1) -ne '/') {
Write-Host "Source File: $($Object.Key)."
Write-Host "Target File: $($TargetDirectory + $Object.Key.Replace('/', '\'))."
Read-S3Object $BucketName -Key $Object.Key -File ($TargetDirectory + $Object.Key.Replace('/', '\')) -Region $Region | Out-Null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment