使用Paperclip expiring_url和S3:permission => :私人的

时间:2013-07-28 22:56:54

标签: ruby-on-rails amazon-web-services amazon-s3 paperclip

我正在创建一个Rails应用程序,用户使用Paperclip将文件上传到S3,然后一个单独的组可以下载上传的文件。我需要这些文件是私有的并且一直在尝试使用:permissions => :私有没有任何运气,但可以在关闭时上传和下载文件。

我一直在关注github问题:https://github.com/thoughtbot/paperclip/wiki/Restricting-Access-to-Objects-Stored-on-Amazon-S3Rails 3, paperclip + S3 - Howto Store for an Instance and Protect Access但我仍然感到困惑并希望得到一些帮助。以下是我到目前为止的情况:

模型/ user.rb

class User < ActiveRecord::Base
  has_many :parts

 ...

  if Rails.env.development?
    has_attached_file :model
  else
    has_attached_file :model,
        :storage => :s3,    
        :path    => "files/:id/:basename.:extension",
        :default_url => '/images/missing_:style.jpg',   
        :bucket => ENV['AWS_BUCKET'],
        :s3_permissions => :private,
        :s3_host_name => 's3-us-west-1.amazonaws.com',
        :s3_credentials => {
            :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
            :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
        }
  end  

user_controller.rb

 def create
    @user = User.new(params[:user])
    @part = Part.new
    if @user.save
      ...
    end
  end

  def download
    @user = User.find(params[:id])
    redirect_to(@user.model.expiring_url(10))
  end

part_controller.rb

class PartsController < ApplicationController
  def index
    @parts = Part.all
    @users = User.all
  end

end

的routes.rb

 match 'users/download' => 'users#download', :as => 'download_model'

份/ index.html.erb

<p>
<% @parts.each do |part| %>
    <p><%= part.model_name %></p>
<% end %>

<% @users.each do |user| %>
    <p><%= link_to "Download", download_model_path(user) %></p>
<% end %>

</p>

0 个答案:

没有答案