file storage, prefix everything

This commit is contained in:
hong-revo 2018-05-14 03:28:36 +10:00
Родитель d32abe97c7
Коммит fd7d1bb920
5 изменённых файлов: 181 добавлений и 37 удалений

Просмотреть файл

@ -1,15 +1,24 @@
# Generated by roxygen2: do not edit by hand
export(az_blob_container)
export(az_blob_endpoint)
export(az_create_blob_container)
export(az_create_file_share)
export(az_delete_blob)
export(az_delete_blob_container)
export(az_delete_file)
export(az_delete_file_share)
export(az_download_blob)
export(az_download_file)
export(az_file_endpoint)
export(az_file_share)
export(az_list_blob_containers)
export(az_list_blobs)
export(az_list_file_shares)
export(az_list_files)
export(az_storage)
export(blob_connection)
export(blob_container)
export(create_blob_container)
export(delete_blob)
export(delete_blob_container)
export(az_upload_blob)
export(az_upload_file)
export(download_azure_blob)
export(download_blob)
export(list_blob_containers)
export(list_blobs)
export(upload_azure_blob)
export(upload_blob)
import(AzureRMR)

Просмотреть файл

@ -1,32 +1,34 @@
#' @export
blob_connection <- function(endpoint, key=NULL, sas=NULL, api_version=getOption("azure_storage_api_version"))
az_blob_endpoint <- function(endpoint, key=NULL, sas=NULL, api_version=getOption("azure_storage_api_version"))
{
if(!grepl(".blob.", endpoint, fixed=TRUE))
stop("Not a blob storage endpoint", call.=FALSE)
obj <- list(endpoint=endpoint, key=key, sas=sas, api_version=api_version)
class(obj) <- "blob_connection"
class(obj) <- "blob_endpoint"
obj
}
#' @export
list_blob_containers <- function(blob_con)
az_list_blob_containers <- function(blob_con)
{
stopifnot(inherits(blob_con, "blob_connection"))
stopifnot(inherits(blob_con, "blob_endpoint"))
lst <- do_storage_call(blob_con$endpoint, "/", options=list(comp="list"),
key=blob_con$key, sas=blob_con$sas, api_version=blob_con$api_version)
lst <- lapply(lst$Containers, function(cont) blob_container(blob_con, cont$Name[[1]]))
lst <- lapply(lst$Containers, function(cont) az_blob_container(blob_con, cont$Name[[1]]))
named_list(lst)
}
#' @export
blob_container <- function(blob_con, name, key=NULL, sas=NULL, api_version=getOption("azure_storage_api_version"))
az_blob_container <- function(blob_con, name, key=NULL, sas=NULL, api_version=getOption("azure_storage_api_version"))
{
if(missing(name) && is_url(blob_con))
{
stor_path <- parse_storage_url(blob_con)
name <- stor_path[2]
blob_con <- blob_connection(stor_path[1], key, sas, api_version)
blob_con <- az_blob_endpoint(stor_path[1], key, sas, api_version)
}
obj <- list(name=name, con=blob_con)
@ -36,14 +38,15 @@ blob_container <- function(blob_con, name, key=NULL, sas=NULL, api_version=getOp
#' @export
create_blob_container <- function(blob_con, name, key=NULL, sas=NULL, api_version=getOption("azure_storage_api_version"),
public_access=c("none", "blob", "container"))
az_create_blob_container <- function(blob_con, name, key=NULL, sas=NULL,
api_version=getOption("azure_storage_api_version"),
public_access=c("none", "blob", "container"))
{
if(missing(name) && is_url(blob_con))
{
stor_path <- parse_storage_url(blob_con)
name <- stor_path[2]
blob_con <- blob_connection(stor_path[1], key, sas, api_version)
blob_con <- az_blob_endpoint(stor_path[1], key, sas, api_version)
}
public_access <- match.arg(public_access)
@ -51,31 +54,30 @@ create_blob_container <- function(blob_con, name, key=NULL, sas=NULL, api_versio
list("x-ms-blob-public-access"=public_access)
else list()
obj <- blob_container(blob_con, name)
obj <- az_blob_container(blob_con, name)
container_op(obj, options=list(restype="container"), headers=headers, http_verb="PUT")
obj
}
#' @export
delete_blob_container <- function(container, confirm=TRUE)
az_delete_blob_container <- function(container, confirm=TRUE)
{
if(confirm && interactive())
{
con <- container$con
path <- paste0(con$endpoint, con$name, "/")
yn <- readline(paste0("Are you sure you really want to delete blob container '", path, "'? (y/N) "))
yn <- readline(paste0("Are you sure you really want to delete the container '", path, "'? (y/N) "))
if(tolower(substr(yn, 1, 1)) != "y")
return(invisible(NULL))
}
container_op(container, options=list(restype="container"), http_verb="DELETE")
invisible(NULL)
}
#' @export
list_blobs <- function(container)
az_list_blobs <- function(container)
{
lst <- container_op(container, options=list(comp="list", restype="container"))
if(is_empty(lst$Blobs))
@ -85,7 +87,7 @@ list_blobs <- function(container)
#' @export
upload_blob <- function(container, src, dest, type="BlockBlob")
az_upload_blob <- function(container, src, dest, type="BlockBlob")
{
body <- readBin(src, "raw", file.info(src)$size)
hash <- openssl::base64_encode(openssl::md5(body))
@ -101,20 +103,20 @@ upload_blob <- function(container, src, dest, type="BlockBlob")
#' @export
download_blob <- function(container, src, dest, overwrite=FALSE)
az_download_blob <- function(container, src, dest, overwrite=FALSE)
{
container_op(container, src, config=httr::write_disk(dest, overwrite))
}
#' @export
delete_blob <- function(container, blob, confirm=TRUE)
az_delete_blob <- function(container, blob, confirm=TRUE)
{
if(confirm && interactive())
{
con <- container$con
path <- paste0(con$endpoint, con$name, blob, "/")
yn <- readline(paste0("Are you sure you really want to delete blob '", path, "'? (y/N) "))
yn <- readline(paste0("Are you sure you really want to delete '", path, "'? (y/N) "))
if(tolower(substr(yn, 1, 1)) != "y")
return(invisible(NULL))
}
@ -123,13 +125,13 @@ delete_blob <- function(container, blob, confirm=TRUE)
}
container_op=function(container, blob="", options=list(), headers=list(), http_verb="GET", ...)
container_op <- function(container, path="", options=list(), headers=list(), http_verb="GET", ...)
{
con <- container$con
path <- paste0(container$name, "/", blob)
do_storage_call(con$endpoint, path, options=options, headers=headers,
key=con$key, sas=con$sas, api_version=con$api_version,
http_verb=http_verb, ...)
path <- sub("//", "/", paste0(container$name, "/", path))
invisible(do_storage_call(con$endpoint, path, options=options, headers=headers,
key=con$key, sas=con$sas, api_version=con$api_version,
http_verb=http_verb, ...))
}

126
R/file_client_funcs.R Normal file
Просмотреть файл

@ -0,0 +1,126 @@
#' @export
az_file_endpoint <- function(endpoint, key=NULL, sas=NULL, api_version=getOption("azure_storage_api_version"))
{
if(!grepl(".file.", endpoint, fixed=TRUE))
stop("Not a file storage endpoint", call.=FALSE)
obj <- list(endpoint=endpoint, key=key, sas=sas, api_version=api_version)
class(obj) <- "file_endpoint"
obj
}
#' @export
az_list_file_shares <- function(fs_con)
{
stopifnot(inherits(fs_con, "file_endpoint"))
lst <- do_storage_call(fs_con$endpoint, "/", options=list(comp="list"),
key=fs_con$key, sas=fs_con$sas, api_version=fs_con$api_version)
lst <- lapply(lst$Shares, function(cont) az_file_share(fs_con, cont$Name[[1]]))
named_list(lst)
}
#' @export
az_file_share <- function(fs_con, name, key=NULL, sas=NULL, api_version=getOption("azure_storage_api_version"))
{
if(missing(name) && is_url(fs_con))
{
stor_path <- parse_storage_url(fs_con)
name <- stor_path[2]
fs_con <- az_file_endpoint(stor_path[1], key, sas, api_version)
}
obj <- list(name=name, con=fs_con)
class(obj) <- "file_share"
obj
}
#' @export
az_create_file_share <- function(fs_con, name, key=NULL, sas=NULL, api_version=getOption("azure_storage_api_version"))
{
if(missing(name) && is_url(fs_con))
{
stor_path <- parse_storage_url(fs_con)
name <- stor_path[2]
fs_con <- az_file_endpoint(stor_path[1], key, sas, api_version)
}
obj <- az_file_share(fs_con, name)
container_op(obj, options=list(restype="share"), http_verb="PUT")
obj
}
#' @export
az_delete_file_share <- function(share, confirm=TRUE)
{
if(confirm && interactive())
{
con <- container$con
path <- paste0(con$endpoint, con$name, "/")
yn <- readline(paste0("Are you sure you really want to delete the share '", path, "'? (y/N) "))
if(tolower(substr(yn, 1, 1)) != "y")
return(invisible(NULL))
}
container_op(share, options=list(restype="share"), http_verb="DELETE")
}
#' @export
az_list_files <- function(share, dir)
{
lst <- container_op(share, dir, options=list(comp="list", restype="directory"))
if(is_empty(lst$Entries))
list()
else unname(sapply(lst$Entries, function(b) b$Name[[1]]))
}
#' @export
az_upload_file <- function(share, src, dest)
{
body <- readBin(src, "raw", file.info(src)$size)
# first, create the file
headers <- list("x-ms-type"="file",
"x-ms-content-length"=length(body))
container_op(share, dest, headers=headers, http_verb="PUT")
# then write the bytes into it
hash <- openssl::base64_encode(openssl::md5(body))
options <- list(comp="range")
headers <- list("content-length"=length(body),
"range"=paste0("bytes=0-", length(body) - 1),
"content-md5"=hash,
"content-type"="application/octet-stream",
"x-ms-write"="Update")
container_op(share, dest, options=options, headers=headers, body=body, http_verb="PUT")
}
#' @export
az_download_file <- function(share, src, dest, overwrite=FALSE)
{
container_op(share, src, config=httr::write_disk(dest, overwrite))
}
#' @export
az_delete_file <- function(share, file, confirm=TRUE)
{
if(confirm && interactive())
{
con <- share$con
path <- paste0(con$endpoint, con$name, file, "/")
yn <- readline(paste0("Are you sure you really want to delete '", path, "'? (y/N) "))
if(tolower(substr(yn, 1, 1)) != "y")
return(invisible(NULL))
}
container_op(share, file, http_verb="DELETE")
}

Просмотреть файл

@ -50,9 +50,14 @@ public=list(
az_file_client$new(self$properties$primaryEndpoints$file, key=key)
},
get_blob_connection=function(key=self$list_keys()[1])
get_blob_endpoint=function(key=self$list_keys()[1])
{
blob_connection(self$properties$primaryEndpoints$blob, key=key)
az_blob_endpoint(self$properties$primaryEndpoints$blob, key=key)
},
get_file_endpoint=function(key=self$list_keys()[1])
{
az_file_endpoint(self$properties$primaryEndpoints$file, key=key)
}
),

Просмотреть файл

@ -27,7 +27,6 @@ do_storage_call <- function(endpoint, path, options=list(), headers=list(), body
}
verb <- get(verb, getNamespace("httr"))
response <- verb(url, headers, body=body, ...)
handler <- match.arg(http_status_handler)
@ -44,7 +43,9 @@ do_storage_call <- function(endpoint, path, options=list(), headers=list(), body
cont <- suppressMessages(httr::content(response))
if(is_empty(cont))
NULL
else xml2::as_list(cont)
else if(inherits(cont, "xml_node"))
xml2::as_list(cont)
else cont
}
else response
}
@ -73,6 +74,7 @@ make_signature <- function(key, verb, acct_name, resource, options, headers)
names(headers) <- tolower(names(headers))
ms_headers <- headers[grepl("^x-ms", names(headers))]
ms_headers <- ms_headers[order(names(ms_headers))]
ms_headers <- paste(names(ms_headers), ms_headers, sep=":", collapse="\n")
options <- paste(names(options), options, sep=":", collapse="\n")