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

Commit

Permalink
Add updateAzureActiveContext() function to simplify code #46
Browse files Browse the repository at this point in the history
  • Loading branch information
andrie committed Feb 12, 2017
1 parent 1d3946f commit ee4987f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 52 deletions.
69 changes: 30 additions & 39 deletions R/AzureBlob.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"),
Expand All @@ -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)
Expand Down Expand Up @@ -280,38 +280,30 @@ 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

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)
Expand All @@ -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")
}
Expand Down Expand Up @@ -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,
Expand All @@ -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")
}


Expand Down
38 changes: 25 additions & 13 deletions R/internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit ee4987f

Please sign in to comment.