Kranthi
Kranthi

Reputation: 1417

How to read file from s3?

I'm trying to read a CSV file directly from s3.

I'm getting the s3 URL but I am not able to open it as it's not in the local system. I don't want to download the file and read it.

Is there any other way to achieve this?

Upvotes: 8

Views: 13694

Answers (3)

Aleks
Aleks

Reputation: 5320

There are few ways, depending on the gems that you are using. For example, one of the approaches from official documentation:

s3 = Aws::S3::Client.new
resp = s3.get_object(bucket:'bucket-name', key:'object-key')

resp.body
#=> #<StringIO ...> 

resp.body.read
#=> '...'

Or if you are using CarrierWave/Fog:

obj = YourModel.first
content = obj.attachment.read

Upvotes: 12

Grych
Grych

Reputation: 2901

You can open the file from URL directly:

require 'open-uri'
csv = open('http://server.com/path-to-your-file.csv').read

Upvotes: 6

hcarreras
hcarreras

Reputation: 4612

I think s3 doesn't provide you any way of reading the file without downloading it. What you can do is save it in a tempfile:

@temp_file = Tempfile.open("your_csv.csv")
@temp_file.close
`s3cmd get s3://#{@your_path} #{@temp_file.path}`

For further information: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/tempfile/rdoc/Tempfile.html

Upvotes: -2

Related Questions