From ee4987feb3111f971b6094bbb14b3f4859cdddd2 Mon Sep 17 00:00:00 2001 From: Andrie de Vries Date: Sun, 12 Feb 2017 10:48:22 +0000 Subject: [PATCH] Add updateAzureActiveContext() function to simplify code #46 --- R/AzureBlob.R | 69 ++++++++++++++++++++++----------------------------- R/internal.R | 38 ++++++++++++++++++---------- 2 files changed, 55 insertions(+), 52 deletions(-) diff --git a/R/AzureBlob.R b/R/AzureBlob.R index 3bab015..60c1a6e 100644 --- a/R/AzureBlob.R +++ b/R/AzureBlob.R @@ -11,9 +11,7 @@ azureListStorageBlobs <- function(azureActiveContext, storageAccount, storageKey container, resourceGroup, subscriptionID, azToken, verbose = FALSE) { - #if (!missing(azureActiveContext)) { - #azureCheckToken(azureActiveContext) - #} + if (!missing(azureActiveContext)) azureCheckToken(azureActiveContext) if (missing(resourceGroup) && !missing(azureActiveContext)) { resourceGroup <- azureActiveContext$resourceGroup @@ -85,17 +83,17 @@ azureListStorageBlobs <- function(azureActiveContext, storageAccount, storageKey ) } + updateAzureActiveContext(azureActiveContext, + storageAccount = storageAccount, + resourceGroup = resourceGroup, + storageKey = storageKey + ) getXmlBlob <- function(x, property, path = "//blobs//blob//properties/"){ pth <- paste0(path, property) xpathSApply(y, pth, xmlValue) } - - azureActiveContext$storageAccount <- storageAccount - azureActiveContext$resourceGroup <- resourceGroup - azureActiveContext$storageKey <- storageKey - data.frame( name = getXmlBlob(y, "name", path = "//blobs//blob//"), lastModified = getXmlBlob(y, "last-modified"), @@ -122,7 +120,9 @@ azureListStorageBlobs <- function(azureActiveContext, storageAccount, storageKey azureBlobLS <- function(azureActiveContext, directory, recursive = FALSE, storageAccount, storageKey, container, resourceGroup, subscriptionID, azToken, verbose = FALSE) { - azureCheckToken(azureActiveContext) + + if (!missing(azureActiveContext)) azureCheckToken(azureActiveContext) + if (missing(subscriptionID)) { subscriptionID <- azureActiveContext$subscriptionID } else (subscriptionID <- subscriptionID) @@ -280,7 +280,7 @@ azureBlobLS <- function(azureActiveContext, directory, recursive = FALSE, #' @inheritParams azureSAGetKey #' @inheritParams azureBlobLS -#' @param type String, either "text" or "raw" +#' @param type String, either "text" or "raw". Passed to \code{\link[httr]{content}} #' #' @family blob store functions #' @export @@ -288,30 +288,22 @@ azureBlobLS <- function(azureActiveContext, directory, recursive = FALSE, azureGetBlob <- function(azureActiveContext, blob, directory, type = "text", storageAccount, storageKey, container, resourceGroup, subscriptionID, azToken, verbose = FALSE) { - #if (!missing(azureActiveContext) || !is.null(azureActiveContext)) { - #azureCheckToken(azureActiveContext) - #} - - #if (missing(subscriptionID)) { - #subscriptionID <- azureActiveContext$subscriptionID - #} - #if (missing(azToken)) { - #azToken <- azureActiveContext$Token - #} + if (!missing(azureActiveContext)) azureCheckToken(azureActiveContext) - if (missing(resourceGroup)) { + if (missing(resourceGroup) && !missing(azureActiveContext)) { resourceGroup <- azureActiveContext$resourceGroup - } - if (missing(storageAccount)) { + } + + if (missing(storageAccount) && !missing(azureActiveContext) && !is.null(azureActiveContext)) { storageAccount <- azureActiveContext$storageAccount - } - if (missing(storageKey)) { + } + if (missing(storageKey) && !missing(azureActiveContext)) { storageKey <- azureActiveContext$storageKey - } - if (missing(container)) { + } + if (missing(container) && !missing(azureActiveContext)) { container <- azureActiveContext$container - } - if (missing(blob)) { + } + if (missing(blob) && !missing(azureActiveContext)) { blob <- azureActiveContext$blob } verbosity <- if (verbose) @@ -330,8 +322,6 @@ azureGetBlob <- function(azureActiveContext, blob, directory, type = "text", stop("Error: No blob provided: Use blob argument or set in AzureContext") } - #storageKey <- refreshStorageKey(azureActiveContext, storageAccount, resourceGroup) - if (length(storageKey) < 1) { stop("Error: No storageKey provided: Use storageKey argument or set in AzureContext") } @@ -360,7 +350,7 @@ azureGetBlob <- function(azureActiveContext, blob, directory, type = "text", blob <- paste0(directory, blob) blob <- gsub("^/", "", blob) blob <- gsub("^\\./", "", blob) -`` + URL <- paste0("http://", storageAccount, ".blob.core.windows.net/", container, "/", blob) r <- callAzureStorageApi(URL, storageKey = storageKey, storageAccount = storageAccount, container = container, @@ -373,14 +363,15 @@ azureGetBlob <- function(azureActiveContext, blob, directory, type = "text", } else if (status_code(r) != 200) stopWithAzureError(r) - r2 <- content(r, type, encoding = "UTF-8") + updateAzureActiveContext(azureActiveContext, + storageAccount = storageAccount, + resourceGroup = resourceGroup, + storageKey = storageKey, + container = container, + blob = blob + ) - azureActiveContext$storageAccount <- storageAccount - azureActiveContext$resourceGroup <- resourceGroup - azureActiveContext$storageKey <- storageKey - azureActiveContext$container <- container - azureActiveContext$blob <- blob - return(r2) + content(r, type, encoding = "UTF-8") } diff --git a/R/internal.R b/R/internal.R index f313384..57b3a70 100644 --- a/R/internal.R +++ b/R/internal.R @@ -16,39 +16,39 @@ extractUrlArguments <- function(x) { 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") + dateStamp <- 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) + sig <- createAzureStorageSignature(url = URL, verb = verb, + key = storageKey, storageAccount = storageAccount, container = container, + headers = headers, CMD = CMD, contenttype = contenttype, dateStamp = dateStamp, 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) + `x-ms-date` = dateStamp) ), verbosity) - } -createAzureStorageSignature <- function(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") +createAzureStorageSignature <- function(url, verb, + key, storageAccount, container = NULL, + headers = NULL, CMD = NULL, size = NULL, contenttype = NULL, dateStamp, verbose = FALSE) { + + if (missing(dateStamp)) { + dateStamp <- 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") + paste0(headers, "\nx-ms-date:", dateStamp, "\nx-ms-version:2015-04-05") } else { - paste0("x-ms-date:", dateSig, "\nx-ms-version:2015-04-05") + paste0("x-ms-date:", dateStamp, "\nx-ms-version:2015-04-05") } arg2 <- paste0("/", storageAccount, "/", container, CMD) @@ -126,3 +126,15 @@ refreshStorageKey <- function(azureActiveContext, storageAccount, resourceGroup) azureActiveContext$storageKey } } + + +updateAzureActiveContext <- function(x, storageAccount, storageKey, resourceGroup, container, blob) { + # updates the active azure context in place + if (!is.azureActiveContext(x)) return(FALSE) + if (!missing(storageAccount)) x$storageAccount <- storageAccount + if (!missing(resourceGroup)) x$resourceGroup <- resourceGroup + if (!missing(storageKey)) x$storageKey <- storageKey + if (!missing(container)) x$container <- container + if (!missing(blob)) x$blob <- blob + TRUE +}