简体   繁体   English

Carrierwave和Amazon S3文件下载/上传

[英]Carrierwave & Amazon S3 file downloading/uploading

I have a rails 3 app with an UploadsUploader and a Resource model on which this is mounted. 我有一个带有UploadsUploader的rails 3应用程序和一个安装它的资源模型。 I recently switched to using s3 storage and this has broken my ability to download files using the send_to method. 我最近切换到使用s3存储,这破坏了我使用send_to方法下载文件的能力。 I can enable downloading using the redirect_to method which is just forwarding the user to an authenticated s3 url. 我可以使用redirect_to方法启用下载,该方法只是将用户转发到经过身份验证的s3网址。 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? 我需要验证文件下载,我希望网址为http://mydomainname.com/the_file_pathhttp://mydomainname.com/controller_action_name/id_of_resource,所以我假设我需要使用send_to,但有没有办法使用redirect_to方法执行此操作? My current code follows. 我目前的代码如下。 Resources_controller.rb 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 initializer: 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 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 无法读取文件/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. 我曾尝试阅读有关carrierwave,fog,send_to以及所有这些内容,但我所尝试过的所有内容尚未取得丰硕成果。 Uploading is working fine and I can see the files in s3 bucket. 上传工作正常,我可以看到s3存储桶中的文件。 Using re_direct would be great as the file wouldn't pass through my server. 使用re_direct会很好,因为文件不会通过我的服务器。 Any help appreciated. 任何帮助赞赏。 Thanks. 谢谢。

Looks like you want to upload to S3, but have not-public URLs. 您希望上传到S3,但没有公共网址。 Instead of downloading the file from S3 and using send_file, you can redirect the user to the S3 authenticated URL. 您可以将用户重定向到S3经过身份验证的URL,而不是从S3下载文件并使用send_file。 This URL will expire and only be valid for a little while (for the user to download). 此URL将过期并且仅在一段时间内有效(供用户下载)。

Check out this thread: http://groups.google.com/group/carrierwave/browse_thread/thread/2f727c77864ac923 看看这个帖子: http//groups.google.com/group/carrierwave/browse_thread/thread/2f727c77864ac923

Since you're already setting fog_public to false, do you get an authenticated (ie signed) url when calling resource.upload_url 由于您已经将fog_public设置为false,因此在调用resource.upload_url时会获得经过身份验证(即已签名)的URL吗?

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

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