本文介绍了Carrierwave和放大器;亚马逊S3文件下载/上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails 3应用程序与UploadsUploader并在此安装在资源模型。最近,我转向使用S3存储,这也打破了我下载使用send_to法文件的能力。我可以允许使用redirect_to方法这是刚刚转发的用户身份验证的S3网址下载。我需要验证文件下载,我想的网址是 http://mydomainname.com/the_file_path 或http://mydomainname.com/controller_action_name/id_of_resource所以我假设我需要使用send_to,但有没有这样做,使用redirect_to方法的一种方式?我现在的code以下。 Resources_controller.rb

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_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.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

upload_uploader.rb

class UploadUploader < CarrierWave::Uploader::Base
  storage :fog

  def store_dir
    "uploads"
  end
end

这一切都引发错误:

All of this throws the error:

无法读取文件  /home/tom/Documents/ruby/rails/circlshare/My_bucket_name_here/uploads/Picture0024.jpg

我曾尝试阅读了有关carrierwave,雾,send_to和所有的一切,但我已经尝试一直没有卓有成效的呢。上传工作正常,我可以看到在S3存储桶中的文件。使用re_direct将是伟大的,因为该文件将不通过我的服务器。任何帮助AP preciated。谢谢你。

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.

推荐答案

看起来你要上传到S3,但还没有公开的网址。相反,在下载从S3文件,并使用由send_file,你可以将用户重定向到S3的验证URL。该URL将期满,只适用于一小会儿(供用户下载)。

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

看看这个线程:的

既然你已经fog_public设置为false,你打电话时获得认证(即签署)网址 resource.upload_url

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

这篇关于Carrierwave和放大器;亚马逊S3文件下载/上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:10