首页 > 解决方案 > 使用 R 中的 REST API 在 Azure 表存储中创建表

问题描述

我正在 R 笔记本中处理 Azure Databricks,为了编写由我的 R 代码生成的元数据,我尝试连接/读取/写入/从/到 Azure 表存储服务。

在这里,我学习了如何访问和读取一个表(名为“myTable),经过一些修改后工作正常。这是我所做的:

 key <- <myKey>
 account <- <myAccount>

 url <- paste0("https://", account, ".table.core.windows.net/myTable()")
 requestdate <- format(Sys.time(),"%a, %d %b %Y %H:%M:%S %Z", tz="GMT")
 #content_length <- 0

 signature_string <- paste0("GET", "\n",            # HTTP Verb
                       "\n",                   # Content-MD5
                       "text/plain", "\n",     # Content-Type
                       requestdate, "\n",                   # Date
                       # Here comes the Canonicalized Resource
                       "/",account, "/","myTable()" )

 header <- add_headers(Authorization=paste0("SharedKey ",account,":", 
                                            RCurl::base64(digest::hmac(key = 
                                                                         RCurl::base64Decode(key, mode = "raw"),
                                                                       object = enc2utf8(signature_string),
                                                                       algo = "sha256", raw = TRUE))),
                       `x-ms-date`= requestdate,
                       `x-ms-version`= "2015-02-21",
                       `Content-Type`="text/plain")

 GET(url, config = header, verbose()) -> res

现在,我想创建另一个表以及向现有表添加新实体。我采用了上述方法并将其修改为

 url <- paste0("https://", account, ".table.core.windows.net/TABLES")
 requestdate <- format(Sys.time(),"%a, %d %b %Y %H:%M:%S %Z", tz="GMT")

 signature_string <- paste0("POST", "\n",            # HTTP Verb
                       "\n",                   # Content-Encoding  
                       "\n",                   # Content-Language
                       content_length, "\n",   # Content-Length
                       "\n",                   # Content-MD5
                       "application/json", "\n",     # Content-Type
                       "\n",                   # Date
                       "\n",                   # If-Modified-Since
                       "\n",                   # If-Match  
                       "\n",                   # If-None-Match
                       "\n",                   # If-Unmodified-Since
                       "\n",                   # Range
                       # Here comes the Canonicalized Headers
                       # "x-ms-blob-type:BlockBlob","\n",
                       "x-ms-date:",requestdate,"\n",
                       #"x-ms-version:2015-02-21","\n",
                       # Here comes the Canonicalized Resource
                       "/",account, "/","TABLES")

 header <- add_headers(Authorization=paste0("SharedKey ",account,":", 
                                            RCurl::base64(digest::hmac(key = 
                                                                         RCurl::base64Decode(key, mode = "raw"),
                                                                       object = enc2utf8(signature_string),
                                                                       algo = "sha256", raw = TRUE))),
                       `x-ms-date`= requestdate,
                      # `x-ms-version`= "2015-02-21",
                       `Content-Type`="application/json",
                       `Content-Length`=100)

 body <- paste0("{", "\n",
                       "'TableName':'newTable'",  "\n",                          
           "}")

 POST(url = url, config = header, body = body, encode = "json", handle = NULL) -> res

运行它会给我状态 403。确切的消息是:

  Response [https://myAccount.table.core.windows.net/TABLES]
  Date: 2020-01-31 18:47
  Status: 403
  Content-Type: application/json
  Size: 437 B

 AuthenticationFailed 服务器未能验证请求。使... RequestId:9bc35a5f-c002-1666-d88043000000 时间:2020-01-31T18:47:36.6598228Z

我玩了几个小时的 signature_string 输入,但没有成功。我也尝试了不同的身体定义,例如

 request_body <- data.frame(TableName = "access")
 body <- jsonlite::toJSON(request_body, auto_unbox = T)

我不确定以下输入:Content-Length、Content-Type、x-ms-version、request-body 以及我是否遗漏了其他任何内容。

有人可以给我建议如何编写正确的标题来创建新表和添加新实体。如果有更优雅的方式,这也将不胜感激。

标签: razurerestazure-table-storageazure-databricks

解决方案


推荐阅读