【问题标题】:Rails Engine not uploading to s3 with CarrierwaveRails 引擎未使用 Carrierwave 上传到 s3
【发布时间】:2012-12-06 15:39:38
【问题描述】:

我正在使用 rails new plugin my_plugin --mountable 创建一个 Rails 插件

这是一项相当多的工作要弄清楚,但它应该使用carrierwave将文件上传到S3,但它说好的但没有上传任何东西

Carrierwave 用于生成带有rails g uploader photo 的上传器 文件看起来像这样

# my_engine/app/uploaders/my_engine/photo_uploader.rb

# encoding: utf-8
module my_engine
 class PhotoUploader < CarrierWave::Uploader::Base
  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
 end
end

模型有一个坐骑:photo, PhotoUploader

module PdfGeneratorEngine
  class Assemble < ActiveRecord::Base
    attr_accessible :color, :photo, :qr_code_url, :text

    mount_uploader :photo, PhotoUploader
  end
end

我的 CarrierWave 配置文件是这样的

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',
    :aws_access_key_id      => 'MY_ACCES_KEY',
    :aws_secret_access_key  => 'MY_SECRET_KEY',
    :provider => 'AWS',
    :region => 'eu-west-1'
  }
  config.fog_directory  =  'my.bucket.com'
  config.fog_host       =  'https://s3-eu-west-1.amazonaws.com/my.bucket.com'
  config.storage = :fog
  config.s3_use_ssl = true
  config.fog_public = true
end

所以首先它开始对fog_host尖叫,如果是asset_host就可以了

接下来是 s3_use_ssl 的问题,而它是 CarrierWave 的 github 上的合并问题。但是主机已经定义为 https:// 所以我不明白为什么需要该行。

然后它说“好的,它完成了”,当我尝试(使用守护程序)检查文件时,那里什么都没有。

我错过了什么?还是 CarrierWave 和 Rails 可安装引擎存在问题?

【问题讨论】:

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


    【解决方案1】:

    在你的 photo_uploader.rb 中

    评论存储:文件和取消评论存储:雾

      # storage :file
      storage :fog   
    

    -- 看看你的fog.rb,它与这里给出的不一致

    carrierwave#using-amazon-s3

    CarrierWave.configure do |config|
      config.fog_credentials = {
        :provider               => 'AWS',                        # required
        :aws_access_key_id      => 'xxx',                        # required
        :aws_secret_access_key  => 'yyy',                        # required
        :region                 => 'eu-west-1'                   # optional, defaults to 'us-east-1'
        :hosts                  => 's3.example.com'              # optional, defaults to nil
        :endpoint               => 'https://s3.example.com:8080' # optional, defaults to nil
      }
      config.fog_directory  = 'name_of_directory'                     # required
      config.fog_public     = false                                   # optional, defaults to true
      config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}  # optional, defaults to {}
    end
    

    【讨论】:

    • 谢谢,生成上传器后忘记改了,但是因为S3上什么都没有,所以deamon还在呼救
    • [2] pry(#)> @s3.get('my.bucket.com', 'background.png') RightAws::AwsError: NoSuchKey: 指定的键没有存在。来自 /Users/dev/.rvm/gems/ruby-1.9.3-p327@pdf_generator/gems/right_aws-3.0.4/lib/awsbase/right_awsbase.rb:562:in `request_info_impl'
    • 我不明白为什么不是。必填字段在那里。
    • 好的 所以 CarrierWave 有点问题。我已经快速设置了 RightAws,现在它上传到 S3,我可以从我的守护进程中找到它。在我的上传器中,我添加了 @s3 = RightAws::S3Interface.new('MY KEY', 'MY SECRET KEY') @s3.put('my.bucket.com', assemble.photo.identifier ,params[:assemble] [:photo]) 感谢 Nishant 的帮助,CarrierWave 会更流畅、更好,但目前无法正常工作。在他们的 github 中有一个关于在 Rails 引擎中使用的问题。
    【解决方案2】:

    好的,CarrierWave 有点问题。

    我已经快速设置了 RightAws,现在它上传到 S3,我可以从我的守护进程中找到它。

    在我的上传器中我添加了

       @s3 = RightAws::S3Interface.new('MY KEY', 'MY SECRET KEY')
       @s3.put('my.bucket.com', assemble.photo.identifier ,params[:assemble][:photo])
    

    感谢 Nishant 的帮助,CarrierWave 会更流畅、更好,但目前无法正常工作。在他们的 github 中有一个关于在 Rails 引擎中使用的问题。

    【讨论】:

      猜你喜欢
      • 2012-07-08
      • 2016-06-29
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 2016-12-22
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多