【问题标题】:How to sort a list by byte-order for AWS-Calls如何按字节顺序对 AWS-Calls 的列表进行排序
【发布时间】:2015-09-26 15:00:52
【问题描述】:

看看http://associates-amazon.s3.amazonaws.com/signed-requests/helper/index.html

以下名称-值对:

Service=AWSECommerceService
Version=2011-08-01
AssociateTag=PutYourAssociateTagHere
Operation=ItemSearch
SearchIndex=Books
Keywords=harry+potter
Timestamp=2015-09-26T14:10:56.000Z
AWSAccessKeyId=123

名称-值对已按字节顺序排序

应该会导致

AWSAccessKeyId=123
AssociateTag=PutYourAssociateTagHere
Keywords=harry%20potter
Operation=ItemSearch
SearchIndex=Books
Service=AWSECommerceService
Timestamp=2015-09-26T14%3A10%3A56.000Z
Version=2011-08-01

如何在 R 中实现这一点?

据我所知,它们是按其排序的 as.numeric(charToRaw(name)) 值。如果第一个值相等,则它们按第二个排序,然后是第三个,依此类推。

问题:如何在 R 中做到这一点?

【问题讨论】:

    标签: r sorting amazon-web-services amazon-product-api endianness


    【解决方案1】:
    # Name-Value-Pairs
    nvp <- list(                                
    "Service"="AWSECommerceService",
    "Version"="2011-08-01",
    "AssociateTag"="PutYourAssociateTagHere",
    "Operation"="ItemSearch",
    "SearchIndex"="Books",
    "Keywords"="harry potter",
    "Timestamp"="2015-09-26T14:10:56.000Z",
    "AWSAccessKeyId"="123"
    )
    

    获取字节:

    bytes <- function(chr){
      as.data.frame(t(as.numeric(charToRaw(chr))))
    }
    

    计算字节,并重新绑定值

    b <- lapply(names(nvp), bytes)
    b <- data.table::rbindlist(b, fill=TRUE) # other than base::rbind, this fills by NA
    

    按第一列、第二列、第三列、...等对名称进行排序

    names(nvp)[do.call(order, as.list(b))]
    
    [1] "AWSAccessKeyId" "AssociateTag"   "Keywords"       "Operation"      "SearchIndex"   
    [6] "Service"        "Timestamp"      "Version"   
    

    所以最后nvp[do.call(order, as.list(b))] 在正确排序的列表中返回

    【讨论】:

      【解决方案2】:

      上面来自@Floo0 的答案非常好,与来自this answer 的加密签名结合使用时更加有用。

      在找到这两个帖子之前,我被困住了。我使用亚马逊的Signed Request Helper 来验证我是否成功签署了我的查询。使用上面的代码对查询进行正确的规范排序,并使用此代码 (again found here) 对其进行签名:

      library(digest)
      library(RCurl)
      
      curlEscape(
        base64(
               hmac(enc2utf8((secret_key)), 
                    enc2utf8(string_to_sign), 
                    algo = 'sha256', 
                    serialize = FALSE,  
                     raw = TRUE)
                )
               )  
      

      还有,我还没用过,不过有一个Python moduleamazon-product-api,好像工作量不大。

      【讨论】:

      • 也许您还应该提到,这需要 digestRCurl 包才能工作
      猜你喜欢
      • 1970-01-01
      • 2011-02-03
      • 2013-05-02
      • 2014-02-08
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多