我有一个Rails 3应用程序,其中包含UploadsUploader和Resource模型。我最近切换到使用S3存储,这破坏了我使用send_to方法下载文件的能力。我可以使用redirect_to方法启用下载,该方法只是将用户转发到经过身份验证的S3 URL。我需要对文件下载进行身份验证,并且我希望URL为http://mydomainname.com/the_file_path或http://mydomainname.com/controller_action_name/id_of_resource,因此我假设我需要使用send_to,但是否有一种使用redirect_to方法实现此目的的方法?我的当前代码如下。Resources_controller.rb
def download
resource = Resource.find(params[:id])
if resource.shared_items.find_by_shared_with_id(current_user) or resource.user_id == current_user.id
filename = resource.upload_identifier
send_file "#{Rails.root}/my_bucket_name_here/uploads/#{filename}"
else
flash[:notice] = "You don't have permission to access this file."
redirect_to resources_path
end
end
carrierwave.rb初始化器:
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => 'xxxx', # copied off the aws site
:aws_secret_access_key => 'xxxx', #
}
config.fog_directory = 'my_bucket_name_here' # required
config.fog_host = 'https://localhost:3000' # optional, defaults to nil
config.fog_public = false # optional, defaults to true
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
end
upload_uploader.rb
class UploadUploader < CarrierWave::Uploader::Base
storage :fog
def store_dir
"uploads"
end
end
所有的这些都会抛出错误:
无法读取文件/home/tom/Documents/ruby/rails/circlshare/My_bucket_name_here/uploads/Picture0024.jpg
我尝试了解carrierwave、fog、send_to等等,但到目前为止我尝试的一切都没有成果。上传正常工作,我可以在s3存储桶中看到文件。使用re_direct将是很好的选择,因为文件不会通过我的服务器。感谢任何帮助。谢谢。