Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add two sets of fxns, one for buckets, one for files #3

Merged
merged 4 commits into from Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Expand Up @@ -18,7 +18,8 @@ Imports:
paws,
purrr,
rlang,
tibble
tibble,
fs
Suggests:
roxyglobals
Config/roxyglobals/filename: globals.R
Expand Down
11 changes: 11 additions & 0 deletions NAMESPACE
@@ -1,10 +1,21 @@
# 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(aws_file_attr)
export(aws_file_download)
export(aws_file_exists)
export(aws_file_upload)
export(billing)
export(create_user)
export(list_users)
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)
Expand Down
119 changes: 57 additions & 62 deletions 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"), ...)
sckott marked this conversation as resolved.
Show resolved Hide resolved
}

#' 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()
}
84 changes: 84 additions & 0 deletions R/files.R
@@ -0,0 +1,84 @@
#' 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
#' @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
#'
#' @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/)
#' @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
#'
#' @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
#' @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
#'
#' @export
#' @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"))
}
2 changes: 1 addition & 1 deletion R/globals.R
@@ -1,7 +1,7 @@
# Generated by roxyglobals: do not edit by hand

utils::globalVariables(c(
".", # <buckets>
".", # <aws_buckets>
"CreateDate", # <user_list_cleanup>
"PasswordLastUsed", # <user_list_cleanup>
NULL
Expand Down
1 change: 1 addition & 0 deletions R/sixtyfour-package.R
Expand Up @@ -9,5 +9,6 @@

## usethis namespace: start
#' @importFrom magrittr %>%
#' @importFrom paws s3 iam costexplorer
## usethis namespace: end
NULL
2 changes: 0 additions & 2 deletions R/users.R
Expand Up @@ -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 %>%
Expand All @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions 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
# )
# }
9 changes: 5 additions & 4 deletions README.md
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions man/aws_bucket_create.Rd

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

25 changes: 25 additions & 0 deletions man/aws_bucket_exists.Rd

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

21 changes: 21 additions & 0 deletions man/aws_bucket_list_objects.Rd

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