首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过R查询AWS Pricing API

通过R查询AWS Pricing API
EN

Stack Overflow用户
提问于 2020-07-28 20:08:45
回答 1查看 86关注 0票数 0

我尝试通过R查询AWS pricing端点,但收到403错误。

我到处寻找在R中运行AWS端点的通用示例,但真的找不到太多。有什么想法吗?

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-20 19:59:06

您也可以使用paws程序包查询亚马逊网络服务价目表服务应用编程接口。Paws提供了从R内部访问全套亚马逊网络服务的权限,类似于boto3,Python的官方AWS SDK,为Python用户提供的服务。

在下面的示例中,我使用一个定价客户端来获取亚马逊SageMaker当前可用的所有位置。我使用purrr包来解析响应。我将我的亚马逊网络服务凭证(AWS_ACCESS_KEY_ID、AWS_SECRET_ACCESS_KEY)存储在.Renviron中,这就是您在下面的代码中看不到它们的原因。

代码语言:javascript
复制
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)" 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63133790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档