/usr/share/doc/python-pymongo-doc/html/examples/gridfs.html is in python-pymongo-doc 2.6.3-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | <!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="Content-Type" content="text/html; charset=utf-8" />
<title>GridFS Example — PyMongo 2.6.3 documentation</title>
<link rel="stylesheet" href="../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '2.6.3',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</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/sidebar.js"></script>
<link rel="top" title="PyMongo 2.6.3 documentation" href="../index.html" />
<link rel="up" title="Examples" href="index.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">
<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><a href="../index.html">PyMongo 2.6.3 documentation</a> »</li>
<li><a href="index.html" accesskey="U">Examples</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<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"><tt class="xref py py-mod docutils literal"><span class="pre">gridfs</span></tt></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"><tt class="xref py py-mod docutils literal"><span class="pre">gridfs</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">GridFS</span></tt></a> instance to use:</p>
<div class="highlight-python"><div class="highlight"><pre><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"><tt class="xref py py-class docutils literal"><span class="pre">GridFS</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">Database</span></tt></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"><tt class="xref py py-mod docutils literal"><span class="pre">gridfs</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">put()</span></tt></a> and
<a class="reference internal" href="../api/gridfs/index.html#gridfs.GridFS.get" title="gridfs.GridFS.get"><tt class="xref py py-meth docutils literal"><span class="pre">get()</span></tt></a> methods). To write data to GridFS, use
<a class="reference internal" href="../api/gridfs/index.html#gridfs.GridFS.put" title="gridfs.GridFS.put"><tt class="xref py py-meth docutils literal"><span class="pre">put()</span></tt></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><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="s">"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"><tt class="xref py py-meth docutils literal"><span class="pre">put()</span></tt></a> creates a new file in GridFS, and returns
the value of the file document’s <tt class="docutils literal"><span class="pre">"_id"</span></tt> key. Given that <tt class="docutils literal"><span class="pre">"_id"</span></tt>
we can use <a class="reference internal" href="../api/gridfs/index.html#gridfs.GridFS.get" title="gridfs.GridFS.get"><tt class="xref py py-meth docutils literal"><span class="pre">get()</span></tt></a> to get back the contents of the
file:</p>
<div class="highlight-python"><div class="highlight"><pre><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"><tt class="xref py py-meth docutils literal"><span class="pre">get()</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt></a>.</p>
<p>In addition to putting a <tt class="xref py py-class docutils literal"><span class="pre">str</span></tt> as a GridFS file, we can also
put any file-like object (an object with a <tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt>
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-python"><div class="highlight"><pre><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="s">"foo"</span><span class="p">,</span> <span class="n">bar</span><span class="o">=</span><span class="s">"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"><tt class="xref py py-meth docutils literal"><span class="pre">put()</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">get()</span></tt></a>. Some attributes (like <tt class="docutils literal"><span class="pre">"filename"</span></tt>) are
special and are defined in the GridFS specification - see that
document for more details.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<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>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/examples/gridfs.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<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>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<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><a href="../index.html">PyMongo 2.6.3 documentation</a> »</li>
<li><a href="index.html" >Examples</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2008 - 2012, 10gen, Inc..
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.
</div>
</body>
</html>
|