class

Mongo::GridFS::Bucket

Inherits Mongo::GridFS::Bucket::Internal / Reference / Object

A configured GridFS bucket instance.

Constructors

new(db : Database, *, bucket_name : String = "fs", chunk_size_bytes : Int32 = 255 * 1024, write_concern : WriteConcern | Nil = nil, read_concern : ReadConcern | Nil = nil, read_preference : ReadPreference | Nil = nil)

Creates a new GridFSBucket object, managing a GridFS bucket within the given database.

Source

Instance methods

delete(id : FileID) : Nil forall FileID

Given an id, delete this stored file’s files collection document and associated chunks from a GridFS bucket.

gridfs = client["database"].grid_fs
id = BSON::ObjectId.new("5eed35600000000000000000")
gridfs.delete(id)
Source
download_to_stream(id : FileID, destination : IO) : Nil forall FileID

Downloads the contents of the stored file specified by id and writes the contents to the destination Stream.

gridfs = client["database"].grid_fs
stream = IO::Memory.new
id = BSON::ObjectId.new("5eed35600000000000000000")
gridfs.download_to_stream(id, stream)
puts stream.rewind.gets_to_end
Source
download_to_stream_by_name(filename : String, destination : IO, revision : Int32 = -1) : Nil

Downloads the contents of the stored file specified by filename and by an optional revision and writes the contents to the destination IO stream.

See: open_download_stream_by_name for how the revision is calculated.

gridfs = client["database"].grid_fs
io = IO::Memory.new
gridfs.download_to_stream_by_name("file", io, revision: -1)
puts io.to_s
Source
drop

Drops the files and chunks collections associated with this bucket.

gridfs = client["database"].grid_fs
gridfs.drop
Source
find(filter = BSON.new, *, allow_disk_use : Bool | Nil = nil, batch_size : Int32 | Nil = nil, limit : Int32 | Nil = nil, max_time_ms : Int64 | Nil = nil, no_cursor_timeout : Bool | Nil = nil, skip : Int32 | Nil = nil, sort = nil) : Cursor::Wrapper(File(BSON::Value))

Find and return the files collection documents that match filter.

gridfs = client["database"].grid_fs
gridfs.find({
  length: {"$gte": 5000},
})
Source
open_download_stream(id : FileID) : IO forall FileID

Opens a Stream from which the application can read the contents of the stored file specified by id.

Returns a IO stream.

gridfs = client["database"].grid_fs
id = BSON::ObjectId.new("5eed35600000000000000000")
stream = gridfs.open_download_stream(id)
puts stream.gets_to_end
stream.close
Source
open_download_stream_by_name(filename : String, revision : Int32 = -1) : IO

Opens a IO stream from which the application can read the contents of the stored file specified by filename and an optional revision.

Returns a IO stream.

NOTE: It is the responsbility of the caller to close the stream.

gridfs = client["database"].grid_fs
stream = gridfs.open_download_stream_by_name("file", revision: 2)
puts stream.gets_to_end
stream.close

About the the revision argument:

Specifies which revision (documents with the same filename and different uploadDate) of the file to retrieve. Defaults to -1 (the most recent revision).

Revision numbers are defined as follows:

  • 0 = the original stored file
  • 1 = the first revision
  • 2 = the second revision

etc…

  • -2 = the second most recent revision
  • -1 = the most recent revision
Source
open_upload_stream(filename : String, *, id = nil, chunk_size_bytes : Int32 | Nil = nil, metadata = nil) : IO

Opens an IO stream that the caller can write the contents of the file to.

NOTE: It is the responsbility of the caller to flush and close the stream.

gridfs = client["database"].grid_fs
io = gridfs.open_upload_stream(filename: "file.txt", chunk_size_bytes: 1024, metadata: {hello: "world"})
io << "some" << "text"
io.flush
io.close
sleep 1
Source
open_upload_stream(filename : String, *, id : FileID = nil, chunk_size_bytes : Int32 | Nil = nil, metadata = nil, &) forall FileID

Yields an IO stream that the caller can write the contents of the file to.

NOTE: Will flush and close the stream after the block gets executed.

gridfs = client["database"].grid_fs
gridfs.open_upload_stream(filename: "file.txt", chunk_size_bytes: 1024, metadata: {hello: "world"}) { |io|
  io << "some text"
}
sleep 1
Source
rename(id : FileID, new_filename : String) : Nil forall FileID

Renames the stored file with the specified id.

gridfs = client["database"].grid_fs
id = BSON::ObjectId.new("5eed35600000000000000000")
gridfs.rename(id, new_filename: "new_name.txt")
Source
upload_from_stream(filename : String, stream : IO, *, id : FileID = nil, chunk_size_bytes : Int32 | Nil = nil, metadata = nil) forall FileID

Uploads a user file to a GridFS bucket.

The application supplies a custom file id or the driver will generate the file id.

Reads the contents of the user file from the source Stream and uploads it as chunks in the chunks collection. After all the chunks have been uploaded, it creates a files collection document for filename in the files collection.

Returns the id of the uploaded file.

NOTE: It is the responsbility of the caller to flush and close the stream.

gridfs = client["database"].grid_fs
file = File.new("file.txt")
id = gridfs.upload_from_stream("file.txt", file)
file.close
puts id
Source