1

I have a CSV file stored on my S3 bucket, I would like to add a method in Rails so users can click on a button and download the file. In the controller I put the following

def s3_downloader(bucketName, key, localPath)
    # (1) Create S3 object
    s3 = Aws::S3::Resource.new(region: 'us-east-2')
    # (2) Create the source object
    sourceObj = s3.bucket(bucketName).object(key)
    # (3) Download the file
    sourceObj.get(response_target: localPath)
    puts "s3://#{bucketName}/#{key} has been downloaded to #{localPath}"
  end

Which I got from Tutorial

What should I put as localPath please? I tried something as simple as :

s3_downloader(Rails.application.secrets.s3_bucket_name, "Product_csv_file_sample.csv", "/Downloads")

But I get the error:

Errno::EACCES (Permission denied @ rb_sysopen - /Downloads)
2.6.3 :040 >   s3_downloader(Rails.application.secrets.s3_bucket_name, "Product_csv_file_sample.csv", "/Downloads")

Traceback (most recent call last):
        3: from (irb):40
        2: from (irb):40:in `rescue in irb_binding'
        1: from (irb):30:in `s3_downloader'

Also, how do I make sure that whether the user is on Mac or Windows, the localPath still works.

chmod 'chown' in the downlaods folder returns:

usage:  chmod [-fhv] [-R [-H | -L | -P]] [-a | +a | =a  [i][# [ n]]] mode|entry file ...
    chmod [-fhv] [-R [-H | -L | -P]] [-E | -C | -N | -i | -I] file ...
Rene Chan
  • 864
  • 1
  • 11
  • 25
  • Where is the "/Downloads" referring to? On the client or server? You shouldn't need to specify a folder on the client. – Arye Eidelman Dec 01 '19 at 00:08
  • It looks like your trying to access the folder on the server. Instead of responding with the file, and letting the browser place it in the default download location. – Arye Eidelman Dec 01 '19 at 00:15
  • Hi, I thought I was indicating localPath as the folder to download the file to. What do you recommend please? – Rene Chan Dec 01 '19 at 01:13
  • Maybe create a temporary path to store the file before sending it to the client. For example `"/#{Rails.root}/tmp/downloads/#{some_id_here}/"` – Arye Eidelman Dec 01 '19 at 01:25
  • Also look into how to respond to web requests with a file https://stackoverflow.com/questions/9233021/returning-files-from-rails – Arye Eidelman Dec 01 '19 at 01:28
  • Wait, the file is already on AWS S3, I would just like the user to click on a button a download the file. Isn't it a way of doing ? – Rene Chan Dec 01 '19 at 02:45
  • Maybe search for "rails direct download from Amazon s3". Also add public or private to your search depending on what Access level this file has. – Arye Eidelman Dec 01 '19 at 03:52
  • https://stackoverflow.com/a/12282709/3458162 – Arye Eidelman Dec 01 '19 at 03:57
  • It may be easier to use rails active storage https://guides.rubyonrails.org/active_storage_overview.html – Arye Eidelman Dec 01 '19 at 04:06

1 Answers1

0

Based on the error you posted, it looks like the user that's running rails server doesn't have permission to write/read to the Downloads directory. I would check there, and fix them as necessary using chmod`chown`.

sevensidedmarble
  • 643
  • 1
  • 4
  • 14