我想使用 R 包httr
通过他们的 API 访问 EC2 服务。但我有点不确定如何开始,因为它不属于通常的“Oauth2.0”身份验证格式,在这种格式中你有通常的:密钥、秘密、令牌和签名系统。我认为 EC2 使用“签名版本 2”方法,但我不清楚它是如何工作的。
查看 EC2 提供的关于在http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/using-query-api.html提出查询请求的文档
我想我需要签名的价值....但不知道如何获得它
我尝试使用一些给定的命令httr
,如下所示。我可以调整 URL 字符串中的大多数参数来代表我和我想做的事情,例如,AWSAccessKeyId
等等……但只是不知道去哪里获取签名值。ImageId
endpoint
Action
同样在给出的一些示例中,它们似乎也没有提供秘密访问密钥......
因此,尝试过的命令如下,改变了一些代表我的值,但得到了以下结果:
require(httr)
GET("https://ec2.amazonaws.com/
?Action=RunInstances
&ImageId=ami-60a54009
&MaxCount=3
&MinCount=1
&Placement.AvailabilityZone=us-east-1b
&Monitoring.Enabled=true
&AWSAccessKeyId=0GS7553JW74RRM612K02EXAMPLE
&Version=2012-10-01
&Expires=2010-10-10T12:00:00Z
&Signature=lBP67vCvGlDMBQ1dofZxg8E8SUEXAMPLE
&SignatureVersion=2
&SignatureMethod=HmacSHA256")
我得到了回应:
Response [http://aws.amazon.com/ec2/]
Status: 200
Content-type: text/html; charset=UTF-8
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="icon" type="image/ico" href="//d36cz9buwru1tt.cloudfront.net/favicon.ico">
<link rel="shortcut icon" type="image/ico" href="//d36cz9buwru1tt.cloudfront.net/favicon.ico">
<meta name="description" content="Amazon Elastic Compute Cloud delivers scalable, pay-as-you-go compute capacity in the cloud. " /><meta name="keywords" content="" /> ...
有没有人对 EC2 api 及其身份验证过程有任何经验,使用 R 来设置和运行带有我选择的 AMI 的 linux 实例是否足够容易(其中加载了 R 和其他相关包),然后在这些实例中运行一些 R 命令,然后返回输出?
不要认为它与我的 sessionInfo 真正相关,但以防万一:
sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] httr_0.2
loaded via a namespace (and not attached):
[1] digest_0.5.2 plyr_1.7.1 RCurl_1.95-1.1 stringr_0.6.1 tools_2.15.1
编辑:
因此,在进一步尝试遵循@hadley 建议的文档时,这就是我尝试并得到的……任何关于我哪里出错的有用提示将不胜感激……:
require(httr)
aws.key <- "xxxxxxx"
aws.secret <- "xxxxxxxxxxxx"
verb <- "GET"
zone <- "ec2.amazonaws.com"
func <- "DescribeImages"
ami.number <- "ami-xxxxxxxxx"
params <- list(paste0("ImageId.1=",ami.number),
"Version=2012-10-01",
"Expires=2012-11-20T12%3A00%3A00Z")
# adding in method and key parameters for creation of string to sign
orig.len.params <- length(params)
params.w.method.key <- params
params.w.method.key[[orig.len.params+1]] <- "SignatureVersion=2"
params.w.method.key[[orig.len.params+2]] <- "SignatureMethod=HmacSHA1"
params.w.method.key[[orig.len.params+3]] <- paste0("AWSAccessKeyId=",aws.key)
# String to sign (s2s)
s2s <- paste(c(paste0(verb,"\n",zone,"\n","/\n","AWSAccessKeyId=",aws.key),paste0("Action=",func),paste(sort(unlist(params.w.method.key)),collapse="&")),collapse="&")
# Signature(sig)
sig <- hmac_sha1(aws.secret, s2s)
# adding in signature, method and key parameters for signed request url generation
params.w.sig.method.key <- params
params.w.sig.method.key[[orig.len.params+1]] <- paste0("Signature=",sig)
params.w.sig.method.key[[orig.len.params+2]] <- "SignatureVersion=2"
params.w.sig.method.key[[orig.len.params+3]] <- "SignatureMethod=HmacSHA1"
params.w.sig.method.key[[orig.len.params+4]] <- paste0("AWSAccessKeyId=",aws.key)
# Signed request (sr)
sr <- paste(c(paste0("https://",zone,paste0("?Action=",func)),paste(unlist(params.w.sig.method.key),collapse="&")),collapse="&")
# GET signed request
GET(sr)
我得到了回应:
Response [https://ec2.amazonaws.com?Action=DescribeImages&ImageId.1=[ami.number.from.before]&Version=2012-10-01&Expires=2012-11-20T12%3A00%3A00Z&Signature=[sig.value.from.before]&SignatureVersion=2&SignatureMethod=HmacSHA1&AWSAccessKeyId=[aws.key.from.before]/]
Status: 401
Content-type:
<?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>AuthFailure</Code><Message>AWS was not able to validate the provided access credentials</Message></Error></Errors><RequestID>5e10fb0b-f304-4677-9c64-98b4537c659a</RequestID></Response>