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

Commit

Permalink
Fix issues with blobs #65
Browse files Browse the repository at this point in the history
  • Loading branch information
andrie committed May 31, 2017
1 parent dbc70c0 commit 27ed568
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 50 deletions.
69 changes: 27 additions & 42 deletions R/AzureBlob.R
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,14 @@ azurePutBlob <- function(azureActiveContext, blob, contents = "", file = "",
if (missing(resourceGroup)) resourceGroup <- azureActiveContext$resourceGroup
if (missing(blob)) blob <- azureActiveContext$blob
if (missing(directory)) directory <- azureActiveContext$directory
if(missing(container)) container <- azureActiveContext$container
} else {
if (missing(directory)) directory <- "/"
if (missing(container)) container <- ""
}
if (missing(container)) container <- azureActiveContext$container
} else {
if (missing(directory)) directory <- "/"
if (missing(container)) container <- ""
}

if (is.null(directory)) directory <- "/"
if (is.null(container)) container <- ""

assert_that(is_resource_group(resourceGroup))
assert_that(is_storage_account(storageAccount))
Expand All @@ -294,18 +297,18 @@ azurePutBlob <- function(azureActiveContext, blob, contents = "", file = "",

verbosity <- set_verbosity(verbose)

if (!grep("/$", directory)) directory <- paste0(directory, "/")
#browser()
if (!grepl("^/", directory)) directory <- paste0("/", directory)
directory <- gsub("//", "/", directory)

blob <- paste0(directory, blob)
blob <- gsub("^/", "", blob)
blob <- gsub("//", "/", blob)
blob <- gsub("//", "/", blob)

if (missing(contents) && missing(file)) stop("Content or file needs to be supplied")

if (!missing(contents) && !missing(file))stop("Provided either Content OR file Argument")


if (missing(storageKey) && !missing(azureActiveContext) && !is.null(azureActiveContext)) {
storageKey <- refreshStorageKey(azureActiveContext, storageAccount, resourceGroup)
}
Expand Down Expand Up @@ -363,9 +366,7 @@ azureBlobFind <- function(azureActiveContext, file, storageAccount, storageKey,
assert_that(is_container(container))
assert_that(is_storage_key(storageKey))

if (missing(file)) {
stop("Error: No filename{pattern} provided")
}
if (missing(file)) stop("Error: No filename{pattern} provided")
verbosity <- set_verbosity(verbose)

F2 <- data.frame()
Expand Down Expand Up @@ -394,7 +395,7 @@ azureBlobFind <- function(azureActiveContext, file, storageAccount, storageKey,
#' @export
azureBlobCD <- function(azureActiveContext, directory, container, file,
storageAccount, storageKey, resourceGroup, verbose = FALSE) {
if (!missing(azureActiveContext)) {
if (!missing(azureActiveContext) && !is.null(azureActiveContext)) {
assert_that(is.azureActiveContext(azureActiveContext))
azureCheckToken(azureActiveContext)
if (missing(storageAccount)) storageAccount <- azureActiveContext$storageAccount
Expand All @@ -414,32 +415,21 @@ azureBlobCD <- function(azureActiveContext, directory, container, file,
assert_that(is_container(container))
assert_that(is_storage_key(storageKey))

if (missing(directory)) {
directory <- azureActiveContext$directory
container <- azureActiveContext$container
if (length(directory) < 1)
directory <- "/" # No previous Dir value
if (length(container) < 1) {
directory <- "/" # No previous Dir value
container <- ""
} else if (container != container)
directory <- "/" # Change of container
if (!grepl("^/", directory)) directory <- paste0("/", directory)
directory <- gsub("//", "/", directory)

if (!missing(azureActiveContext) && !is.null(azureActiveContext)) {
updateAzureActiveContext(azureActiveContext,
storageAccount = storageAccount,
resourceGroup = resourceGroup,
storageKey = storageKey,
container = container,
directory = directory
)
return(paste0("Current directory - ", storageAccount, " > ", container, " : ", directory))
}

storageKey <- refreshStorageKey(azureActiveContext, storageAccount, resourceGroup)

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

if (directory == "../" || directory == "..") {
# Basic attempt azToken relative paths
Expand Down Expand Up @@ -480,15 +470,20 @@ azureBlobCD <- function(azureActiveContext, directory, container, file,

azureDeleteBlob <- function(azureActiveContext, blob, directory,
storageAccount, storageKey, container, resourceGroup, verbose = FALSE) {
if (!missing(azureActiveContext)) {
if (!missing(azureActiveContext) && !is.null(azureActiveContext)) {
assert_that(is.azureActiveContext(azureActiveContext))
azureCheckToken(azureActiveContext)
if (missing(storageAccount)) storageAccount <- azureActiveContext$storageAccount
if (missing(storageKey)) storageKey <- azureActiveContext$storageKey
if (missing(container)) container <- azureActiveContext$container
if (missing(resourceGroup)) resourceGroup <- azureActiveContext$resourceGroup
if (missing(blob)) blob <- azureActiveContext$blob
if (missing(directory)) directory <- azureActiveContext$directory
if (missing(container)) container <- azureActiveContext$container
storageKey <- refreshStorageKey(azureActiveContext, storageAccount, resourceGroup)
} else {
if (missing(directory)) directory <- "/"
if (missing(container)) container <- ""
}
assert_that(is_resource_group(resourceGroup))
assert_that(is_storage_account(storageAccount))
Expand All @@ -498,21 +493,11 @@ azureDeleteBlob <- function(azureActiveContext, blob, directory,

verbosity <- set_verbosity(verbose)

directory <- azureActiveContext$directory
container <- azureActiveContext$container

if (missing(directory)) {
if (length(directory) < 1)
directory <- "" # No previous Dir value
if (length(container) < 1) {
directory <- "" # No previous Dir value
container <- ""
} else if (container != container)
directory <- "" # Change of container
} else directory <- directory
if (is.null(directory)) directory <- "/"
if (is.null(container)) container <- ""

if (nchar(directory) > 0)
directory <- paste0(directory, "/")
if (!grepl("^/", directory)) directory <- paste0("/", directory)
directory <- gsub("//", "/", directory)

blob <- paste0(directory, blob)
blob <- gsub("^/", "", blob)
Expand Down
4 changes: 2 additions & 2 deletions man/azureBlobLS.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions man/azureListStorageBlobs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tests/testthat/test-2-resources.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ test_that("Can create container", {
context(" - blob")
test_that("Can put, list, get and delete a blob", {
skip_if_missing_config(settingsfile)
expect_warning({
res <- azureListStorageBlobs(asc, container = "tempcontainer")
})
expect_warning({
res <- azureListStorageBlobs(asc, container = "tempcontainer")
})
expect_is(res, "data.frame")
expect_equal(ncol(res), 5)
expect_equal(nrow(res), 0)
Expand Down

0 comments on commit 27ed568

Please sign in to comment.