R

时间:2017-06-13 10:50:44

标签: r azure azure-storage-blobs

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

以下是我尝试使用的代码:

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代码,用于在Put Blob创建身份验证标头或帮我解决此问题?

另外,如果我走得更远,是否有可能将R对象作为blob上传到存储?

提前致谢,

的Gabor

2 个答案:

答案 0 :(得分:3)

我设法通过放置&#34; \ n&#34;来解决这个问题。人物和一切都在正确的地方。 根据Gaurav Mantri的帮助,我使用了:
https://docs.microsoft.com/en-us/rest/api/storageservices/authentication-for-the-azure-storage-services

&#39; signature_string&#39;中的以下更改工作:

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)

答案 1 :(得分:0)

GitHub上有一个Azure官方R套件Microsoft/AzureSMR,它可以帮助您更轻松地使用R&amp; Azure Blob存储,您可以参考其tutorial了解更多详细信息。

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