Download and read contents of a text file on S3 into a string

1,057 views
Skip to first unread message

Brian

unread,
Sep 16, 2011, 3:24:11 PM9/16/11
to ruby-fog
Hi,

My problem is fairly straightforward. I'm trying to read the contents
of a existing text file on S3 into a local ruby string.

I'm looking at the docs, but cannot figure out the answer (http://
rubydoc.info/gems/fog/0.11.0/Fog/Storage/AWS/File).

I tried this:

connection = Fog::Storage.new({
:provider => 'AWS',
:aws_access_key_id => "my_access_key",
:aws_secret_access_key => "my_secret_key"
})

directory = connection.directories.new(:key =>
"my_S3_top_directory")

file = directory.files.new(:key =>
'path_to_existing_file_on_S3')

file_contents = file.body # <=== EMPTY STRING

Am I misusing the 'new' method? Is the 'body' method the wrong one to
use in this case?

Thanks!

geemus (Wesley Beary)

unread,
Sep 16, 2011, 5:10:23 PM9/16/11
to ruby...@googlegroups.com
You are pretty close. The problem lies in using
`directory.files.new(...)` because it assumes you are dealing with
just something local. Since its remote you should grab it so you can
get at the body. So just change that line to
`directory.files.get('path_to_existing_file_on_S3')` and I think the
rest should work as you expect.
Thanks!
wes

Brian

unread,
Sep 18, 2011, 6:49:42 PM9/18/11
to ruby-fog
Wes,

Thanks! Works great.

Brian
Reply all
Reply to author
Forward
0 new messages