简体   繁体   English

Rails下载S3文件

[英]Rails download S3 file

I'm uploading files to S3 with paperclip, and now I would like to download them from the same app. 我正在使用回形针将文件上传到S3,现在我想从同一应用程序下载文件。 So I'm doing what lots of pages says, but if I use 'aws-sdk' it says that AWS::S3::S3Object method 'find' doesn't exist, and If I use 'aws-s3' gem, it says that I need to use 'aws-sdk'. 因此,我正在做很多页面上所说的事情,但是如果我使用“ aws-sdk”,它表示AWS :: S3 :: S3Object方法“ find”不存在,如果我使用“ aws-s3” gem,它说我需要使用“ aws-sdk”。 In controller I'm calling: 在控制器中,我打电话:

aws_object = AWS::S3::S3Object.find @component.folder.path, 'bucket-name'
send_data(aws_object.value, :type => @component.folder_content_type) 

EDIT: My model looks like: 编辑:我的模型看起来像:

attr_accessible :folder

has_attached_file   :folder, 
                    :path => ":rails_root/data/folders/:id/:basename.:extension",
                    :storage => :s3,
                    :s3_credentials => {
                        :bucket => "my-bucket-name",
                        :access_key_id => "XXXXXXXXX",
                        :secret_access_key => "XXXXXXXXX"
                    }

This worked for me: 这为我工作:

http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/ http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/

There's a example to download too. 也有一个示例可供下载。

The secret was ".read" : 秘密是“ .read”:

data = open(asset.uploaded_file.url)
send_data data.read, :type => data.content_type, :x_sendfile => true,:filename => asset.file_name 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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