Azure PUT Blob authentication fails in R
I would like to use R and the Azure Storage Put Blob API to put files in my blob storage account, but it cannot authenticate my request. Unfortunately I couldn't find any documentation or sample code for R. General Put Blob API documentation: https://docs.microsoft.com/en-us/rest/api/storageservices/put-blob
Here is the code I tried to use:
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")`
The request sends:
-> 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
Answer:
<- 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
I tried the same thing with the List Blobs API (with some minor changes to the formatting of the headers) and it works well, but I cannot get it to work with Put Blob. List of Blob solutions from here: stackoverflow
Could you please provide an example R code for creating an Authentication Header in Put Blob or help me solve this problem?
Also, if I go further, is it possible somehow to load R objects as blobs into the store?
Thanks in advance,
Gabor
source to share
I managed to solve this problem by putting the "\ n" characters and everything in the right place. Based on help from Gaurava Mantry, I used:
https://docs.microsoft.com/en-us/rest/api/storageservices/authentication-for-the-azure-storage-services
The following changes to 'signature_string' worked:
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)
source to share
There is an Azure offical R package on GitHub Microsoft/AzureSMR
that can help you simplify the use of R and Azure Blob Storage, you can refer to the tutorial for details.
If you just want to use some Azure services like Blob Storage, not otherwise, I think some of the source code for this project is very valuable for better code improvement, for example createAzureStorageSignature
, which can directly help in creating a signature to solve your problem.
source to share