<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>GridFS Example — PyMongo 3.7.2 documentation</title> <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/language_data.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="next" title="High Availability and PyMongo" href="high_availability.html" /> <link rel="prev" title="Gevent" href="gevent.html" /> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="high_availability.html" title="High Availability and PyMongo" accesskey="N">next</a> |</li> <li class="right" > <a href="gevent.html" title="Gevent" accesskey="P">previous</a> |</li> <li class="nav-item nav-item-0"><a href="../index.html">PyMongo 3.7.2 documentation</a> »</li> <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Examples</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="gridfs-example"> <h1>GridFS Example<a class="headerlink" href="#gridfs-example" title="Permalink to this headline">¶</a></h1> <p>This example shows how to use <a class="reference internal" href="../api/gridfs/index.html#module-gridfs" title="gridfs: Tools for working with GridFS"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gridfs</span></code></a> to store large binary objects (e.g. files) in MongoDB.</p> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last">The API docs for <a class="reference internal" href="../api/gridfs/index.html#module-gridfs" title="gridfs: Tools for working with GridFS"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gridfs</span></code></a>.</p> </div> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last"><a class="reference external" href="http://dirolf.com/2010/03/29/new-gridfs-implementation-for-pymongo.html">This blog post</a> for some motivation behind this API.</p> </div> <div class="section" id="setup"> <h2>Setup<a class="headerlink" href="#setup" title="Permalink to this headline">¶</a></h2> <p>We start by creating a <a class="reference internal" href="../api/gridfs/index.html#gridfs.GridFS" title="gridfs.GridFS"><code class="xref py py-class docutils literal notranslate"><span class="pre">GridFS</span></code></a> instance to use:</p> <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="kn">import</span> <span class="n">MongoClient</span> <span class="gp">>>> </span><span class="kn">import</span> <span class="nn">gridfs</span> <span class="go">>>></span> <span class="gp">>>> </span><span class="n">db</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">()</span><span class="o">.</span><span class="n">gridfs_example</span> <span class="gp">>>> </span><span class="n">fs</span> <span class="o">=</span> <span class="n">gridfs</span><span class="o">.</span><span class="n">GridFS</span><span class="p">(</span><span class="n">db</span><span class="p">)</span> </pre></div> </div> <p>Every <a class="reference internal" href="../api/gridfs/index.html#gridfs.GridFS" title="gridfs.GridFS"><code class="xref py py-class docutils literal notranslate"><span class="pre">GridFS</span></code></a> instance is created with and will operate on a specific <a class="reference internal" href="../api/pymongo/database.html#pymongo.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal notranslate"><span class="pre">Database</span></code></a> instance.</p> </div> <div class="section" id="saving-and-retrieving-data"> <h2>Saving and Retrieving Data<a class="headerlink" href="#saving-and-retrieving-data" title="Permalink to this headline">¶</a></h2> <p>The simplest way to work with <a class="reference internal" href="../api/gridfs/index.html#module-gridfs" title="gridfs: Tools for working with GridFS"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gridfs</span></code></a> is to use its key/value interface (the <a class="reference internal" href="../api/gridfs/index.html#gridfs.GridFS.put" title="gridfs.GridFS.put"><code class="xref py py-meth docutils literal notranslate"><span class="pre">put()</span></code></a> and <a class="reference internal" href="../api/gridfs/index.html#gridfs.GridFS.get" title="gridfs.GridFS.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code></a> methods). To write data to GridFS, use <a class="reference internal" href="../api/gridfs/index.html#gridfs.GridFS.put" title="gridfs.GridFS.put"><code class="xref py py-meth docutils literal notranslate"><span class="pre">put()</span></code></a>:</p> <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">fs</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="sa">b</span><span class="s2">"hello world"</span><span class="p">)</span> </pre></div> </div> <p><a class="reference internal" href="../api/gridfs/index.html#gridfs.GridFS.put" title="gridfs.GridFS.put"><code class="xref py py-meth docutils literal notranslate"><span class="pre">put()</span></code></a> creates a new file in GridFS, and returns the value of the file document’s <code class="docutils literal notranslate"><span class="pre">"_id"</span></code> key. Given that <code class="docutils literal notranslate"><span class="pre">"_id"</span></code> we can use <a class="reference internal" href="../api/gridfs/index.html#gridfs.GridFS.get" title="gridfs.GridFS.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code></a> to get back the contents of the file:</p> <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">fs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> <span class="go">'hello world'</span> </pre></div> </div> <p><a class="reference internal" href="../api/gridfs/index.html#gridfs.GridFS.get" title="gridfs.GridFS.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code></a> returns a file-like object, so we get the file’s contents by calling <a class="reference internal" href="../api/gridfs/grid_file.html#gridfs.grid_file.GridOut.read" title="gridfs.grid_file.GridOut.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a>.</p> <p>In addition to putting a <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code> as a GridFS file, we can also put any file-like object (an object with a <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code> method). GridFS will handle reading the file in chunk-sized segments automatically. We can also add additional attributes to the file as keyword arguments:</p> <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">fs</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">fs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">a</span><span class="p">),</span> <span class="n">filename</span><span class="o">=</span><span class="s2">"foo"</span><span class="p">,</span> <span class="n">bar</span><span class="o">=</span><span class="s2">"baz"</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">out</span> <span class="o">=</span> <span class="n">fs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">out</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> <span class="go">'hello world'</span> <span class="gp">>>> </span><span class="n">out</span><span class="o">.</span><span class="n">filename</span> <span class="go">u'foo'</span> <span class="gp">>>> </span><span class="n">out</span><span class="o">.</span><span class="n">bar</span> <span class="go">u'baz'</span> <span class="gp">>>> </span><span class="n">out</span><span class="o">.</span><span class="n">upload_date</span> <span class="go">datetime.datetime(...)</span> </pre></div> </div> <p>The attributes we set in <a class="reference internal" href="../api/gridfs/index.html#gridfs.GridFS.put" title="gridfs.GridFS.put"><code class="xref py py-meth docutils literal notranslate"><span class="pre">put()</span></code></a> are stored in the file document, and retrievable after calling <a class="reference internal" href="../api/gridfs/index.html#gridfs.GridFS.get" title="gridfs.GridFS.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code></a>. Some attributes (like <code class="docutils literal notranslate"><span class="pre">"filename"</span></code>) are special and are defined in the GridFS specification - see that document for more details.</p> </div> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../index.html">Table of Contents</a></h3> <ul> <li><a class="reference internal" href="#">GridFS Example</a><ul> <li><a class="reference internal" href="#setup">Setup</a></li> <li><a class="reference internal" href="#saving-and-retrieving-data">Saving and Retrieving Data</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="gevent.html" title="previous chapter">Gevent</a></p> <h4>Next topic</h4> <p class="topless"><a href="high_availability.html" title="next chapter">High Availability and PyMongo</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/examples/gridfs.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="high_availability.html" title="High Availability and PyMongo" >next</a> |</li> <li class="right" > <a href="gevent.html" title="Gevent" >previous</a> |</li> <li class="nav-item nav-item-0"><a href="../index.html">PyMongo 3.7.2 documentation</a> »</li> <li class="nav-item nav-item-1"><a href="index.html" >Examples</a> »</li> </ul> </div> <div class="footer" role="contentinfo"> © Copyright MongoDB, Inc. 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc. </div> </body> </html>