Skip to content

Serialises x according to the key's extension (or format) and uploads it:

Usage

write_s3(
  x,
  key,
  bucket = NULL,
  ...,
  format = NULL,
  content_type = NULL,
  metadata = NULL,
  headers = NULL,
  config = s3_config()
)

Arguments

x

The object to write.

key

Object key inside bucket, or an s3://bucket/key URL.

bucket

Bucket; defaults to the config's bucket (S3PE_BUCKET).

...

Passed to the writer (for example pretty = TRUE for JSON, na = "" for CSV).

format

One of "csv", "tsv", "json", "geojson", "parquet", "rds", "xlsx", "text", "raw"; overrides the extension.

content_type

Content type to store with the object.

metadata

Named character vector of user metadata, stored with the object as x-amz-meta-<name> and returned by head_s3().

headers

Named character vector of extra headers to send with the upload: Cache-Control, Content-Disposition, Content-Encoding, x-amz-storage-class, x-amz-server-side-encryption, ... (x-amz-* headers are signed).

config

An s3_config().

Value

Invisibly, a one-row tibble: bucket, key, size (bytes), etag, elapsed (seconds).

Details

ExtensionWriterAccepts
.csv, .csv.gzreadr::write_csv()data frame
.tsv, .tsv.gzreadr::write_tsv()data frame
.jsonjsonlite::toJSON() (auto_unbox = TRUE, digits = NA)anything jsonlite can encode
.geojsonsf::st_write() for sf objects, else jsonlitesf or list
.parquetnanoparquet::write_parquet()data frame
.rdssaveRDS()anything
.txt, .md, .html, ...character (joined by newlines)
anything elseraw vector, or character as text

The content type is set from the extension unless content_type is given. After the upload the server's ETag is compared with the MD5 of what was sent. Bodies larger than the config's multipart_threshold (64 MiB by default) go up as a multipart upload, part by part, aborted on any failure.

Examples

if (FALSE) { # interactive()
write_s3(mtcars, "report/mtcars.csv", bucket = "my-bucket")
write_s3(list(generated_at = Sys.time()), "report/manifest.json", bucket = "my-bucket")
write_s3(readBin("figure1.png", "raw", 1e7), "s3://my-bucket/report/figure1.png")
write_s3(mtcars, "report/mtcars.csv", bucket = "my-bucket",
         metadata = c(author = "ana"), headers = c("Cache-Control" = "max-age=3600"))
}