title: How to get a binary stream by GridFS ObjectId with Spring Data MongoDB
body: <p>I can't figure out how to stream a binary file from GridFS with spring-data-mongodb and its <code>GridFSTemplate</code> when I already have the right <code>ObjectId</code>.</p>
  
  <p>GridFSTemplate returns either <code>GridFSResource</code> (<code>getResource()</code>) or <code>GridFSFile</code> (<code>findX()</code>). </p>
  
  <p>I can get the <code>GridFSFile</code> by ID: </p>
  
  <pre><code>// no way to get the InputStream?
  GridFSFile file = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(id)))
  </code></pre>
  
  <p>but there is no obvious way how to get an <code>InputStream</code> for that <code>GridFSFile</code>.</p>
  
  <p>Only <code>GridFSResource</code> allows me to get hold of the corresonding <code>InputStream</code> with <code>InputStreamResource#getInputstream</code>. But the only way to get a <code>GridFSResource</code> is by its <code>filename</code>.</p>
  
  <pre><code>// no way to get GridFSResource by ID?
  GridFSResource resource = gridFsTemplate.getResource("test.jpeg");
  return resource.getInputStream();
  </code></pre>
  
  <p>Somehow the <code>GridFsTemplate</code> API implies that filenames are unique - which they are not. The <code>GridFsTemplate</code> implementation just returns the first element.</p>
  
  <p>Now I'm using the native MongoDB API and everything makes sense again:</p>
  
  <pre><code>GridFS gridFs = new GridFs(mongo);
  GridFSDBFile nativeFile = gridFs.find(blobId);
  return nativeFile.getInputStream();
  </code></pre>
  
  <p>It looks like I'm misunderstanding the fundamental concepts behind the Spring Data Mongo GridFS abstraction. I'd expect (at least) one of the following things to be possible/true:</p>
  
  <ul>
  <li>get a <code>GridFSResource</code> by its ID</li>
  <li>get a <code>GridFSResource</code> or <code>InputStream</code> for a <code>GridFsFile</code> I already have</li>
  </ul>
  
  <p>Am I wrong or is there something odd with this particular piece of the Spring Data MongoDB API?</p>