rails 4 amazon s3 seahorse getaddrinfo错误

时间:2015-03-10 07:26:11

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

我正在尝试使用aws-sdk的第2版完成此tutorial,我已经包含了这些额外的宝石

gem 'aws-sdk-core'

gem 'dotenv-rails', :groups => [:development, :test]

根文件夹中的.env文件是

S3_BUCKET=XXXredactedXXX
AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXredactedXXXXXX
AWS_SECRET_ACCESS_KEY=XXXXXredactedXXXXXX
AWS_REGION=Oregon

我试图通过在rails控制台中运行这些命令来测试配置。

s3 = Aws::S3::Client.new
resp = s3.list_buckets
resp.buckets.map(&:name)
#=> ["bucket-1", "bucket-2", ...]

我假设.env文件中的配置正在运行,因为没有AWS_REGION条目它会引发此错误

  

Aws :: Errors :: MissingRegionError:缺少区域;使用:区域选项或   将区域名称导出到ENV [' AWS_REGION']

这个' resp = s3.list_buckets'命令触发此错误' Seahorse :: Client :: NetworkingError:getaddrinfo:名称或服务未知'

防火墙端口已打开

To                         Action      From
--                         ------      ----
80                         ALLOW       Anywhere
443                        ALLOW       Anywhere
53693                      ALLOW       Anywhere
8118                       ALLOW       Anywhere
21/tcp                     ALLOW       Anywhere
1863                       ALLOW       Anywhere
53                         ALLOW       Anywhere
3000                       ALLOW       Anywhere
80 (v6)                    ALLOW       Anywhere (v6)
443 (v6)                   ALLOW       Anywhere (v6)
53693 (v6)                 ALLOW       Anywhere (v6)
8118 (v6)                  ALLOW       Anywhere (v6)
21/tcp (v6)                ALLOW       Anywhere (v6)
1863 (v6)                  ALLOW       Anywhere (v6)
53 (v6)                    ALLOW       Anywhere (v6)
3000 (v6)                  ALLOW       Anywhere (v6)

存储桶日志条目

  

XXXredactedXX my-first-bucket-tutorial985 [10 / Mar / 2015:06:31:51 +0000]   10.232.8.40 XXXredactedXX D37580D87A1D8EE9 REST.GET.NOTIFICATION - " GET /?通知HTTP / 1.1" 200 - 115 - 15 - " - " " AWS-内部/ 3" -

**更新,凭据设置和工作

亚马逊有一个红宝石样本here和许多凭据选项here。有一个命令行工具aws命令行实用程序here。此工具使用凭据和配置文件创建目录(.aws)。这取代了我相信的.env宝石,因为它有同样的目的。

凭证

[default]
aws_access_key_id = XXXXXredcatedXXX
aws_secret_access_key = XXXXXredactedXXXX

配置

[default]
#region = us-west-2
#output = json

正如您所看到的那样,区域已被注释掉并且不会影响ruby示例,因为在创建资源实例时会提供区域。

的Gemfile

source 'https://rubygems.org'
gem 'aws-sdk', '~> 2.0.22'
gem 'uuid', '~> 2.3.7'

s3_sample.rb

# Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

require 'rubygems'
require 'bundler/setup'
require 'aws-sdk'
require 'uuid'

s3 = Aws::S3::Resource.new(region: 'us-west-2')

uuid = UUID.new
bucket_name = "ruby-sdk-sample-#{uuid.generate}"
bucket = s3.bucket(bucket_name)
bucket.create

object = bucket.object('ruby_sample_key.txt')
object.put(body: "Hello World!")

1 个答案:

答案 0 :(得分:1)

最后我找到了一个解决方案,这个stackoverflow post添加了AWS

通过在根目录[〜/]中创建文件夹[.aws],然后创建包含以下内容的凭据和配置文件

,像以前一样设置凭据

凭证

[default]
aws_access_key_id = XXXXXredcatedXXX
aws_secret_access_key = XXXXXredactedXXXX

配置

[default]
#region = us-west-2
#output = json

将这2个宝石添加到生成的Gemfile

gem 'carrierwave'
gem 'aws-sdk', '~> 2'

我修改过的PostsController,Carrierwave确实有一种方法可以确定保存后文件的路径,但是我无法让它工作。

class PostsController < ApplicationController

  skip_before_action :verify_authenticity_token

  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
  end

  # GET /posts/1
  # GET /posts/1.json
  def show

    @post_attachments = @post.post_attachments.all

  end

  # GET /posts/new
  def new

    @post = Post.new

    @post_attachment = @post.post_attachments.build

  end

  # GET /posts/1/edit
  def edit
  end

  # POST /posts
  # POST /posts.json
  def create

    s3 = Aws::S3::Resource.new(region: 'us-west-2')

    bucket = s3.bucket('my-first-bucket-tutorial1985')

    @post = Post.new(post_params)

    respond_to do |format|
      if @post.save
        params[:post_attachments]['avatar'].each do |a|

          @post_attachment = @post.post_attachments.create!(:avatar => a, :post_id => @post.id)

          File.open('public/uploads/post_attachment/avatar/' + "#{@post_attachment[:post_id]}" + '/'+ "#{@post_attachment[:avatar]}", 'rb') do |file|
            s3.bucket('my-first-bucket-tutorial985').object("#{@post_attachment[:avatar]}").put(body:file)
          end



        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

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:title, post_attachments_attributes: [:id, :post_id, :avatar])
    end
end

它起作用的证据

enter image description here