|
1 |
| -.. uses gridfs.rst |
| 1 | +.. _pymongo-gridfs: |
2 | 2 |
|
3 |
| -GridFS Example |
4 |
| -============== |
| 3 | +GridFS |
| 4 | +====== |
5 | 5 |
|
6 |
| -.. code-block:: python |
7 |
| - |
8 |
| - from pymongo import MongoClient |
9 |
| - |
10 |
| - client = MongoClient() |
11 |
| - client.drop_database("gridfs_example") |
| 6 | +.. contents:: On this page |
| 7 | + :local: |
| 8 | + :backlinks: none |
| 9 | + :depth: 1 |
| 10 | + :class: singlecol |
12 | 11 |
|
13 |
| -This example shows how to use ``gridfs`` to store large binary |
14 |
| -objects (e.g. files) in MongoDB. |
| 12 | +.. facet:: |
| 13 | + :name: genre |
| 14 | + :values: reference |
| 15 | + |
| 16 | +.. meta:: |
| 17 | + :keywords: binary large object, blob, file, storage |
15 | 18 |
|
16 |
| -.. seealso:: The API docs for ``gridfs``. |
17 |
| - |
18 |
| -.. seealso:: `This blog post |
19 |
| - <http://dirolf.com/2010/03/29/new-gridfs-implementation-for-pymongo.html>`_ |
20 |
| - for some motivation behind this API. |
| 19 | +This guide shows how to use GridFS to store and retrieve large files |
| 20 | +in MongoDB. |
21 | 21 |
|
22 | 22 | Setup
|
23 | 23 | -----
|
24 | 24 |
|
25 |
| -We start by creating a ``~gridfs.GridFS`` instance to use: |
| 25 | +Each ``GridFS`` instance operates on a specific MongoDB database. |
| 26 | +To create a ``GridFS`` instance, call the ``GridFS()`` |
| 27 | +constructor and pass in a MongoDB database: |
26 | 28 |
|
27 | 29 | .. code-block:: python
|
28 | 30 |
|
29 |
| - >>> from pymongo import MongoClient |
30 |
| - >>> import gridfs |
31 |
| - >>> |
32 |
| - >>> db = MongoClient().gridfs_example |
33 |
| - >>> fs = gridfs.GridFS(db) |
34 |
| - |
35 |
| -Every ``~gridfs.GridFS`` instance is created with and will |
36 |
| -operate on a specific ``~pymongo.database.Database`` instance. |
| 31 | + >>> from pymongo import MongoClient |
| 32 | + >>> import gridfs |
| 33 | + >>> |
| 34 | + >>> db = MongoClient().gridfs_example |
| 35 | + >>> fs = gridfs.GridFS(db) |
37 | 36 |
|
38 | 37 | Saving and Retrieving Data
|
39 | 38 | --------------------------
|
40 | 39 |
|
41 |
| -The simplest way to work with ``gridfs`` is to use its key/value |
42 |
| -interface (the the ``~gridfs.GridFS.put`` method and |
43 |
| -the ``~gridfs.GridFS.get`` method methods). To write data to GridFS, use |
44 |
| -the ``~gridfs.GridFS.put`` method: |
| 40 | +GridFS offers a key-value-pair interface through the ``~gridfs.GridFS.put()`` and |
| 41 | +``~gridfs.GridFS.get()`` methods. |
| 42 | + |
| 43 | +To write data to GridFS, use the ``~gridfs.GridFS.put()`` method: |
45 | 44 |
|
46 | 45 | .. code-block:: python
|
47 | 46 |
|
48 |
| - >>> a = fs.put(b"hello world") |
| 47 | + >>> a = fs.put("hello world") |
49 | 48 |
|
50 |
| -the ``~gridfs.GridFS.put`` method creates a new file in GridFS, and returns |
51 |
| -the value of the file document's ``"_id"`` key. Given that ``"_id"`` |
52 |
| -we can use the ``~gridfs.GridFS.get`` method to get back the contents of the |
53 |
| -file: |
| 49 | +The ``~gridfs.GridFS.put()`` method creates a new file in GridFS, and returns |
| 50 | +the value of the file's ``_id`` key. You can use the file's returned ``_id`` |
| 51 | +value to retrieve the contents of the file: |
54 | 52 |
|
55 | 53 | .. code-block:: python
|
56 | 54 |
|
57 |
| - >>> fs.get(a).read() |
58 |
| - b'hello world' |
| 55 | + >>> fs.get(a).read() |
| 56 | + 'hello world' |
59 | 57 |
|
60 |
| -the ``~gridfs.GridFS.get`` method returns a file-like object, so we get the |
61 |
| -file's contents by calling the ``~gridfs.grid_file.GridOut.read`` method. |
| 58 | +The ``~gridfs.GridFS.get()`` method returns a file-like object. To read the |
| 59 | +file's contents, call the ``~gridfs.grid_file.GridOut.read()`` method. You can store |
| 60 | +any object that has a ``read()`` method in GridFS. |
62 | 61 |
|
63 |
| -In addition to putting a ``str`` as a GridFS file, we can also |
64 |
| -put any file-like object (an object with a the ``read`` method |
65 |
| -method). GridFS will handle reading the file in chunk-sized segments |
66 |
| -automatically. We can also add additional attributes to the file as |
67 |
| -keyword arguments: |
| 62 | +The following code example shows how to add additional attributes to a file. |
| 63 | +To add an attribute, pass it as a named argument to the ``~gridfs.GridFS.put()`` method. |
| 64 | +To retrieve an attribute's value, call |
| 65 | +the ``~gridfs.GridFS.get()`` method to retrieve the file-like object, |
| 66 | +then use the attribute name to access each value on this object. |
68 | 67 |
|
69 | 68 | .. code-block:: python
|
70 | 69 |
|
71 |
| - >>> b = fs.put(fs.get(a), filename="foo", bar="baz") |
72 |
| - >>> out = fs.get(b) |
73 |
| - >>> out.read() |
74 |
| - b'hello world' |
75 |
| - >>> out.filename |
76 |
| - 'foo' |
77 |
| - >>> out.bar |
78 |
| - 'baz' |
79 |
| - >>> out.upload_date |
80 |
| - datetime.datetime(...) |
81 |
| - |
82 |
| -The attributes we set in the ``~gridfs.GridFS.put`` method are stored in the |
83 |
| -file document, and retrievable after calling |
84 |
| -the ``~gridfs.GridFS.get`` method. Some attributes (like ``"filename"``) are |
85 |
| -special and are defined in the GridFS specification - see that |
86 |
| -document for more details. |
| 70 | + >>> b = fs.put(fs.get(a), filename="foo", bar="baz") |
| 71 | + >>> out = fs.get(b) |
| 72 | + >>> out.read() |
| 73 | + 'hello world' |
| 74 | + >>> out.filename |
| 75 | + 'foo' |
| 76 | + >>> out.bar |
| 77 | + 'baz' |
| 78 | + >>> out.upload_date |
| 79 | + datetime.datetime(...) |
| 80 | + |
| 81 | +.. note:: |
| 82 | + |
| 83 | + Some attributes (for example, ``"filename"``) are GridFS keywords and can't be |
| 84 | + assigned values. |
0 commit comments