Download file from S3 to Rails 4 app
13,118
The aws-sdk
v2 gem provides a simple interface for downloading objects from Amazon S3.
require 'aws-sdk'
s3 = Aws::S3::Resource.new(
region: 'us-east-1',
access_key_id: '...',
secret_access_key: '...'
)
s3.bucket('bucket-name').object('key').get(response_target: '/path/to/file')
Related videos on Youtube
Author by
jdesilvio
Updated on July 13, 2022Comments
-
jdesilvio almost 2 years
I have an AWS VM that runs a daily task and generates several files. I want my Rails app to download these files and place them in a folder within the app. Is there a gem or method in Ruby that can do this?
I know how to do it in bash with s3cmd and I guess I could create a script to get them this way, but looking for a more native rails way.
I am using the data in these files for the app, but I don't want the users to be able to download them.
-
jdesilvio almost 9 yearsthanks. how do I actually run this? I just put this in a script in
lib/assets
. On the command line i've tried just usingruby /path/to/file
ruby runner /path/to/file
and I keep getting anAccess Denied
error. -
jdesilvio almost 9 yearsalso tried
bundle exec rails runner eval(File.read '/path/to/file')"
-
jdesilvio almost 9 yearsI was able to execute it using
load path/to/file
and the file downloaded -
sockmonk almost 9 yearsYou should be able to put that code in a custom rake task, and then run it via rake manually, or from cron or whatever scheduling system you like.
-
The Whiz of Oz over 7 yearsthat's your file's name.