我想使用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