Anconia
Anconia

Reputation: 4028

Accessing data stored in Amazon S3 through Rails

My goal is to make graphs from data within excel files that users have uploaded to Amazon S3.

I've already implemented the functionality for users to upload the excel files with Carrierwave, now I need to be able to access the data and make it presentable for use with a charting library (Highcharts).

The task that I am stuck on is directly accessing the data in S3 through Rails. Once the data is pulled it should be fairly straightforward to manipulate it with Highcharts.

Any suggestions would be much appreciated!

Upvotes: 4

Views: 4828

Answers (2)

Taimoor Changaiz
Taimoor Changaiz

Reputation: 10684

s3 = Aws::S3::Client.new
bucket = Aws::S3::Bucket.new('AWS_BUCKET NAME HERE')
bucket.objects.each do |obj|
  File.open("#{Rails.root}/#{obj.key}", 'wb') do |file|
    s3.get_object( bucket:ENV[:AWS_BUCKET], key: obj.key , response_target: file)
  end
end

OR

s3 = Aws::S3::Client.new
s3.list_objects(bucket: 'AWS_BUCKET NAME HERE').each do |response|
  response.contents.each do |obj|
    File.open("#{Rails.root}/#{obj.key}", 'wb') do |file|
      s3.get_object( bucket: 'AWS_BUCKET NAME HERE', key: obj.key , response_target: file)
    end
  end
end

There is official AWS-SDK RUBY gem

AWS SDK ruby official documentation for version 2

For enviornment variable configuration you can figaro or dotenv (for development environment) or set in ~/.bashrc file.

Note:

  1. You need to create S3 bucket and get AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
  2. Before development you can test and access your s3 bucket data using google chrome s3 browser extenstion
  3. Run command source ~/.bashrc or . ~/.bashrc file to reflect changes, if you store ENV variables there.

Code Reference

Upvotes: 0

LandonSchropp
LandonSchropp

Reputation: 10234

You can use the AWS SDK:

require 'aws-sdk'

# retrieve the access key and secret key
access_key_id = ENV["ACCESS_KEY_ID"]
secret_access_key = ENV["SECRET_ACCESS_KEY"]

# create an instance of the s3 client
s3 = AWS::S3.new(access_key_id: access_key_id, secret_access_key: secret_access_key)

# get the bucket
bucket = s3.buckets['your-bucket-name']

# retrieve the objects
bucket.objects.each do |object|
  puts object.key
  puts object.read
end

Upvotes: 5

Related Questions