【问题标题】:carrierwave Excon::Errors::SocketError载波 Excon::Errors::SocketError
【发布时间】:2014-07-30 05:52:31
【问题描述】:

我已经阅读了一些与此相关的帖子和​​解决方案,但我仍然无法弄清楚并且仍然返回 " Excon::Errors::SocketError 在 /posts getaddrinfo: nodename 或 servname 提供,或未知 (SocketError)"

这是 config/carrierwave.rb

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',                        # required
    :aws_access_key_id      => '----',                        # required
    :aws_secret_access_key  => '----',                        # required
    :region                 => 'eu-west-1',                  # optional, defaults to 'us-east-1'
    :host                   => '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 {}
   config.asset_host     = 'https://assets.example.com'   
 end

头像上传.rb

 # encoding: utf-8

  class AvatarUploader < CarrierWave::Uploader::Base

    include CarrierWave::MiniMagick

    storage :fog
    process :resize_to_fit => [900,500]

   version :thumb do
      process :resize_to_fill => [200,200]
     end

   def cache_dir
      "#{Rails.root}/tmp/uploads"
    end

   def store_dir
      "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
   end
    def default_url
      #   # For Rails 3.1+ asset pipeline compatibility:
      #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
     #
        "/images/fallback/" + [version_name, "default.png"].compact.join('_')
    end

    def extension_white_list
      %w(jpg jpeg gif png)
    end
  end

来自后期控制器

 def create
   @post = Post.new(post_params)
   @post.user = current_user

    unless current_user.admin?
     unless current_user.manager?
       redirect_to :back, :alert => "acces denied"
     end
    end

    respond_to do |format|
     if @post.save
        params[:photo_attachments]['avatar'].each do |a|
         @photo_attachment = @post.photo_attachments.create!(:avatar => a, :post_id =>@post.id)
        end
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
         format.json { render :show, status: :created, location: @post }
        else
         format.html { render :new }
         format.json { render json: @post.errors, status: :unprocessable_entity }
      end
     end
    end

安装carrierwave-aws后更新了config/carrierwave.rb

CarrierWave.configure do |config|
   config.storage    = :aws
   config.aws_bucket = ENV['S3_BUCKET_NAME']
   config.aws_acl    = :public_read
   config.asset_host = 'http://example.com'
   config.aws_authenticated_url_expiration = 60 * 60 * 24 * 365

  config.aws_credentials = {
    access_key_id:     ENV['---'],
    secret_access_key: ENV['---']
  }
end

更改为存储后:aws

AWS::Errors::MissingCredentialsError

Missing Credentials.

Unable to find AWS credentials.  You can configure your AWS credentials
a few different ways:

* Call AWS.config with :access_key_id and :secret_access_key

* Export AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to ENV

* On EC2 you can run instances with an IAM instance profile and credentials
  will be auto loaded from the instance metadata service on those
  instances.

 * Call AWS.config with :credential_provider.  A credential provider should
  either include AWS::Core::CredentialProviders::Provider or respond to
  the same public methods.

 = Ruby on Rails

   In a Ruby on Rails application you may also specify your credentials in
  the following ways:

   * Via a config initializer script using any of the methods mentioned above
    (e.g. RAILS_ROOT/config/initializers/aws-sdk.rb).

   * Via a yaml configuration file located at RAILS_ROOT/config/aws.yml.
    This file should be formated like the default RAILS_ROOT/config/database.yml
    file.

【问题讨论】:

    标签: ruby-on-rails heroku carrierwave null


    【解决方案1】:

    这可能是因为使用 fog 的实现,也许尝试将您的 gem 切换到 carrierwave-aws 看看它是否能解决您的问题 :)

    https://github.com/sorentwo/carrierwave-aws

    我记得有类似的错误,更改后,工作正常!

    【讨论】:

    • 我已经更新了配置。现在我收到“不是公认的存储提供商”错误:(
    • ohh - 您需要将您的carrierwave.rb 放入初始化程序部分,因此它不是存在于config/carrierwave.rb,而是需要位于config/initializers/carrierwave.rb
    • 哦,对不起!我已经放入 config/initializers/carrierwave.rb :( 我必须将名称更改为 fog.rb 吗??
    • 不,这应该可行 - 还要确保在您的 AvatarUploader 中停止对 fog 的引用并放入 storage :aws (或者完全删除此行,因为它现在从您的初始化程序中设置)
    • 现在我得到“缺少凭据”,即使我在配置中提供了密钥:((
    【解决方案2】:

    我遇到了同样的问题,切换到carrierwave-aws 有效,我也没有在 S3 中为我的用户附加策略,所以检查一下是否仍然无法正常工作。

    【讨论】:

      猜你喜欢
      • 2012-12-14
      • 2013-07-20
      • 2016-06-01
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      相关资源
      最近更新 更多