简体   繁体   中英

Ruby on Rails Download S3 Video File

Want to download S3 Signed Video file...but instead of download its going to playing...

Using send_data S3 download

send_data open(file).read, filename: 'archive12.mp4', type: 'video/mp4', disposition: 'attachment'

Video File= " https://s3-eu-west-1.amazonaws.com/tokboxhub.mangoapps.com/46250362/2e1ad9d5-8240-41d2-82bc-38c34bf92e7e/archive.mp4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJCPWVPHOCSAJPE5A%2F20190117%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Date=20190117T181610Z&X-Amz-Expires=9000&X-Amz-SignedHeaders=host&X-Amz-Signature=a26d991341b5349f1f1e3afb820883b2187bc6151f395506add87cc78daa15ff "

You are using open(file).read . Hence it reads the file and plays it. Maybe remove the .read . Try adding the stream flag. Look at send_file too: https://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_file .

OR Use the aws-sdk maybe?

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')

If you still want to use send_data , maybe try:

data = open("https://s3.amazonaws.com/PATTH TO YOUR FILE") 
send_data data.read, filename: 'archive12.mp4', stream: false, type: 'video/mp4', disposition: 'attachment'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM