2017-06-13 48 views
1

我想使用R和Azure存储的Put Blob API将文件放入我的blob存储帐户,但它无法验证我的请求。不幸的是,我无法找到把一滴API的R.通用文档的任何文件或示例代码: https://docs.microsoft.com/en-us/rest/api/storageservices/put-blobAzure PUT Blob身份验证在R中失败

这里是我试图用代码:

library(httr) 

account <- "myAccount" 
container <- "myContainer" 
filename <- "test.txt" 
key <- "primaryKey" 
object <- "Hello World" 

url <- paste0("https://", account, ".blob.core.windows.net/", container, "/", filename) 
requestdate <- format(Sys.time(),"%a, %d %b %Y %H:%M:%S %Z", tz="GMT") 
content_length <- nchar(object, type = "bytes") 

signature_string <- paste0("PUT", "\n", "\n", "\n", 
         content_length, "\n", 
         "\n", 
         "x-ms-date:",requestdate, "\n", 
         "x-ms-version:2015-02-21", "\n", 
         "x-ms-blob-type:BlockBlob", "\n", 
         "Content-Type:text/plain", "\n", 
         "\n", 
         "x-ms-blob-content-dis filename=", filename, "\n", 
         "\n", 
         "/",account, "/",container,"/", filename) 

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

content(PUT(url, config = headerstuff, body = object, verbose()), as = "text")` 

提出要求发送:

-> PUT /myContainer/test.txt HTTP/1.1 
-> Host: myAccount.blob.core.windows.net 
-> User-Agent: libcurl/7.49.1 r-curl/2.3 httr/1.2.1 
-> Accept-Encoding: gzip, deflate 
-> Accept: application/json, text/xml, application/xml, */* 
-> Authorization: SharedKey myAccount:hashedSignatureString 
-> Content-Length: 11 
-> x-ms-date: Tue, 13 Jun 2017 08:50:38 GMT 
-> x-ms-version: 2015-02-21 
-> x-ms-blob-type: BlockBlob 
-> Content-Type: text/plain 
-> 
>> Hello World 

响应:

<- HTTP/1.1 403 Server failed to authenticate the request. Make sure the 
value of Authorization header is formed correctly including the signature. 
<- Content-Length: 693 
<- Content-Type: application/xml 
<- Server: Microsoft-HTTPAPI/2.0 
<- x-ms-request-id: efc2c8de-0001-00a9-3d21-e41b06000000 
<- Date: Tue, 13 Jun 2017 08:48:56 GMT 

我尝试了与List Blobs API(对标题格式化进行一些小改动)相同的方法,它运行良好,但我无法使它与Put Blob一起工作。 列表Blob解决方案从这里:https://stackoverflow.com/a/29286040/8085694

您可以请提供一些示例R代码为验证标题创建放置Blob或帮助我解决此问题?

此外,如果我走得更远,是否有可能以某种方式将R对象作为斑点上传到存储?

由于提前,

的Gabor

+0

请确保您的'signature_string'完全基于此处所述的规范构建:https://docs.microsoft.com/en-us/rest/api/storageservices/authentication-for-the-azure-storage-服务。小偏差将导致403错误。 –

+0

谢谢,我设法通过检查文档中的每个小细节来实现这个工作。 – Gabor

回答

3

我设法把“\ n”字,一切都在正确的地方,以解决此问题。 基于拉夫Mantri的帮助下,我用:
https://docs.microsoft.com/en-us/rest/api/storageservices/authentication-for-the-azure-storage-services

在 'signature_string' 以下变化工作:

signature_string <- paste0("PUT", "\n",   # HTTP Verb 
          "\n",     # Content-Encoding 
          "\n",     # Content-Language 
          content_length, "\n", # Content-Length 
          "\n",     # Content-MD5 
          "text/plain", "\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, "/",container,"/", filename) 
0

有GitHub上的Azure的官方[R包Microsoft/AzureSMR,它可以帮助你更轻松使用R & Azure Blob Storage,您可以参考其tutorial以了解更多详细信息。

如果你只是想使用一些像Blob存储这样的Azure服务,而不是其他的,我认为这个项目的一些源代码对于更好地重建你的代码非常有价值,如createAzureStorageSignature方法可以直接帮助构建签名来解决你的问题。