# File lib/mongo/gridfs/grid.rb, line 54
    def put(data, opts={})
      begin
        # Ensure there is an index on files_id and n, as state may have changed since instantiation of self.
        # Recall that index definitions are cached with ensure_index so this statement won't unneccesarily repeat index creation.
        @chunks.ensure_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]], :unique => true)
        opts     = opts.dup
        filename = opts.delete(:filename)
        opts.merge!(default_grid_io_opts)
        file = GridIO.new(@files, @chunks, filename, 'w', opts)
        file.write(data)
        file.close
        file.files_id
      rescue Mongo::ConnectionFailure => e
        raise e, "Failed to create necessary index and write data."
      end
    end