aws s3 rails carrierwave图片上传无法正常工作

时间:2017-05-12 04:04:53

标签: ruby-on-rails amazon-s3 carrierwave

我正在使用s3和aws尝试设置我的rails应用程序。除图片上传器外,它的所有功能都正常工作。问题是图片上传器在本地服务器上运行得很好。我已将代码连接到s3存储桶。但我得到的只是上传时的文件名。我的方法有什么问题。

这是相关文件。

# picture_uploader.rb
# encoding: utf-8

class PictureUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  process resize_to_limit: [400, 400]

  if Rails.env.production?
    storage :fog
  else
    storage :file
  end


  # Choose what kind of storage to use for this uploader:
  storage :fog

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end

end

micropost.rb

class Micropost < ApplicationRecord
  belongs_to :user
  default_scope -> { order(created_at: :desc) }
  mount_uploader :picture, PictureUploader
  validates :user_id, presence: true
  validates :content, presence: true, length: { maximum: 2500 }
  validate  :picture_size
  has_many :notifications, dependent: :destroy  
  acts_as_votable  
  acts_as_commontable
  is_impressionable

  def set_success(format, opts)
    self.success = true
  end

  def self.search(search) 
    where("content LIKE ?", "%#{search}%")
  end  

  private

    # Validates the size of an uploaded picture.
    def picture_size
      if picture.size > 5.megabytes
        errors.add(:picture, "should be less than 5MB")
      end
    end
end

0 个答案:

没有答案