本文介绍了carrierwave Excon :: Errors :: SocketError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了与此相关的几篇文章和解决方案,但我仍然无法弄清楚,仍然在/ posts
处返回

Excon :: Errors :: SocketError getaddrinfo:nodename (SocketError)



这是config / carrierwave.rb

  CarrierWave.configure do | config | 
config.fog_credentials = {
:provider => 'AWS',#需要
:aws_access_key_id => '----',#required
:aws_secret_access_key => '----',#需要
:region => 'eu-west-1',#可选,默认为'us-east-1'
:host => 's3.example.com',#可选,默认为零
:endpoint => 'https://s3.example.com:8080'#可选,默认为零
}
config.fog_directory ='name_of_directory'#必填
config.fog_public = false#可选,默认值to true
config.fog_attributes = {'Cache-Control'=>'max-age = 315576000'}#可选,默认为{}
config.asset_host ='https://assets.example .com'
end

avatar_upload.rb

 #encoding:utf-8 

class AvatarUploader< CarrierWave :: Uploader :: Base

包括CarrierWave :: MiniMagick

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

版本:thumb do
过程:resize_to_fill => [200,200]
end

def cache_dir
#{Rails.root} / tmp / uploads
结束
$ b $ def store_dir
uploads /#{model.class.to_s.underscore} /#{mounted_as} /#{model.id}
结束
def default_url
##For Rails 3.1+资产管道兼容性:
##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)
结束
结束

来自后控制器

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

除非current_user.admin?
除非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。配置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 ['---']
}
结束

存储:aws

  AWS ::错误:: MissingCredentialsError 

缺少凭证。

无法找到AWS凭证。您可以通过几种不同的方式配置您的AWS凭证
a:

*使用access_key_id和:secret_access_key调用AWS.config

*将AWS_ACCESS_KEY_ID和AWS_SECRET_ACCESS_KEY导出到ENV

*在EC2上,您可以使用IAM实例配置文件运行实例,凭证
将从这些
实例上的实例元数据服务自动加载。

*使用:credential_provider调用AWS.config。证书提供者应该
包含AWS :: Core :: CredentialProviders :: Provider或者对
使用相同的公共方法。

= Ruby on Rails

在Ruby on Rails应用程序中,您还可以通过以下方式在
中指定凭证:

*通过配置初始化脚本使用上面提到的任何方法
(例如RAILS_ROOT / config / initializers / aws-sdk.rb)。

*通过位于RAILS_ROOT / config / aws.yml的yaml配置文件。
这个文件应该像默认的RAILS_ROOT / config / database.yml
文件一样被格式化。


解决方案

这可能是因为使用 fog ,可能试着将你的gem切换到 carrierwave-aws ,看看它是否解决了你的问题:)



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


I have read several posts and solutions related to this, but I still cannot figure it out and still returns "Excon::Errors::SocketError at /postsgetaddrinfo: nodename nor servname provided, or not known (SocketError)"

this is 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

avatar_upload.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

from post controller

 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

updated config/carrierwave.rb after installing carrierwave-aws

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

after changing to storage :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.
解决方案

This is possibly because of an implementation with fog, perhaps try switching your gem to carrierwave-aws and see if it fixes your problems :)

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

I remember having a similar error and after changing, works fine!

这篇关于carrierwave Excon :: Errors :: SocketError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 07:46