如何在不安装AWS开发工具包的情况下通过Powershell从S3下载文件?

时间:2019-09-07 17:14:53

标签: amazon-web-services powershell amazon-s3

我想使用Windows Powershell从我的AWS S3存储桶下载文件。我无法安装任何AWS软件,需要创建一个API才能访问AWS S3中的文件。我使用Postman来测试该文件是否可访问并且成功。

enter image description here

鉴于此成功,我尝试遵循AWS指南,该指南说我需要创建以下内容:

  • 创建规范请求。
  • 使用规范请求和其他元数据创建字符串 进行签名。
  • 从您的AWS秘密访问密钥中获取签名密钥。然后使用 签名密钥以及上一步中的字符串,以创建一个 签名。
  • 将结果签名添加到标头中或作为HTTP请求的 查询字符串参数。

我所看到的最接近的例子是Abhaya的https://forums.aws.amazon.com/thread.jspa?threadID=251722中的这个例子,但是它也没有解决。 (此示例中的有效负载哈希是空白的有效负载哈希)。我已经看过几本AWS指南,但是在尝试将它们应用于Powershell时,这些指南非常令人困惑。 https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html

以下代码中的网址生成了看起来正确的网址:http://SAMPLEBUCKETNAME HERE.s3-ap-southeast-1.amazonaws.com/test.xlsx?&X-Amz-Algorithm=AWS4-HMAC-SHA256&X- Amz凭据=%2F20190907%2Fap-southeast-1%2Fs3%2Faws4_request&X-Amz-Date = 20190907T1644136560000Z&X-Amz-E0&X-Amz-SignedHeaders = host&X-Amz-Signature = HASH HERE

$method = 'GET'
$service = 's3'
$host1 = 'SAMPLES3BUCKETNAME.s3-ap-southeast-1.amazonaws.com'
$region = 'ap-southeast-1'
$endpoint = 'http://SAMPLES3BUCKETNAME.s3-ap-southeast-1.amazonaws.com/test.xlsx'

function HmacSHA256($message, $secret){
   <#$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
    $hmacsha.key = [Text.Encoding]::UTF8.GetBytes($secret)
    #$hmacsha.key = $secret
    $signature = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($message))
    $signature = [Convert]::ToBase64String($signature)
   #>
    $hmacsha = New-Object System.Security.Cryptography.HMACSHA256

    $hmacsha.Key = @($secret -split '(?<=\G..)(?=.)'|ForEach-Object {[byte]::Parse($_,'HexNumber')})

    $sign = [BitConverter]::ToString($hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($message))).Replace('-','').ToLower()

    return $sign
}

function getSignatureKey($key, $dateStamp, $regionName, $serviceName)
{
    $kSecret = [Text.Encoding]::UTF8.GetBytes(("AWS4" + $key).toCharArray())
    $kDate = HmacSHA256 $dateStamp $kSecret;
    $kRegion = HmacSHA256 $regionName $kDate ;
    $kService = HmacSHA256 $serviceName $kRegion ;
    $kSigning = HmacSHA256 "aws4_request" $kService ;

    return $kSigning;
}


$access_key = 'SAMPLEACCESSKEY'
$secret_key = 'SAMPLESECRETKEY'

$amz_date = [DateTime]::UtcNow.ToString('yyyyMMddTHHmmssfffffffZ')
$datestamp = [DateTime]::UtcNow.ToString('yyyyMMdd')

$canonical_uri = '/'
$canonical_headers = 'host:' + $host1 + "`n"
$signed_headers = 'host'

$algorithm = 'AWS4-HMAC-SHA256'
$credential_scope = $datestamp + '/' + $region + '/' + $service + '/' + 'aws4_request'

$canonical_querystring = ''
$canonical_querystring += '&X-Amz-Algorithm=AWS4-HMAC-SHA256'
$canonical_querystring += '&X-Amz-Credential=' + [uri]::EscapeDataString(($access_key + '/' + $credential_scope))
$canonical_querystring += '&X-Amz-Date=' + $amz_date
$canonical_querystring += '&X-Amz-Expires=86400'
$canonical_querystring += '&X-Amz-SignedHeaders=' + $signed_headers


$payload_hash = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'

$canonical_request1 = $method + "`n" +$canonical_uri + "`n" + $canonical_querystring + "`n" + $canonical_headers + "`n" + $signed_headers + "`n" + $payload_hash
Write-Host $canonical_request1

function hash($request) {

$hasher = [System.Security.Cryptography.SHA256]::Create()
$content = [Text.Encoding]::UTF8.GetBytes($request)
$hash = [System.Convert]::ToBase64String($hasher.ComputeHash($content))
return $hash

}

$canonical_request = hash -request $canonical_request1
$string_to_sign = $algorithm + "`n" +  $amz_date + "`n" +  $credential_scope + "`n" +  $canonical_request


$signing_key = getSignatureKey $secret_key $datestamp $region $service
$signature =  HmacSHA256 -secret $signing_key -message $string_to_sign

$canonical_querystring += '&X-Amz-Signature=' + $signature

$request_url = $endpoint + "?" + $canonical_querystring
$request_url

当我尝试访问URL时出现以下错误。 enter image description here

1 个答案:

答案 0 :(得分:2)

存在一些错误,特别是您如何计算签名,建立时间戳记,以及所看到的错误是因为未正确传递参数。

以下是纠正这些问题的版本:

$method = 'GET'
$service = 's3'
$bucket = "SAMPLES3BUCKETNAME"
$key = 'test.xlsx'
$region = 'ap-southeast-1'
$host1 = $bucket + '.s3-' + $region + '.amazonaws.com'
$access_key = 'SAMPLEACCESSKEY'
$secret_key = 'SAMPLESECRETKEY'

function HmacSHA256($message, $secret)
{
    $hmacsha = New-Object System.Security.Cryptography.HMACSHA256
    $hmacsha.key = $secret
    $signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($message))

    return $signature
}

function getSignatureKey($key, $dateStamp, $regionName, $serviceName)
{
    $kSecret = [Text.Encoding]::UTF8.GetBytes(("AWS4" + $key).toCharArray())
    $kDate = HmacSHA256 $dateStamp $kSecret;
    $kRegion = HmacSHA256 $regionName $kDate;
    $kService = HmacSHA256 $serviceName $kRegion;
    $kSigning = HmacSHA256 "aws4_request" $kService;

    return $kSigning
}

function hash($request)
{
    $hasher = [System.Security.Cryptography.SHA256]::Create()
    $content = [Text.Encoding]::UTF8.GetBytes($request)
    $bytes = $hasher.ComputeHash($content)

    return ($bytes|ForEach-Object ToString x2) -join ''
}

$now = [DateTime]::UtcNow
$amz_date = $now.ToString('yyyyMMddTHHmmssZ')
$datestamp = $now.ToString('yyyyMMdd')

$signed_headers = 'host'
$credential_scope = $datestamp + '/' + $region + '/' + $service + '/' + 'aws4_request'

$canonical_querystring = 'X-Amz-Algorithm=AWS4-HMAC-SHA256'
$canonical_querystring += '&X-Amz-Credential=' + [uri]::EscapeDataString(($access_key + '/' + $credential_scope))
$canonical_querystring += '&X-Amz-Date=' + $amz_date
$canonical_querystring += '&X-Amz-Expires=86400'
$canonical_querystring += '&X-Amz-SignedHeaders=' + $signed_headers

$canonical_headers = 'host:' + $host1 + "`n"

$canonical_request = $method + "`n"
$canonical_request += "/" + $key + "`n"
$canonical_request += $canonical_querystring + "`n"
$canonical_request += $canonical_headers + "`n"
$canonical_request += $signed_headers + "`n"
$canonical_request += "UNSIGNED-PAYLOAD"

$algorithm = 'AWS4-HMAC-SHA256'

$canonical_request_hash = hash -request $canonical_request
$string_to_sign = $algorithm + "`n"
$string_to_sign += $amz_date + "`n"
$string_to_sign += $credential_scope + "`n"
$string_to_sign += $canonical_request_hash

$signing_key = getSignatureKey $secret_key $datestamp $region $service
$signature =  HmacSHA256 -secret $signing_key -message $string_to_sign
$signature = ($signature|ForEach-Object ToString x2) -join ''

$canonical_querystring += '&X-Amz-Signature=' + $signature

$request_url = "http://" + $host1 + "/" + $key + "?" + $canonical_querystring

Write-Host $request_url