带有设计的回形针上传到s3

时间:2015-05-22 21:02:58

标签: ruby-on-rails amazon-s3 devise mongoid paperclip

我正在建设一个休息api。它使用mongoiddevise,现在安装了paperclip mongoid-paperclipaws-sdk个宝石

Gemfile.rb

gem 'rails', '4.2.1'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :docgi
gem 'mongoid'
gem 'rspec-its', '~> 1.2.0'
gem 'unicorn', '~> 4.9.0'
gem 'kaminari' # adds pagination to ActiveModels
gem 'devise', '~> 3.4.1'
gem 'simple_token_authentication', '~> 1.9.1'
gem 'cancancan'
gem 'paperclip', '~> 4.1'
gem "mongoid-paperclip", :require => "mongoid_paperclip"
gem 'aws-sdk', '< 2.0'

我的用户模型使用所有这些:

class User
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paperclip
  has_many :posts
  has_many :friendships
  has_many :passive_friendships, :class_name => "Friendship", :foreign_key => "friend_id"

  validates :email, presence: true,
            uniqueness: true,
            format: {
                with: /\A[A-Za-z0-9._%+-]+@[A-Za-z0-9\.-]+\.[A-Za-z]+\Z/
            }
  validates :username, presence: true, uniqueness: true
  validates :telephone, presence: true
  validates :name, presence: true
  validates :gender, presence: true
  validates :dob, presence: true
  validates :password, presence: true
  has_mongoid_attached_file :picture, :styles => { :medium => "300x300>", :thumb => "100x100#" },
                            :storage => :s3,
                            :s3_credentials => {
                                :bucket => 'app-imgs/profile',
                                :access_key_id => "key",
                                :secret_access_key => "+t0v4+key1"
                            },
                            :url => "app-imgs.s3-website-us-west-2.amazonaws.com",
                            :path => "/:id/:style_:origin_timestamp",
                            :use_timestamp => true
  validates_attachment_content_type :picture, :content_type => /\Aimage\/.*\Z/

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :trackable, :validatable
  ## Token Authenticatable
  acts_as_token_authenticatable
  field :authentication_token

  ## Profile
  field :name,               type: String, default: ""
  field :gender,             type: String
  field :dob,                type: Date
  field :telephone,          type: String
  field :username,           type: String
  field :last_action,        type: Time
  ## Database authenticatable
  field :email,              type: String, default: ""
  field :encrypted_password, type: String, default: ""

  ## Trackable
  field :sign_in_count,      type: Integer, default: 0
  field :current_sign_in_at, type: Time
  field :last_sign_in_at,    type: Time
  field :current_sign_in_ip, type: String
  field :last_sign_in_ip,    type: String

  ## Indexes
  index({ email: 1, username: 1 })
  index({ last_location: "2d" }, { min: -200, max: 200 })
  index({ gender: 1, dob: 1, telephone: 1, loc: -1, posts: -1})


end

然后我创建了一个回形针初始化器来处理插值:

Paperclip::Attachment.default_options[:filename] = DateTime.now.strftime("%Y%m%d%H%M%S")
Paperclip.interpolates ('origin_timestamp') do |style, attachment|
  DateTime.now.strftime("%Y%m%d%H%M%S")
end

我的路线中的设计我补充道:

devise_for :users, :controllers => {:registrations => 'registrations', :sessions => 'sessions'}

我像这样覆盖了设计创建函数:

def create
    build_resource(sign_up_params)
    resource_saved = resource.save
    yield resource if block_given?
    if resource_saved
      sign_up(resource_name, resource)
      render json: resource
    else
      clean_up_passwords resource
      render json: resource.errors
      # @validatable = devise_mapping.validatable?
      # if @validatable
      #   @minimum_password_length = resource_class.password_length.min
      # end
      # respond_with resource
    end
  end
 private
  def sign_up_params
    params.permit(:name, :gender, :dob, :telephone, :username, :email, :password, :password_confirmation, :picture)
  end

现在,当我启动rails s并使用邮递员模拟用户注册时,我收到以下错误:

, [2015-05-22T13:57:31.529648 #27393]  INFO -- : [paperclip] saving 555f983b6a75736b01030000/original_20150522135731
[AWS S3 403 0.382363 0 retries] put_object(:acl=>:public_read,:bucket_name=>"app-imgs/profile",:content_length=>107729,:content_type=>"image/jpeg",:data=>Paperclip::UploadedFileAdapter: upsellPanel.jpg,:key=>"555f983b6a75736b01030000/original_20150522135731") AWS::S3::Errors::AllAccessDisabled All access to this object has been disabled

Completed 500 Internal Server Error in 576ms

AWS::S3::Errors::AllAccessDisabled (All access to this object has been disabled):
  app/controllers/registrations_controller.rb:17:in `create'

我一直在搜索,无法找到解决此问题的方法。我将配置从远程服务器复制到我的localhost。我知道它至少在一个方面起作用,因为我可以在终端中输入aws s3 ls,它会告诉我我的桶。

1 个答案:

答案 0 :(得分:0)

bucket-name显示:bu​​cket_name =&gt;&#34; app-imgs / profile&#34; ,纠正它。