-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3_transfer.R
250 lines (222 loc) · 7.14 KB
/
s3_transfer.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#' @title Upload a local file to S3
#'
#' @description Uploads a local file from the project's directory to its
#' corresponding location within the project's S3 root folder.
#'
#' @inheritParams doc_file
#' @inheritParams cloud_s3_ls
#'
#' @return Invisibly returns `NULL` after successfully uploading the file.
#'
#' @examplesIf interactive()
#' # create a toy csv file
#' dir.create("toy_data")
#' write.csv(mtcars, "toy_data/mtcars.csv")
#'
#' # uploads toy_data/mtcars.csv to 'data' subfolder of project's S3 folder
#' cloud_s3_upload("toy_data/mtcars.csv")
#'
#' # clean up
#' unlink("toy_data", recursive = TRUE)
#'
#' @export
cloud_s3_upload <- function(file, root = NULL) {
check_path(file)
check_string(root, alt_null = TRUE)
if (is.null(root)) root <- cloud_s3_get_root()
full_path <- file.path(root, file)
bucket_prefix <- s3_path_to_bucket_prefix(full_path)
s3_file_path <- file.path(root, file)
if (!file.exists(file)) {
cli::cli_abort("Can't find {.path {file}}.")
}
aws.s3::put_object(
bucket = bucket_prefix$bucket,
file = file,
object = bucket_prefix$prefix,
multipart = TRUE
)
cli::cli_alert_success(
"File {.path {file}} uploaded to S3 root {.field {root}}."
)
invisible(NULL)
}
#' @title Download a file from S3 to the local project folder
#'
#' @description Retrieves a file from the project's S3 root folder and saves it
#' to the local project folder, maintaining the original folder structure.
#'
#' @inheritParams doc_file
#' @inheritParams cloud_s3_ls
#'
#' @return Invisibly returns `NULL` after successfully downloading the file.
#'
#' @examplesIf interactive()
#' # downloads toy_data/demo.csv from project's S3 folder (provided it exists)
#' # and saves it to local 'toy_data' folder
#' cloud_s3_download("toy_data/demo.csv")
#'
#' # clean up
#' unlink("toy_data", recursive = TRUE)
#'
#' @export
cloud_s3_download <- function(file, root = NULL) {
check_path(file)
check_string(root, alt_null = TRUE)
if (is.null(root)) root <- cloud_s3_get_root()
full_path <- file.path(root, file)
bucket_prefix <- s3_path_to_bucket_prefix(full_path)
aws.s3::save_object(
bucket = bucket_prefix$bucket,
object = bucket_prefix$prefix,
file = file
)
cli::cli_alert_success(
"File {.path {file}} downloaded from S3 root {.field {root}}."
)
invisible(NULL)
}
#' @title Write an object to S3
#'
#' @description Saves an R object to a designated location in the project's
#' S3 storage. If no custom writing function is specified, the function will
#' infer the appropriate writing method based on the file's extension.
#'
#' @inheritParams doc_file
#' @inheritParams cloud_s3_ls
#' @inheritParams doc_local
#'
#' @param x An R object to be written to S3.
#' @param fun A custom writing function. If `NULL` (default), the appropriate
#' writing function will be inferred based on the file's extension.
#' @param ... Additional arguments to pass to the writing function `fun`.
#'
#' @inheritSection cloud_guess_write_fun Default writing functions
#'
#' @return Invisibly returns `NULL` after successfully writing the object to S3.
#'
#' @examplesIf interactive()
#' # write mtcars dataframe to mtcars.csv in data folder
#' cloud_s3_write(mtcars, "data/mtcars.csv")
#' cloud_s3_write(random_forest, "models/random_forest.rds")
#'
#' # provide custom writing function with parameters
#' cloud_s3_write(c("one", "two"), "text/count.txt", writeLines, sep = "\n\n")
#'
#' @export
cloud_s3_write <- function(x, file, fun = NULL, ..., local = FALSE,
root = NULL) {
check_path(file)
check_bool(local)
check_class(fun, "function", alt_null = TRUE)
check_string(root, alt_null = TRUE)
if (is.null(fun)) {
fun <- cloud_guess_write_fun(file)
}
if (is.null(root)) root <- cloud_s3_get_root()
full_path <- file.path(root, file)
bucket_prefix <- s3_path_to_bucket_prefix(full_path)
if (local) {
local_file <- file
} else {
local_file <- tempfile(fileext = paste0(".", tools::file_ext(file)))
}
local_dir <- dirname(local_file)
if (!dir.exists(local_dir)) dir.create(local_dir, recursive = TRUE)
fun(x, local_file, ...)
aws.s3::put_object(
file = local_file,
bucket = bucket_prefix$bucket,
object = bucket_prefix$prefix,
multipart = TRUE
)
if (!local) {unlink(local_file)}
cli::cli_alert_success(
"Written to {.path {file}} in S3 root {.field {root}}."
)
invisible(NULL)
}
#' @title Read a file from S3
#'
#' @description Retrieves and reads a file from the project's S3 folder. By
#' default, the function attempts to determine the appropriate reading
#' function based on the file's extension. However, you can specify a custom
#' reading function if necessary.
#'
#' @inheritParams doc_file
#' @inheritParams cloud_s3_ls
#'
#' @param fun A custom reading function. If `NULL` (default), the appropriate
#' reading function will be inferred based on the file's extension.
#' @param ... Additional arguments to pass to the reading function `fun`.
#'
#' @return The content of the file read from S3, with additional attributes
#' containing metadata about the file.
#'
#' @inheritSection cloud_guess_read_fun Default reading functions
#'
#' @examplesIf interactive()
#' # provided there are folders called "data" and "models" in the root of your
#' # project's main S3 folder and they contain the files mentioned below
#' cloud_s3_read("data/mtcars.csv")
#' cloud_s3_read("models/random_forest.rds")
#' cloud_s3_read("data/dm.sas7bdat", fun = haven::read_sas)
#'
#' @export
cloud_s3_read <- function(file, fun = NULL, ..., root = NULL) {
check_path(file)
check_string(root, alt_null = TRUE)
if (is.null(fun)) {
fun <- cloud_guess_read_fun(file)
}
if (is.null(root)) root <- cloud_s3_get_root()
full_path <- file.path(root, file)
bucket_prefix <- s3_path_to_bucket_prefix(full_path)
cli::cli_alert_info(
"Trying to read {.path {file}} from S3 root {.field {root}}."
)
res <-
aws.s3::s3read_using(
bucket = bucket_prefix$bucket,
object = bucket_prefix$prefix,
FUN = fun,
...
)
meta <- cloud_s3_get_obj_meta(bucket_prefix$bucket, bucket_prefix$prefix)
for (n in names(meta)) {
attr(res, n) <- meta[[n]]
cli::cli_text("{.field {n}}: {.val {meta[[n]]}}")
}
res
}
#' @title Obtain relevant meta from an S3 object
#'
#' @description Given a file path on the 'bucket-name' bucket returns a list
#' with it's meta information like e.g. creation date.
#'
#' @noRd
cloud_s3_get_obj_meta <- function(bucket, prefix) {
check_string(bucket)
check_string(prefix)
obj <-
aws.s3::get_bucket(
bucket = bucket,
prefix = prefix,
delimiter = "/"
)
if (length(obj) == 0) cli::cli_abort(
"S3 request for {.path {prefix}} in {.path {bucket}} bucket did not \\
return any results."
)
if (length(obj) > 1) cli::cli_abort(
"S3 request for {.path {prefix}} in {.path {bucket}} bucket returned \\
more than one result."
)
lm_char <- obj[[1]]$LastModified
lm <- as.POSIXct(lm_char, format = "%Y-%m-%dT%H:%M:%OS", tz = "EST")
list(
cloud = "S3",
key = obj[[1]]$Key,
last_modified = lm
)
}