Skip to content
This repository has been archived by the owner on Oct 31, 2019. It is now read-only.

Commit

Permalink
Define callAzureStorageApi() function and use in azureListStorageBlob…
Browse files Browse the repository at this point in the history
…s() #46
  • Loading branch information
andrie committed Feb 11, 2017
1 parent e62f8df commit dbd313b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 51 deletions.
55 changes: 13 additions & 42 deletions R/AzureBlob.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,15 @@ azureListStorageBlobs <- function(azureActiveContext, storageAccount, storageKey
storageKey <- refreshStorageKey(azureActiveContext, storageAccount, resourceGroup)
}

if (length(storageKey) < 1) {
stop("Error: No storageKey provided: Use storageKey argument or set in AzureContext")
}

if (length(storageKey) < 1) {
stop("Error: No storageKey provided: Use storageKey argument or set in AzureContext")
}

URL <- paste0("http://", storageAccount, ".blob.core.windows.net/", container, "?restype=container&comp=list")
D1 <- Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME", "us")
Sys.setlocale("LC_TIME", D1)
D1 <- format(Sys.time(), "%a, %d %b %Y %H:%M:%S %Z", tz = "GMT")
r <- callAzureStorageApi(URL,
storageKey = storageKey, storageAccount = storageAccount, container = container,
verbose = verbose)

SIG <- getSig(azureActiveContext, url = URL, verb = "GET", key = storageKey,
storageAccount = storageAccount, container = container,
CMD = "\ncomp:list\nrestype:container", dateS = D1)

AT <- paste0("SharedKey ", storageAccount, ":", SIG)
r <- GET(URL, add_headers(.headers = c(Authorization = AT, `Content-Length` = "0",
`x-ms-version` = "2015-04-05",
`x-ms-date` = D1)),
verbosity)


if (status_code(r) == 404) {
Expand Down Expand Up @@ -371,33 +360,15 @@ azureGetBlob <- function(azureActiveContext, blob, directory, type = "text",
blob <- paste0(directory, blob)
blob <- gsub("^/", "", blob)
blob <- gsub("^\\./", "", blob)
cat(blob)

URL <- paste("http://", storageAccount, ".blob.core.windows.net/", container, "/",
blob, sep = "")


D1 <- Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME", "C")
# `x-ms-date` <- format(Sys.time(),'%a, %d %b %Y %H:%M:%S %Z',
# tz='GMT')
Sys.setlocale("LC_TIME", D1)
D1 <- format(Sys.time(), "%a, %d %b %Y %H:%M:%S %Z", tz = "GMT")

SIG <- getSig(azureActiveContext, url = URL, verb = "GET", key = storageKey,
storageAccount = storageAccount, container = container,
CMD = paste0("/", blob), dateS = D1)

AT <- paste0("SharedKey ", storageAccount, ":", SIG)

r <- GET(URL, add_headers(.headers = c(Authorization = AT, `Content-Length` = "0",
`x-ms-version` = "2015-04-05",
`x-ms-date` = D1)),
verbosity)
``
URL <- paste0("http://", storageAccount, ".blob.core.windows.net/", container, "/", blob)
r <- callAzureStorageApi(URL,
storageKey = storageKey, storageAccount = storageAccount, container = container,
CMD = paste0("/", blob)
)

if (status_code(r) == 404) {
cat(blob)
warning("file not found")
warning("blob not found")
return(NULL)
} else if (status_code(r) != 200)
stopWithAzureError(r)
Expand Down
89 changes: 80 additions & 9 deletions R/internal.R
Original file line number Diff line number Diff line change
@@ -1,26 +1,97 @@
getSig <- function(azureActiveContext, url, verb, key, storageAccount,
extractUrlArguments <- function(x) {
ptn <- ".*\\?(.*?)"
args <- grepl("\\?", x)
z <- if (args) gsub(ptn, "\\1", x) else ""
if (z == "") {
""
} else {
z <- strsplit(z, "&")[[1]]
z <- sort(z)
z <- paste(z, collapse = "\n")
z <- gsub("=", ":", z)
paste0("\n", z)
}
}

callAzureStorageApi <- function(url, verb = "GET", storageKey, storageAccount,
headers = NULL, container = NULL, CMD, size = NULL, contenttype = NULL,
verbose = FALSE) {
dateSig <- format(Sys.time(), "%a, %d %b %Y %H:%M:%S %Z", tz = "GMT")

verbosity <- if (verbose) httr::verbose(TRUE) else NULL

if (missing(CMD) || is.null(CMD)) CMD <- extractUrlArguments(url)

sig <- createAzureStorageSignature(url = URL, verb = verb, key = storageKey,
storageAccount = storageAccount, container = container,
CMD = CMD, dateSig = dateSig, verbose = verbose)

at <- paste0("SharedKey ", storageAccount, ":", sig)

GET(url, add_headers(.headers = c(Authorization = at,
`Content-Length` = "0",
`x-ms-version` = "2015-04-05",
`x-ms-date` = dateSig)
),
verbosity)

}


createAzureStorageSignature <- function(url, verb, key, storageAccount,
headers = NULL, container = NULL, CMD = NULL, size = NULL, contenttype = NULL,
dateS, verbose = FALSE) {
dateSig, verbose = FALSE) {
if (missing(dateSig)) {
dateSig <- format(Sys.time(), "%a, %d %b %Y %H:%M:%S %Z", tz = "GMT")
}

if (length(headers)){
ARG1 <- paste0(headers, "\nx-ms-date:", dateS, "\nx-ms-version:2015-04-05")
arg1 <- if (length(headers)) {
paste0(headers, "\nx-ms-date:", dateSig, "\nx-ms-version:2015-04-05")
} else {
ARG1 <- paste0("x-ms-date:", dateS, "\nx-ms-version:2015-04-05")
paste0("x-ms-date:", dateSig, "\nx-ms-version:2015-04-05")
}

ARG2 <- paste0("/", storageAccount, "/", container, CMD)
arg2 <- paste0("/", storageAccount, "/", container, CMD)

SIG <- paste0(verb, "\n\n\n", size, "\n\n", contenttype, "\n\n\n\n\n\n\n",
ARG1, "\n", ARG2)
arg1, "\n", arg2)
if (verbose) message(paste0("TRACE: STRINGTOSIGN: ", SIG))
base64encode(hmac(key = base64decode(key),
object = iconv(SIG, "ASCII",to = "UTF-8"),
object = iconv(SIG, "ASCII", to = "UTF-8"),
algo = "sha256",
raw = TRUE)
)
)
}



getSig <- function(azureActiveContext, url, verb, key, storageAccount,
headers = NULL, container = NULL, CMD = NULL, size = NULL, contenttype = NULL,
dateSig, verbose = FALSE) {

if (missing(dateSig)) {
dateSig <- format(Sys.time(), "%a, %d %b %Y %H:%M:%S %Z", tz = "GMT")
}

arg1 <- if (length(headers)) {
paste0(headers, "\nx-ms-date:", dateSig, "\nx-ms-version:2015-04-05")
} else {
paste0("x-ms-date:", dateSig, "\nx-ms-version:2015-04-05")
}

arg2 <- paste0("/", storageAccount, "/", container, CMD)

SIG <- paste0(verb, "\n\n\n", size, "\n\n", contenttype, "\n\n\n\n\n\n\n",
arg1, "\n", arg2)
if (verbose) message(paste0("TRACE: STRINGTOSIGN: ", SIG))
base64encode(hmac(key = base64decode(key),
object = iconv(SIG, "ASCII", to = "UTF-8"),
algo = "sha256",
raw = TRUE)
)
}


stopWithAzureError <- function(r){

msg <- paste0(as.character(sys.call(1))[1], "()") # Name of calling fucntion
Expand Down

0 comments on commit dbd313b

Please sign in to comment.