R 中的 Azure PUT Blob 身份验证失败 [英] Azure PUT Blob authentication fails in R
问题描述
我想使用 R 和 Azure 存储的 Put Blob API 将文件放入我的 Blob 存储帐户,但它无法验证我的请求.不幸的是,我找不到 R 的任何文档或示例代码. Put Blob API 的一般文档:https://docs.microsoft.com/en-us/休息/api/storageservices/put-blob
I would like to use R and the Azure Storage's Put Blob API to put files into my blob storage account but it fails to authenticate my request. Unfortunately, I couldn't find any documentation or sample code for R. General documentation of 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", "
", "
", "
",
content_length, "
",
"
",
"x-ms-date:",requestdate, "
",
"x-ms-version:2015-02-21", "
",
"x-ms-blob-type:BlockBlob", "
",
"Content-Type:text/plain", "
",
"
",
"x-ms-blob-content-dis filename=", filename, "
",
"
",
"/",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 Blob API 进行了相同的尝试(对标头的格式进行了一些细微的更改)并且它运行良好,但我无法使其与 Put Blob 一起使用.从此处列出 Blob 解决方案:https://stackoverflow.com/a/29286040/8085694
I tried the same with the List Blobs API (with some minor changes in the formatting of the headers) and it works well, but I can't make it work with Put Blob. List Blob solution from here: https://stackoverflow.com/a/29286040/8085694
能否提供一些示例 R 代码用于在 Put Blob 中创建身份验证标头或帮助我解决此问题?
Could you please provide some sample R code for Authentication header creation at Put Blob or help me resolve this issue?
另外,如果我更进一步,是否有可能以某种方式将 R 对象作为 blob 上传到存储?
Also, if I go further, is it possible somehow to upload R objects as blobs to the storage?
提前致谢,
加博尔
推荐答案
我设法通过将
"字符和所有内容放在正确的位置来解决这个问题.基于 Gaurav Mantri 的帮助,我使用了:
https://docs.microsoft.com/en-us/rest/api/storageservices/authentication-for-the-azure-storage-services
I managed to resolve this issue by putting the "
" characters and everything in the right place.
Based on Gaurav Mantri's help, I used:
https://docs.microsoft.com/en-us/rest/api/storageservices/authentication-for-the-azure-storage-services
signature_string"中的以下更改有效:
The following changes in the 'signature_string' worked:
signature_string <- paste0("PUT", "
", # HTTP Verb
"
", # Content-Encoding
"
", # Content-Language
content_length, "
", # Content-Length
"
", # Content-MD5
"text/plain", "
", # Content-Type
"
", # Date
"
", # If-Modified-Since
"
", # If-Match
"
", # If-None-Match
"
", # If-Unmodified-Since
"
", # Range
# Here comes the Canonicalized Headers
"x-ms-blob-type:BlockBlob","
",
"x-ms-date:",requestdate,"
",
"x-ms-version:2015-02-21","
",
# Here comes the Canonicalized Resource
"/",account, "/",container,"/", filename)
这篇关于R 中的 Azure PUT Blob 身份验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!