2015-05-22 140 views
0

我有一個休息API,我正在建設。它採用mongoiddevise現在已經paperclipmongoid-paperclipaws-sdk寶石安裝回形針與設計上傳到S3

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._%+-][email protected][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' 

我一直在尋找並找不出一個分辨率離開這個問題。我將配置從遠程服務器複製到本地主機。我知道它至少在一個方面起作用,我可以在終端中鍵入aws s3 ls,它會向我展示我的存儲桶。

回答

0

bucket-name顯示:bu​​cket_name =>「app-imgs/profile」,更正它。

+0

但這是正確的。在我想要的位置將其上傳app-imgs bucket,位於配置文件的子文件夾中 – Johnny