问题
New to ruby here,
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?
I want to be able to parse the content of this file after I have it loaded into memory.
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 try this in IRB i get:
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 inside users.csv?
回答1:
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 check here: https://aws.amazon.com/blogs/developer/downloading-objects-from-amazon-s3-using-the-aws-sdk-for-ruby/
来源:https://stackoverflow.com/questions/47317042/how-can-i-download-the-content-of-a-file-from-s3-bucket-into-memory-with-ruby