Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 704
Kranthi Avatar asked Mar 18 '23 22:03

Kranthi


2 Answers

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
like image 95
Aleks Avatar answered Mar 27 '23 20:03

Aleks


You can open the file from URL directly:

require 'open-uri'
csv = open('http://server.com/path-to-your-file.csv').read
like image 21
Grych Avatar answered Mar 27 '23 20:03

Grych