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')
Share:
13,118

Related videos on Youtube

jdesilvio
Author by

jdesilvio

Updated on July 13, 2022

Comments

  • jdesilvio
    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
    jdesilvio almost 9 years
    thanks. how do I actually run this? I just put this in a script in lib/assets. On the command line i've tried just using ruby /path/to/file ruby runner /path/to/file and I keep getting an Access Denied error.
  • jdesilvio
    jdesilvio almost 9 years
    also tried bundle exec rails runner eval(File.read '/path/to/file')"
  • jdesilvio
    jdesilvio almost 9 years
    I was able to execute it using load path/to/file and the file downloaded
  • sockmonk
    sockmonk almost 9 years
    You 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
    The Whiz of Oz over 7 years
    that's your file's name.