我试图通过R查询AWS定价端点,但收到403错误。
我四处寻找了在R中运行AWS端点的通用示例,但实际上没有发现太多。有什么想法吗?
library(aws.signature)
library(httr)
# validate arguments and setup request URL
current <- Sys.time()
d_timestamp <- format(current, "%Y%m%dT%H%M%SZ", tz = "UTC")
hdrs <- list(`Content-Type` = "application/x-www-form-urlencoded",
Host = "apigateway.us-east-1.amazonaws.com",
`x-amz-date` = d_timestamp)
params <- signature_v4_auth(
datetime = d_timestamp,
region = "us-east-1",
service = "execute-api",
verb = "GET",
action = "iMetaAPI",
query_args = list(),
canonical_headers = hdrs,
request_body = "json",
key = "***********",
secret = "***************",
session_token = NULL,
query = FALSE,
algorithm = "AWS4-HMAC-SHA256",
verbose = TRUE)
a <- GET("https://api.pricing.us-east-1.amazonaws.com",
query = params)
您还可以使用paws
包查询AWS价目表服务API。Paws提供了从R中访问全套AWS服务的权限,类似于面向Python的官方AWS SDK boto3向Python用户提供的服务。
在下面的示例中,我使用定价客户端来获取Amazon SageMaker当前可用的所有位置。我使用purrr
包来解析响应。我将我的AWS凭据(AWS_ACCESS_KEY_ID、AWS_SECRET_ACCESS_KEY(存储在.Renviron
中,这就是您在下面的代码中看不到它们的原因。
library(paws)
library(purrr)
# Create pricing client
pricing_client <- pricing()
# Fetch metadata information of the SageMaker service
sm_metadata <- pricing_client$describe_services(
ServiceCode = "AmazonSageMaker")
str(sm_metadata)
#> List of 3
#> $ Services :List of 1
#> ..$ :List of 2
#> .. ..$ ServiceCode : chr "AmazonSageMaker"
#> .. ..$ AttributeNames: chr [1:34] "productFamily" "automaticLabel" "volumeType" "memory" ...
#> $ FormatVersion: chr "aws_v1"
#> $ NextToken : chr(0)
# Store available SageMaker service attribute names in a vector
sm_attributes <- sm_metadata %>%
pluck("Services", 1, "AttributeNames")
tail(sm_attributes)
#> [1] "groupDescription" "currentGeneration" "integratingService" "location" "processorArchitecture" "operation"
# Fetch all locations where the SageMaker service is available
sm_locations <- pricing_client$get_attribute_values("AmazonSageMaker", "location") %>%
.[["AttributeValues"]] %>%
map_chr(function(x) x[["Value"]])
sm_locations
#> [1] "AWS GovCloud (US-West)" "Any" "Asia Pacific (Hong Kong)"
#> [4] "Asia Pacific (Mumbai)" "Asia Pacific (Seoul)" "Asia Pacific (Singapore)"
#> [7] "Asia Pacific (Sydney)" "Asia Pacific (Tokyo)" "Canada (Central)"
#> [10] "EU (Frankfurt)" "EU (Ireland)" "EU (London)"
#> [13] "EU (Paris)" "EU (Stockholm)" "Middle East (Bahrain)"
#> [16] "South America (Sao Paulo)" "US East (N. Virginia)" "US East (Ohio)"
#> [19] "US West (N. California)" "US West (Oregon)"