David
David

Reputation: 1569

How to download the content of a file from S3 bucket into memory with Ruby

I have a bucket in Amazon AWS S3 which has a file in it called users.csv.

How can I load the content of this file from the S3 bucket into memory with Ruby so I can parse it?

Here is my code:

require 'aws-sdk'
s3 = Aws::S3::Resource.new(region: 'us-west-1')
resp = s3.list_objects(bucket: 'bucket-name', max_keys: 1)
resp.contents.each do |object|
    puts #{object.value}
end

When I tried this in IRB I got:

struct Aws::S3::Types::Object key="users.csv", last_modified=2017-11-15 19:10:28 UTC, etag="\"9a3d50c07aa4aa6976037ce774294a26\"", size=101, storage_class="STANDARD", owner=struct Aws::S3::Types::Owner display_name="owner-name", id="42093cfa4ccb23a8156cdab8500a41a10bdbf90deebb0ee8a3b340dd1e0c3622"

How can I parse the content of users.csv?

Upvotes: 4

Views: 8568

Answers (1)

ExploringApple
ExploringApple

Reputation: 1484

From the AWS Documentation:

Downloading Objects into Memory

For small objects, it can be useful to get an object and have it available in your Ruby processes. If you do not specify a :target for the download, the entire object is loaded into memory into a StringIO object.

def import_from_s3 

  #initiate the client
  s3 = Aws::S3::Client.new({
      region:            region,
      access_key_id:     key_id,
      secret_access_key: secret
  })

  #Get the object
  resp = s3.get_object(bucket: bucket, key: key)

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

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

Call #read or #string on the StringIO to get the body as a String object.

For more information see "Downloading Objects from Amazon S3 using the AWS SDK for Ruby".

Upvotes: 8

Related Questions