-
Notifications
You must be signed in to change notification settings - Fork 532
/
gridfs.rb
44 lines (33 loc) · 956 Bytes
/
gridfs.rb
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
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
def assert
raise "Failed!" unless yield
end
require 'mongo'
include Mongo
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
puts "Connecting to #{host}:#{port}"
db = Connection.new(host, port).db('ruby-mongo-examples')
data = "hello, world!"
grid = Grid.new(db)
# Write a new file. data can be a string or an io object responding to #read.
id = grid.put(data, :filename => 'hello.txt')
# Read it and print out the contents
file = grid.get(id)
puts file.read
# Delete the file
grid.delete(id)
begin
grid.get(id)
rescue => e
assert {e.class == Mongo::GridError}
end
# Metadata
id = grid.put(data, :filename => 'hello.txt', :content_type => 'text/plain', :metadata => {'name' => 'hello'})
file = grid.get(id)
p file.content_type
p file.metadata.inspect
p file.chunk_size
p file.file_length
p file.filename
p file.data