Azure PUT Blob 身份验证在 R 中失败

Azure PUT Blob authentication fails in R

我想使用 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 进行了相同的尝试(对 headers 的格式进行了一些细微的更改)并且效果很好,但我无法使其与 Put 一起使用斑点。 从这里列出 Blob 解决方案:

您能否提供一些用于在 Put Blob 中创建身份验证 header 的示例 R 代码或帮助我解决此问题?

此外,如果我更进一步,是否可以以某种方式将 R objects 作为 blob 上传到存储?

提前致谢,

嘉宝

我设法通过将“\n”字符和所有内容放在正确的位置来解决这个问题。 根据 Gaurav 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)

有一个 Azure 官方 R 包 Microsoft/AzureSMR on GitHub, which can help you easier using R & Azure Blob Storage, you can refer to its tutorial 了解更多详情。

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