# NOT RUN {
# get an object in memory
## create bucket
b <- put_bucket("myexamplebucket")
## save a dataset to the bucket
s3save(mtcars, bucket = b, object = "mtcars")
obj <- get_bucket(b)
## get the object in memory
x <- get_object(obj[[1]])
load(rawConnection(x))
"mtcars" %in% ls()
# save an object locally
y <- save_object(obj[[1]], file = object[[1]][["Key"]])
y %in% dir()
# return object using 'S3 URI' syntax, with progress bar
get_object("s3://myexamplebucket/mtcars", show_progress = TRUE)
# return parts of an object
## use 'Range' header to specify bytes
get_object(object = obj[[1]], headers = list('Range' = 'bytes=1-120'))
# example of streaming connection
## setup a bucket and object
b <- put_bucket("myexamplebucket")
s3write_using(mtcars, bucket = b, object = "mtcars.csv", FUN = utils::write.csv)
## setup the connection
con <- s3connection("mtcars.csv", bucket = b)
## line-by-line read
while(length(x <- readLines(con, n = 1L))) {
print(x)
}
## use data.table::fread without saving object to file
library(data.table)
s3write_using(as.data.table(mtcars), bucket = b, object = "mtcars2.csv", FUN = data.table::fwrite)
fread(get_object("mtcars2.csv", bucket = b, as = "text"))
## cleanup
close(con)
delete_bucket("myexamplebucket")
# }
Run the code above in your browser using DataLab