From a658f5602d2bafef3ec06303f7d01a75708f22de Mon Sep 17 00:00:00 2001 From: Scott Chamberlain Date: Tue, 10 Oct 2023 15:56:14 -0700 Subject: [PATCH 1/4] add two sets of fxns, one for buckets, one for files --- DESCRIPTION | 3 +- NAMESPACE | 7 +- R/bucket.R | 119 ++++++++++++++++----------------- R/files.R | 80 ++++++++++++++++++++++ R/globals.R | 2 +- R/utils.R | 19 ++++++ README.md | 9 +-- man/aws_bucket_create.Rd | 21 ++++++ man/aws_bucket_exists.Rd | 25 +++++++ man/aws_bucket_list_objects.Rd | 21 ++++++ man/aws_buckets.Rd | 26 +++++++ man/aws_file_attr.Rd | 28 ++++++++ man/aws_file_download.Rd | 37 ++++++++++ man/aws_file_exists.Rd | 27 ++++++++ man/aws_file_upload.Rd | 50 ++++++++++++++ 15 files changed, 405 insertions(+), 69 deletions(-) create mode 100644 R/files.R create mode 100644 R/utils.R create mode 100644 man/aws_bucket_create.Rd create mode 100644 man/aws_bucket_exists.Rd create mode 100644 man/aws_bucket_list_objects.Rd create mode 100644 man/aws_buckets.Rd create mode 100644 man/aws_file_attr.Rd create mode 100644 man/aws_file_download.Rd create mode 100644 man/aws_file_exists.Rd create mode 100644 man/aws_file_upload.Rd diff --git a/DESCRIPTION b/DESCRIPTION index f1572d6..388d94b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -18,7 +18,8 @@ Imports: paws, purrr, rlang, - tibble + tibble, + fs Suggests: roxyglobals Config/roxyglobals/filename: globals.R diff --git a/NAMESPACE b/NAMESPACE index fd2e29d..5d9b558 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,12 +1,17 @@ # Generated by roxygen2: do not edit by hand +export(aws_bucket_create) +export(aws_bucket_exists) +export(aws_bucket_list_objects) +export(aws_buckets) +export(billing) export(create_user) export(list_users) importFrom(dplyr,mutate) +importFrom(fs,file_exists) importFrom(lubridate,as_datetime) importFrom(magrittr,"%>%") importFrom(paws,iam) -importFrom(paws,s3) importFrom(purrr,list_rbind) importFrom(purrr,map) importFrom(purrr,map_chr) diff --git a/R/bucket.R b/R/bucket.R index d22a0c7..3df518c 100644 --- a/R/bucket.R +++ b/R/bucket.R @@ -1,62 +1,57 @@ -## TODO: not looked over this file yet (commented out below so package can run) - -# sss <- paws::s3() - -# sss$create_bucket(Bucket = "s64-test-3-private", -# CreateBucketConfiguration = list(LocationConstraint = "us-west-2")) - -# sss$put_public_access_block(Bucket = "s64-test-2", -# PublicAccessBlockConfiguration = list( -# BlockPublicPolicy = FALSE, -# RestrictPublicBuckets = FALSE -# )) - -# sss$put_object( -# Body = "~/Desktop/koth.png", -# Bucket = "s64-test-2", -# Key = basename("~/Desktop/koth.png"), -# Tagging = NULL) - -# sss$put_object_acl( -# AccessControlPolicy = structure( -# list(), -# names = character( -# 0 -# ) -# ), -# Bucket = "s64-test-2", -# Key = basename("~/Desktop/koth.png"), -# GrantRead = "uri=http://acs.amazonaws.com/groups/global/AllUsers" -# ) - -# sss$list_objects("s64-test-2") - -# policy <- '{ -# "Version":"2012-10-17", -# "Statement":[{ -# "Sid":"PublicReadGetObject", -# "Effect":"Allow", -# "Principal": "*", -# "Action":"s3:GetObject", -# "Resource":"arn:aws:s3:::s64-test-2/*" -# }] -# }' - -# sss$put_bucket_policy(Bucket = "s64-test-2", Policy = policy) - -# sss$object - -# buckets <- sss$list_buckets() -# buckets[[1]] %>% -# map_chr(~ .x$Name) - -# sss$put_bucket_ownership_controls(Bucket = "s64-test-2", -# OwnershipControls = list( -# Rules = list( -# list( -# ObjectOwnership = "ObjectWriter" -# ) -# ) -# )) - -# sss$get_bucket_ownership_controls(Bucket = "s64-test-2") +#' Create an S3 bucket +#' +#' @export +#' @param bucket (character) bucket name. required +#' @note internally uses [head_bucket](https://www.paws-r-sdk.com/docs/s3_head_bucket/) +#' @examples \dontrun{ +#' # exists +#' aws_bucket_exists(bucket="s64-test-2") +#' # does not exist +#' aws_bucket_exists(bucket="no-bucket") +#' } +aws_bucket_exists <- function(bucket) { + res <- tryCatch({ + env64$s3$head_bucket(Bucket = bucket) + }, error = function(e) e) + !inherits(res, c("error", "error_response")) +} + +#' Create an S3 bucket +#' +#' @export +#' @param bucket (character) bucket name. required +#' @param ... named parameters passed on to [list_objects](https://www.paws-r-sdk.com/docs/s3_create_bucket/) +#' @examples \dontrun{ +#' aws_bucket_create(bucket="s64-test-2") +#' } +aws_bucket_create <- function(bucket, ...) { + env64$s3$create_bucket(Bucket = bucket, + CreateBucketConfiguration = list(LocationConstraint = "us-west-2"), ...) +} + +#' List objects in an S3 bucket +#' +#' @export +#' @param bucket (character) bucket name. required +#' @param ... named parameters passed on to [list_objects](https://www.paws-r-sdk.com/docs/s3_list_objects/) +#' @examples \dontrun{ +#' aws_bucket_list_objects(bucket="s64-test-2") +#' } +aws_bucket_list_objects <- function(bucket, ...) { + env64$s3$list_objects(Bucket = bucket, ...) +} + +#' List S3 buckets +#' +#' @export +#' @param ... named parameters passed on to [list_buckets](https://www.paws-r-sdk.com/docs/s3_list_buckets/) +#' @return tibble with zero or more rows (each an S3 bucket), with two columns: +#' * Name (character) +#' * CreationDate (dttm) +#' @autoglobal +#' @examples \dontrun{ +#' aws_buckets() +#' } +aws_buckets <- function(...) { + env64$s3$list_buckets(...) %>% .$Buckets %>% map(., as_tibble) %>% list_rbind() +} diff --git a/R/files.R b/R/files.R new file mode 100644 index 0000000..d01ed3b --- /dev/null +++ b/R/files.R @@ -0,0 +1,80 @@ +#' Upload a file +#' +#' @importFrom fs file_exists +#' @param bucket (character) an S3 bucket. required +#' @param path (character) a file path to read from or write to. required +#' @param key (character) a key for an object in an S3 `bucket`. required +#' @param ... named parameters passed on to [put_object](https://www.paws-r-sdk.com/docs/s3_put_object/) +#' @details Wraps [put_object](https://www.paws-r-sdk.com/docs/s3_put_object/) +#' @return a tibble with two columns and many rows +#' @details `bucket` parameter: +#' - For upload: if it does exist it will be created +#' - For download: if it does not exist, function will return an error +#' @examples \dontrun{ +#' desc_file <- file.path(system.file(), "DESCRIPTION") +#' aws_file_upload(bucket = "s64-test-2", path = desc_file) +#' +#' # supply a different key +#' aws_file_upload(bucket = "s64-test-2", path = desc_file, key = "d_file") +#' +#' # set expiration, expire 1 minute from now +#' aws_file_upload(bucket = "s64-test-2", path = desc_file, key = "ddd", +#' Expires = Sys.time() + 60) +#' +#' # bucket doesn't exist +#' aws_file_upload(bucket = "not-a-bucket", path = desc_file) +#' # path doesn't exist +#' aws_file_upload(bucket = "s64-test-2", path = "file_doesnt_exist.txt") +#' } +aws_file_upload <- function(bucket, path, key = basename(path), ...) { + stopifnot(fs::file_exists(path)) + if (!aws_bucket_exists(bucket)) aws_bucket_create(bucket) + env64$s3$put_object(Body = path, Bucket = bucket, Key = key, ...) %>% + tibble_transpose() +} + +#' Download a file +#' +#' @inheritParams aws_file_upload +#' @param ... named parameters passed on to [download_file](https://www.paws-r-sdk.com/docs/s3_download_file/) +#' @details Wraps [download_file](https://www.paws-r-sdk.com/docs/s3_download_file/) +#' @return `list` of length 0 +#' @examples \dontrun{ +#' temp_path <- tempfile() +#' aws_file_download(bucket = "s64-test-2", key = "DESCRIPTION", +#' path = temp_path) +#' +#' # S3 key doesn't exist +#' aws_file_download(bucket = "s64-test-2", key = "TESTING123", +#' path = temp_path) +#' } +aws_file_download <- function(bucket, key, path, ...) { + env64$s3$download_file(Bucket = bucket, Key = key, Filename = path, ...) +} + +#' File attributes +#' +#' @inheritParams aws_file_upload +#' @param ... named parameters passed on to [head_object](https://www.paws-r-sdk.com/docs/s3_head_object/) +#' @return `list` of length 0 +#' @examples \dontrun{ +#' aws_file_attr(bucket = "s64-test-2", key = "DESCRIPTION") +#' aws_file_attr(bucket = "s64-test-2", key = "ddd") +#' aws_file_attr(bucket = "s64-test-2", key = "doesntexist") +#' } +aws_file_attr <- function(bucket, key, ...) { + env64$s3$head_object(Bucket = bucket, Key = key, ...) +} + +#' Check if a file exists +#' +#' @inheritParams aws_file_upload +#' @return TRUE or FALSE +#' @examples \dontrun{ +#' aws_file_exists(bucket = "s64-test-2", key = "DESCRIPTION") +#' aws_file_exists(bucket = "s64-test-2", key = "doesntexist") +#' } +aws_file_exists <- function(bucket, key, ...) { + res <- paws_handlr(aws_file_attr(bucket, key, ...)) + !inherits(res, c("error", "error_response")) +} diff --git a/R/globals.R b/R/globals.R index 529ce82..96962fc 100644 --- a/R/globals.R +++ b/R/globals.R @@ -1,7 +1,7 @@ # Generated by roxyglobals: do not edit by hand utils::globalVariables(c( - ".", # + ".", # "CreateDate", # "PasswordLastUsed", # NULL diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 0000000..71130d0 --- /dev/null +++ b/R/utils.R @@ -0,0 +1,19 @@ +tibble_transpose <- function(a_list) { + df <- lapply(a_list, function(x) ifelse(length(x) == 0, NA_character_, x)) %>% + as_tibble(.) + as_tibble(cbind(nms = names(df), t(df))) +} + +paws_handlr <- function(...) { + tryCatch(..., error = function(e) e) +} + +# TODO: maybe use a custom switch to have more useful error messages? +# status_swap <- function(err) { +# dplyr::case_match( +# err$status_code, +# 404 = "Not found", +# 403 = "Not found", +# .default = err$message +# ) +# } diff --git a/README.md b/README.md index 088c875..8311e78 100644 --- a/README.md +++ b/README.md @@ -20,10 +20,11 @@ This package is not on CRAN (yet) ## sixtyfour high level organization -- billing: get AWS billing details -- files: manage files on AWS -- users: manage users on AWS -- database: interact with AWS databases +- `aws_billing`: get AWS billing details +- `aws_bucket*`: manage S3 buckets +- `aws_file_*`: manage files in S3 buckets on AWS +- `aws_user*`: manage users on AWS +- `aws_db*`: interact with AWS databases ## Getting Started diff --git a/man/aws_bucket_create.Rd b/man/aws_bucket_create.Rd new file mode 100644 index 0000000..cf9113a --- /dev/null +++ b/man/aws_bucket_create.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/bucket.R +\name{aws_bucket_create} +\alias{aws_bucket_create} +\title{Create an S3 bucket} +\usage{ +aws_bucket_create(bucket, ...) +} +\arguments{ +\item{bucket}{(character) bucket name. required} + +\item{...}{named parameters passed on to \href{https://www.paws-r-sdk.com/docs/s3_create_bucket/}{list_objects}} +} +\description{ +Create an S3 bucket +} +\examples{ +\dontrun{ +aws_bucket_create(bucket="s64-test-2") +} +} diff --git a/man/aws_bucket_exists.Rd b/man/aws_bucket_exists.Rd new file mode 100644 index 0000000..1300f57 --- /dev/null +++ b/man/aws_bucket_exists.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/bucket.R +\name{aws_bucket_exists} +\alias{aws_bucket_exists} +\title{Create an S3 bucket} +\usage{ +aws_bucket_exists(bucket) +} +\arguments{ +\item{bucket}{(character) bucket name. required} +} +\description{ +Create an S3 bucket +} +\note{ +internally uses \href{https://www.paws-r-sdk.com/docs/s3_head_bucket/}{head_bucket} +} +\examples{ +\dontrun{ +# exists +aws_bucket_exists(bucket="s64-test-2") +# does not exist +aws_bucket_exists(bucket="no-bucket") +} +} diff --git a/man/aws_bucket_list_objects.Rd b/man/aws_bucket_list_objects.Rd new file mode 100644 index 0000000..e7c4035 --- /dev/null +++ b/man/aws_bucket_list_objects.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/bucket.R +\name{aws_bucket_list_objects} +\alias{aws_bucket_list_objects} +\title{List objects in an S3 bucket} +\usage{ +aws_bucket_list_objects(bucket, ...) +} +\arguments{ +\item{bucket}{(character) bucket name. required} + +\item{...}{named parameters passed on to \href{https://www.paws-r-sdk.com/docs/s3_list_objects/}{list_objects}} +} +\description{ +List objects in an S3 bucket +} +\examples{ +\dontrun{ +aws_bucket_list_objects(bucket="s64-test-2") +} +} diff --git a/man/aws_buckets.Rd b/man/aws_buckets.Rd new file mode 100644 index 0000000..50730fd --- /dev/null +++ b/man/aws_buckets.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/bucket.R +\name{aws_buckets} +\alias{aws_buckets} +\title{List S3 buckets} +\usage{ +aws_buckets(...) +} +\arguments{ +\item{...}{named parameters passed on to \href{https://www.paws-r-sdk.com/docs/s3_list_buckets/}{list_buckets}} +} +\value{ +tibble with zero or more rows (each an S3 bucket), with two columns: +\itemize{ +\item Name (character) +\item CreationDate (dttm) +} +} +\description{ +List S3 buckets +} +\examples{ +\dontrun{ +aws_buckets() +} +} diff --git a/man/aws_file_attr.Rd b/man/aws_file_attr.Rd new file mode 100644 index 0000000..1ab4853 --- /dev/null +++ b/man/aws_file_attr.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/files.R +\name{aws_file_attr} +\alias{aws_file_attr} +\title{File attributes} +\usage{ +aws_file_attr(bucket, key, ...) +} +\arguments{ +\item{bucket}{(character) an S3 bucket. required} + +\item{key}{(character) a key for an object in an S3 \code{bucket}. required} + +\item{...}{named parameters passed on to \href{https://www.paws-r-sdk.com/docs/s3_head_object/}{head_object}} +} +\value{ +\code{list} of length 0 +} +\description{ +File attributes +} +\examples{ +\dontrun{ +aws_file_attr(bucket = "s64-test-2", key = "DESCRIPTION") +aws_file_attr(bucket = "s64-test-2", key = "ddd") +aws_file_attr(bucket = "s64-test-2", key = "doesntexist") +} +} diff --git a/man/aws_file_download.Rd b/man/aws_file_download.Rd new file mode 100644 index 0000000..5d8eb0b --- /dev/null +++ b/man/aws_file_download.Rd @@ -0,0 +1,37 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/files.R +\name{aws_file_download} +\alias{aws_file_download} +\title{Download a file} +\usage{ +aws_file_download(bucket, key, path, ...) +} +\arguments{ +\item{bucket}{(character) an S3 bucket. required} + +\item{key}{(character) a key for an object in an S3 \code{bucket}. required} + +\item{path}{(character) a file path to read from or write to. required} + +\item{...}{named parameters passed on to \href{https://www.paws-r-sdk.com/docs/s3_download_file/}{download_file}} +} +\value{ +\code{list} of length 0 +} +\description{ +Download a file +} +\details{ +Wraps \href{https://www.paws-r-sdk.com/docs/s3_download_file/}{download_file} +} +\examples{ +\dontrun{ +temp_path <- tempfile() +aws_file_download(bucket = "s64-test-2", key = "DESCRIPTION", +path = temp_path) + +# S3 key doesn't exist +aws_file_download(bucket = "s64-test-2", key = "TESTING123", +path = temp_path) +} +} diff --git a/man/aws_file_exists.Rd b/man/aws_file_exists.Rd new file mode 100644 index 0000000..4f20451 --- /dev/null +++ b/man/aws_file_exists.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/files.R +\name{aws_file_exists} +\alias{aws_file_exists} +\title{Check if a file exists} +\usage{ +aws_file_exists(bucket, key, ...) +} +\arguments{ +\item{bucket}{(character) an S3 bucket. required} + +\item{key}{(character) a key for an object in an S3 \code{bucket}. required} + +\item{...}{named parameters passed on to \href{https://www.paws-r-sdk.com/docs/s3_put_object/}{put_object}} +} +\value{ +TRUE or FALSE +} +\description{ +Check if a file exists +} +\examples{ +\dontrun{ +aws_file_exists(bucket = "s64-test-2", key = "DESCRIPTION") +aws_file_exists(bucket = "s64-test-2", key = "doesntexist") +} +} diff --git a/man/aws_file_upload.Rd b/man/aws_file_upload.Rd new file mode 100644 index 0000000..cb19c6b --- /dev/null +++ b/man/aws_file_upload.Rd @@ -0,0 +1,50 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/files.R +\name{aws_file_upload} +\alias{aws_file_upload} +\title{Upload a file} +\usage{ +aws_file_upload(bucket, path, key = basename(path), ...) +} +\arguments{ +\item{bucket}{(character) an S3 bucket. required} + +\item{path}{(character) a file path to read from or write to. required} + +\item{key}{(character) a key for an object in an S3 \code{bucket}. required} + +\item{...}{named parameters passed on to \href{https://www.paws-r-sdk.com/docs/s3_put_object/}{put_object}} +} +\value{ +a tibble with two columns and many rows +} +\description{ +Upload a file +} +\details{ +Wraps \href{https://www.paws-r-sdk.com/docs/s3_put_object/}{put_object} + +\code{bucket} parameter: +\itemize{ +\item For upload: if it does exist it will be created +\item For download: if it does not exist, function will return an error +} +} +\examples{ +\dontrun{ +desc_file <- file.path(system.file(), "DESCRIPTION") +aws_file_upload(bucket = "s64-test-2", path = desc_file) + +# supply a different key +aws_file_upload(bucket = "s64-test-2", path = desc_file, key = "d_file") + +# set expiration, expire 1 minute from now +aws_file_upload(bucket = "s64-test-2", path = desc_file, key = "ddd", +Expires = Sys.time() + 60) + +# bucket doesn't exist +aws_file_upload(bucket = "not-a-bucket", path = desc_file) +# path doesn't exist +aws_file_upload(bucket = "s64-test-2", path = "file_doesnt_exist.txt") +} +} From 16af6c89f0b73cd84941dcad418e882d2be3874d Mon Sep 17 00:00:00 2001 From: Scott Chamberlain Date: Tue, 10 Oct 2023 15:58:55 -0700 Subject: [PATCH 2/4] add back import paws::costexplorer and paws::s3 --- NAMESPACE | 2 ++ R/sixtyfour-package.R | 1 + R/users.R | 2 -- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 5d9b558..22aa8a2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,7 +11,9 @@ importFrom(dplyr,mutate) importFrom(fs,file_exists) importFrom(lubridate,as_datetime) importFrom(magrittr,"%>%") +importFrom(paws,costexplorer) importFrom(paws,iam) +importFrom(paws,s3) importFrom(purrr,list_rbind) importFrom(purrr,map) importFrom(purrr,map_chr) diff --git a/R/sixtyfour-package.R b/R/sixtyfour-package.R index dc6c522..5772977 100644 --- a/R/sixtyfour-package.R +++ b/R/sixtyfour-package.R @@ -9,5 +9,6 @@ ## usethis namespace: start #' @importFrom magrittr %>% +#' @importFrom paws s3 iam costexplorer ## usethis namespace: end NULL diff --git a/R/users.R b/R/users.R index 32ff3d2..797bd35 100644 --- a/R/users.R +++ b/R/users.R @@ -22,7 +22,6 @@ user_list_cleanup <- function(x) { #' List Users #' #' @export -#' @importFrom paws iam #' @returns A data frame with information about user accounts. list_users <- function() { env64$iam$list_users()$Users %>% @@ -32,7 +31,6 @@ list_users <- function() { #' Create a User #' #' @export -#' @importFrom paws iam #' @param username A user name create_user <- function(username) { result <- env64$iam$create_user(UserName = username) From 646483fd4f9fd8593a0c9109c6ad6980c9825233 Mon Sep 17 00:00:00 2001 From: Scott Chamberlain Date: Tue, 10 Oct 2023 16:13:50 -0700 Subject: [PATCH 3/4] woopsy, export file functions --- NAMESPACE | 4 ++++ R/files.R | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index 22aa8a2..4ab9e1f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,10 @@ export(aws_bucket_create) export(aws_bucket_exists) export(aws_bucket_list_objects) export(aws_buckets) +export(aws_file_attr) +export(aws_file_download) +export(aws_file_exists) +export(aws_file_upload) export(billing) export(create_user) export(list_users) diff --git a/R/files.R b/R/files.R index d01ed3b..0d47844 100644 --- a/R/files.R +++ b/R/files.R @@ -1,5 +1,6 @@ #' Upload a file #' +#' @export #' @importFrom fs file_exists #' @param bucket (character) an S3 bucket. required #' @param path (character) a file path to read from or write to. required @@ -35,6 +36,7 @@ aws_file_upload <- function(bucket, path, key = basename(path), ...) { #' Download a file #' +#' @export #' @inheritParams aws_file_upload #' @param ... named parameters passed on to [download_file](https://www.paws-r-sdk.com/docs/s3_download_file/) #' @details Wraps [download_file](https://www.paws-r-sdk.com/docs/s3_download_file/) @@ -54,6 +56,7 @@ aws_file_download <- function(bucket, key, path, ...) { #' File attributes #' +#' @export #' @inheritParams aws_file_upload #' @param ... named parameters passed on to [head_object](https://www.paws-r-sdk.com/docs/s3_head_object/) #' @return `list` of length 0 @@ -68,6 +71,7 @@ aws_file_attr <- function(bucket, key, ...) { #' Check if a file exists #' +#' @export #' @inheritParams aws_file_upload #' @return TRUE or FALSE #' @examples \dontrun{ From a234d43b1e9aea919462076d6acdd7d13b42052b Mon Sep 17 00:00:00 2001 From: Scott Chamberlain Date: Mon, 16 Oct 2023 21:57:21 -0700 Subject: [PATCH 4/4] in aws_bucket_create get aws_region from an env var rather than hard-coded; remove commented out fxn in utils.R --- R/bucket.R | 3 ++- R/utils.R | 16 +++++++--------- man/aws_bucket_create.Rd | 3 +++ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/R/bucket.R b/R/bucket.R index 3df518c..4b5e85b 100644 --- a/R/bucket.R +++ b/R/bucket.R @@ -21,12 +21,13 @@ aws_bucket_exists <- function(bucket) { #' @export #' @param bucket (character) bucket name. required #' @param ... named parameters passed on to [list_objects](https://www.paws-r-sdk.com/docs/s3_create_bucket/) +#' @note Requires the env var `AWS_REGION` #' @examples \dontrun{ #' aws_bucket_create(bucket="s64-test-2") #' } aws_bucket_create <- function(bucket, ...) { env64$s3$create_bucket(Bucket = bucket, - CreateBucketConfiguration = list(LocationConstraint = "us-west-2"), ...) + CreateBucketConfiguration = list(LocationConstraint = env_var("AWS_REGION")), ...) } #' List objects in an S3 bucket diff --git a/R/utils.R b/R/utils.R index 71130d0..ade4209 100644 --- a/R/utils.R +++ b/R/utils.R @@ -8,12 +8,10 @@ paws_handlr <- function(...) { tryCatch(..., error = function(e) e) } -# TODO: maybe use a custom switch to have more useful error messages? -# status_swap <- function(err) { -# dplyr::case_match( -# err$status_code, -# 404 = "Not found", -# 403 = "Not found", -# .default = err$message -# ) -# } +# Check for an env var; stop with message if not found +env_var <- function(env_name) { + x <- Sys.getenv(env_name, "") + stop_msg <- sprintf("Environment variable '%s' not found", env_name) + if (identical(x, "")) stop(stop_msg) + return(x) +} diff --git a/man/aws_bucket_create.Rd b/man/aws_bucket_create.Rd index cf9113a..e5579e1 100644 --- a/man/aws_bucket_create.Rd +++ b/man/aws_bucket_create.Rd @@ -14,6 +14,9 @@ aws_bucket_create(bucket, ...) \description{ Create an S3 bucket } +\note{ +Requires the env var \code{AWS_REGION} +} \examples{ \dontrun{ aws_bucket_create(bucket="s64-test-2")