【问题标题】:rails 4 amazon s3 seahorse getaddrinfo errorrails 4 amazon s3 seahorse getaddrinfo 错误
【发布时间】:2015-05-11 13:29:43
【问题描述】:

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

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:缺少区域;使用 :region 选项或 将区域名称导出到 ENV['AWS_REGION']

此 'resp = s3.list_buckets' 命令触发此错误 'Seahorse::Client::NetworkingError: getaddrinfo: Name or service not known'

防火墙端口已打开

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 /?notification HTTP/1.1”200-115-15-“-”“aws-internal/3”-

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

Amazon 有一个 ruby​​ 示例 here 和许多凭据选项 here。有一个命令行工具 aws 命令行实用程序here。此工具会创建一个包含凭据和配置文件的目录 (.aws)。这取代了我认为的 .env gem,因为它具有相同的目的。

凭据

[default]
aws_access_key_id = XXXXXredcatedXXX
aws_secret_access_key = XXXXXredactedXXXX

配置

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

正如您所见,该区域已被注释掉,并且不会影响 ruby​​ 示例,因为在创建资源实例时提供了一个区域。

宝石文件

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!")

【问题讨论】:

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


    【解决方案1】:

    我终于找到了一个解决方案,这个 stackoverflow post 加上 AWS 的附加功能

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

    凭据

    [default]
    aws_access_key_id = XXXXXredcatedXXX
    aws_secret_access_key = XXXXXredactedXXXX
    

    配置

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

    将这 2 个 gem 添加到生成的 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
    

    有效的证明

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-15
      • 2015-06-11
      • 1970-01-01
      • 2022-11-10
      • 2013-05-03
      • 1970-01-01
      相关资源
      最近更新 更多