开发者

Carrierwave & Amazon S3 file downloading/uploading

I have a rails 3 app with an UploadsUploader and a Resource model on which this is mounted. I recently switched to using s3 storage and this has broken my ability to download files using the send_to method. I can enable downloading using the redirect_to method which is just forwarding the user to an authenticated s3 url. I need to authenticate file downloads and I want the url to be http://mydomainname.com/the_file_path or http://mydomainname.com/controller_action_name/id_of_resource so I am assuming I need to use send_to, but is there a way of doing that using the redirect_to method? My current code follows. 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_n开发者_运维知识库ame_here/uploads/#{filename}"
    else
        flash[:notice] = "You don't have permission to access this file."
        redirect_to resources_path
    end
end

carrierwave.rb initializer:

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

All of this throws the error:

Cannot read file /home/tom/Documents/ruby/rails/circlshare/My_bucket_name_here/uploads/Picture0024.jpg

I have tried reading up about carrierwave, fog, send_to and all of that but everything I have tried hasn't been fruitful as yet. Uploading is working fine and I can see the files in s3 bucket. Using re_direct would be great as the file wouldn't pass through my server. Any help appreciated. Thanks.


Looks like you want to upload to S3, but have not-public URLs. Instead of downloading the file from S3 and using send_file, you can redirect the user to the S3 authenticated URL. This URL will expire and only be valid for a little while (for the user to download).

Check out this thread: http://groups.google.com/group/carrierwave/browse_thread/thread/2f727c77864ac923

Since you're already setting fog_public to false, do you get an authenticated (i.e. signed) url when calling resource.upload_url

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜