neosqlite.gridfs.gridfs_legacy module

class neosqlite.gridfs.gridfs_legacy.GridFS(db: Connection, collection_name: str = 'fs')[source]

Bases: object

A legacy GridFS interface for storing and retrieving files in SQLite.

This class provides the legacy PyMongo-compatible GridFS interface, which is simpler to use than the GridFSBucket API but less flexible.

__init__(db: Connection, collection_name: str = 'fs')[source]

Initialize a new GridFS instance.

Parameters:
  • db – SQLite database connection

  • collection_name – The collection name for the GridFS files (default: “fs”)

put(data: bytes | IOBase, filename: str | None = None, **kwargs: Any) ObjectId[source]

Put data into GridFS.

Parameters:
  • data – The data to store (bytes or file-like object)

  • filename – The filename to use (optional)

  • **kwargs – Additional metadata fields

Returns:

The ObjectId of the stored file document

get(file_id: ObjectId | str | int) GridOut[source]

Get a file from GridFS by its _id.

Parameters:

file_id – The _id of the file to retrieve (ObjectId, hex string, or integer ID)

Returns:

A GridOut instance for reading the file

get_version(filename: str, version: int = -1) GridOut[source]

Get a file from GridFS by filename and version.

Parameters:
  • filename – The name of the file to retrieve

  • version – The version number (-1 for latest, 0 for first, etc.)

Returns:

A GridOut instance for reading the file

get_last_version(filename: str) GridOut[source]

Get the most recent version of a file from GridFS by filename.

Parameters:

filename – The name of the file to retrieve

Returns:

A GridOut instance for reading the file

delete(file_id: ObjectId | str | int) None[source]

Delete a file from GridFS by its _id.

Parameters:

file_id – The _id of the file to delete (ObjectId, hex string, or integer ID)

list() list[source]

List all filenames in GridFS.

Returns:

A list of filenames

find(filter: dict[str, Any] | None = None) GridOutCursor[source]

Find files in GridFS that match the filter.

Parameters:

filter – The filter to apply when searching for files

Returns:

A GridOutCursor instance

find_one(filter: dict[str, Any] | None = None) GridOut | None[source]

Find a single file in GridFS that matches the filter.

Parameters:

filter – The filter to apply when searching for files

Returns:

A GridOut instance for reading the file, or None if not found

exists(file_id: ObjectId | str | int | None = None, **kwargs: Any) bool[source]

Check if a file exists in GridFS.

Parameters:
  • file_id – The _id of the file to check (ObjectId, hex string, or integer ID)

  • **kwargs – Additional filter criteria (e.g., filename=”test.txt”)

Returns:

True if the file exists, False otherwise

new_file(**kwargs: Any) GridIn[source]

Create a new file in GridFS and return a GridIn instance to which data can be written.

Parameters:

**kwargs – Arguments to pass to the GridIn constructor (e.g., filename, metadata)

Returns:

A GridIn instance for writing the file contents