繁体   English   中英

Carrierwave和Amazon S3文件下载/上传

[英]Carrierwave & Amazon S3 file downloading/uploading

我有一个带有UploadsUploader的rails 3应用程序和一个安装它的资源模型。 我最近切换到使用s3存储,这破坏了我使用send_to方法下载文件的能力。 我可以使用redirect_to方法启用下载,该方法只是将用户转发到经过身份验证的s3网址。 我需要验证文件下载,我希望网址为http://mydomainname.com/the_file_pathhttp://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会很好,因为文件不会通过我的服务器。 任何帮助赞赏。 谢谢。

您希望上传到S3,但没有公共网址。 您可以将用户重定向到S3经过身份验证的URL,而不是从S3下载文件并使用send_file。 此URL将过期并且仅在一段时间内有效(供用户下载)。

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

由于您已经将fog_public设置为false,因此在调用resource.upload_url时会获得经过身份验证(即已签名)的URL吗?

暂无
暂无

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

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