我正在创建一个 Rails 应用程序,其中用户使用 Paperclip 将文件上传到 S3,然后一个单独的组可以下载上传的文件。我需要文件是私有的,并且一直在尝试使用 :permissions => :private 没有任何运气,但可以在关闭此功能的情况下上传和下载文件。
我一直在关注 github 问题:https ://github.com/thoughtbot/paperclip/wiki/Restricting-Access-to-Objects-Stored-on-Amazon-S3和Rails 3, paperclip + S3 - Howto Store for an Instance和保护访问,但仍然卡住,希望能得到一些帮助。这是我到目前为止所拥有的:
模型/用户.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
用户控制器.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
路线.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>