我想使用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
請確保您的'signature_string'完全基於此處所述的規範構建:https://docs.microsoft.com/en-us/rest/api/storageservices/authentication-for-the-azure-storage-服務。小偏差將導致403錯誤。 –
謝謝,我設法通過檢查文檔中的每個小細節來實現這個工作。 – Gabor