gridfs function

GridFS API

GridFS API

Connect to a GridFS database to search, read, write and delete files.

gridfs( db = "test", url = "mongodb://localhost", prefix = "fs", options = ssl_options() )

Arguments

  • db: name of database
  • url: address of the mongodb server in mongo connection string URI format
  • prefix: string to prefix the collection name
  • options: additional connection options such as SSL keys/certs.

Details

We support two interfaces for sending/receiving data from/to GridFS. The fs$read() and fs$write() methods are the most flexible and can send data from/to an R connection, such as a file , socket

or url . These methods support a progress counter and can be interrupted if needed. These methods are recommended for reading or writing single files.

The fs$upload() and fs$download() methods on the other hand copy directly between GridFS and your local disk. This API is vectorized so it can transfer many files at once. However individual transfers cannot be interrupted and will block R until completed. This API is only recommended to upload/download a large number of small files.

Modifying files in GridFS is currently unsupported: uploading a file with the same name will generate a new file.

Methods

  • find(filter = "{}", options = "{}"): Search and list files in the GridFS
  • download(name, path = '.'): Download one or more files from GridFS to disk. Path may be an existing directory or vector of filenames equal to 'name'.
  • upload(path, name = basename(path), content_type = NULL, metadata =NULL): Upload one or more files from disk to GridFS. Metadata is an optional JSON string.
  • read(name, con = NULL, progress = TRUE): Reads a single file from GridFS into a writable R connection . If con is a string it is treated as a filepath; if it is NULL then the output is buffered in memory and returned as a raw vector.
  • write(con, name, content_type = NULL, metadata = NULL, progress = TRUE): Stream write a single file into GridFS from a readable R connection . If con is a string it is treated as a filepath; it may also be a raw vector containing the data to upload. Metadata is an optional JSON string.
  • remove(name): Remove a single file from the GridFS
  • drop(): Removes the entire GridFS collection, including all files

Examples

# Upload a file to GridFS fs <- gridfs(url = "mongodb+srv://readwrite:test@cluster0-84vdt.mongodb.net/test") input <- file.path(R.home('doc'), "html/logo.jpg") fs$upload(input, name = 'logo.jpg') # Download the file back to disk output <- file.path(tempdir(), 'logo1.jpg') fs$download('logo.jpg', output) # Or you can also stream it con <- file(file.path(tempdir(), 'logo2.jpg')) fs$read('logo.jpg', con) # Delete the file on the server fs$remove('logo.jpg') files <- c(input, file.path(tempdir(), c('logo1.jpg', 'logo2.jpg'))) hashes <- tools::md5sum(files) stopifnot(length(unique(hashes)) == 1) ## Not run: # Insert Binary Data fs <- gridfs() buf <- serialize(nycflights13::flights, NULL) fs$write(buf, 'flights') out <- fs$read('flights') flights <- unserialize(out$data) tmp <- file.path(tempdir(), 'flights.rds') fs$download('flights', tmp) flights2 <- readRDS(tmp) stopifnot(all.equal(flights, nycflights13::flights)) stopifnot(all.equal(flights2, nycflights13::flights)) # Show what we have fs$find() fs$drop() ## End(Not run)
  • Maintainer: Jeroen Ooms
  • License: Apache License 2.0
  • Last published: 2025-03-17