class Mongo::GridFS::Bucket

Overview

A configured GridFS bucket instance.

Included Modules

Defined in:

cryomongo/gridfs.cr

Constructors

Instance Method Summary

Instance methods inherited from module Mongo::GridFS::Bucket::Internal

bucket bucket, check_collection_index(collection, keys) check_collection_index, check_indexes(bucket, chunks) check_indexes, chunk_count(file : File(FileID)) : Int64 forall FileID chunk_count, chunks chunks, fill_slice(io : IO, slice : Bytes) fill_slice, get_chunk(id : FileID, n : Int64) forall FileID get_chunk, get_file(id : FileID) : File(FileID) forall FileID get_file, get_file_by_name(name : String, revision : Int32 = -1) : File(BSON::Value) get_file_by_name, integrity_check!(file : File, chunk : Chunk, remaining : Int64) integrity_check!

Constructor Detail

def self.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.


Instance Method Detail

def 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)

def 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

def 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

def drop #

Drops the files and chunks collections associated with this bucket.

gridfs = client["database"].grid_fs
gridfs.drop

def 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},
})

def 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

def 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

def 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

def 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

def 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")

def 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