ruby-on-rails - Carrierwave 在本地上传图像,而不是到 S3

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

我已将 Carrierwave+Fog 配置为与 Amazon S3 配合使用。我遇到的问题是,所有内容都是在我的项目的公共(public)文件夹中创建的,并且没有上传到 S3。拇指和目录都正常,只是图像的位置不起作用。

我的上传者:

    class ImageUploader < CarrierWave::Uploader::Base
    include CarrierWave::MiniMagick
    storage :fog
    def store_dir
      "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end
    version :thumb do
      process :resize_to_fit => [200, 200]
    end
    def extension_white_list
     %w(jpg jpeg gif png)
    end
end

雾.rb

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider => 'AWS',
    :aws_access_key_id => 'xxx',
    :aws_secret_access_key => 'yyy',
    :region => 'eu-west-1'
  }

  config.fog_directory = 'your_bucket_here'
  config.fog_public = true
  config.fog_attributes = {'Cache-Control' => 'max-age=315576000'} 
end

使用它的类:

class Image < ActiveRecord::Base
  belongs_to :product
  mount_uploader :remote_file, ImageUploader
end

Controller 代码:

    i = Image.new
    i.save
    i.remote_file = params[:image]

    render :json => {:response => i.remote_file.url}

响应是: {“响应”:“/uploads/tmp/1387464252-27678-6793/logo11w.png”}

图像是在 Rails 项目的公共(public)目录中创建的。

谢谢!

最佳答案

CarrierWave 使用 ActiveRecord before_save and after_save callbacks 执行上传。在您的 Controller 中,在调用 save 之前设置 remote_file:

i = Image.new
i.remote_file = params[:image]
i.save

关于ruby-on-rails - Carrierwave 在本地上传图像,而不是到 S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20684645/

相关文章:

ruby-on-rails - 未定义的局部变量或方法 `f'在局部 View 中

ruby-on-rails - Rails 日期时间和装置

ruby-on-rails - Rails 回形针错误 `AWS::S3::Errors::BadRequest` 在 `exists?` 和 `clear`

scala - 如何覆盖 awsglue 中的数据?

xml - 亚马逊产品 API : ItemLookup with Multiple ASINs

ruby-on-rails - 错误 "no such file to load"-- sqlite3/sqlite3_native (LoadError)

ruby-on-rails - 在 Rails "each"中排除第一个(或最后一个)

ruby-on-rails - Heroku、CarrierWave、MiniMagick : random tmp file missing

ruby-on-rails - 使用Carrierwave从S3下载图像(无需打开)

ruby-on-rails - 如果超过高度/宽度,则使用载波调整图像大小