Skip to content

Instantly share code, notes, and snippets.

@plivox
Created June 20, 2017 14:16
Show Gist options
  • Save plivox/a876f8ecf30c6eb9776f4eeee1ccab16 to your computer and use it in GitHub Desktop.
Save plivox/a876f8ecf30c6eb9776f4eeee1ccab16 to your computer and use it in GitHub Desktop.
AWS S3 Get file with PowerShell
function S3-Get([string]$uri, [string]$bucket) {
$date = Get-Date -format "ddd, dd MMM yyyy HH:mm:ss zz00"
$content_type = "text/plain"
$request = "GET`n`n${content_type}`n${date}`n${bucket}"
#
# Bash equivalent is:
# $ echo -en ... | openssl sha1 -hmac ... -binary | base64
#
$hmacsha = New-Object System.Security.Cryptography.HMACSHA1
$hmacsha.key = [Text.Encoding]::ASCII.GetBytes($AWS_SECRET_ACCESS_KEY)
$signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($request))
$signature = [Convert]::ToBase64String($signature)
curl --insecure `
-H "Date: ${date}" `
-H "Content-Type: ${content_type}" `
-H "Authorization: AWS ${AWS_ACCESS_KEY_ID}:${signature}" "${uri}${bucket}"
}
# Exemple:
# S3-Get "s3_url ... " "/my_bucket"
@plivox
Copy link
Author

plivox commented Jun 20, 2017

Require curl for Windows, install with chocolatey:

$ choco install curl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment