Using send_file to download a file from Amazon S3?

前端 未结 7 1580
感动是毒
感动是毒 2020-11-29 18:10

I have a download link in my app from which users should be able to download files which are stored on s3. These files will be publicly accessible on urls which look somethi

7条回答
  •  执笔经年
    2020-11-29 18:56

    The following is what ended up working well for me. Getting the raw data from the S3 object and then using send_data to pass that on to the browser.

    Using the aws-sdk gem documentation found here http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3/S3Object.html

    full controller method

    def download
      AWS.config({
        access_key_id: "SECRET_KEY",
        secret_access_key: "SECRET_ACCESS_KEY"
      })
    
      send_data( 
        AWS::S3.new.buckets["S3_BUCKET"].objects["FILENAME"].read, {
          filename: "NAME_YOUR_FILE.pdf", 
          type: "application/pdf", 
          disposition: 'attachment', 
          stream: 'true', 
          buffer_size: '4096'
        }
      )
    end
    

提交回复
热议问题