首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails 4亚马逊s3海马getaddrinfo错误

rails 4亚马逊s3海马getaddrinfo错误
EN

Stack Overflow用户
提问于 2015-03-10 07:26:11
回答 1查看 1.7K关注 0票数 1

我试图使用aws的第2版完成这个教程,我已经包含了这些额外的gems。

代码语言:javascript
复制
gem 'aws-sdk-core'

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

根文件夹中的.env文件是

代码语言:javascript
复制
S3_BUCKET=XXXredactedXXX
AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXredactedXXXXXX
AWS_SECRET_ACCESS_KEY=XXXXXredactedXXXXXX
AWS_REGION=Oregon

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

代码语言:javascript
复制
s3 = Aws::S3::Client.new
resp = s3.list_buckets
resp.buckets.map(&:name)
#=> ["bucket-1", "bucket-2", ...]

我假设.env文件中的配置是有效的,因为如果没有AWS_REGION条目,它会抛出这个错误。

Aws::Error::MissingRegionError:缺失区域;使用:region选项或将区域名称导出到ENV'AWS_ region‘

这个'resp = s3.list_buckets‘命令触发这个错误'Seahorse::Client::NetworkingError: getaddrinfo: Name或service’

防火墙端口是开放的

代码语言:javascript
复制
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-桶-教程985 10/2015:06:31:51 +0000 10.232.8.40 XXXredactedXX D37580D87A1D8EE9 REST.GET.NOTIFICATION - "GET /?notification HTTP/1.1“200-115-15-”aws/3“-

**更新、设置证书和工作

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

凭据

代码语言:javascript
复制
[default]
aws_access_key_id = XXXXXredcatedXXX
aws_secret_access_key = XXXXXredactedXXXX

配置

代码语言:javascript
复制
[default]
#region = us-west-2
#output = json

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

Gemfile

代码语言:javascript
复制
source 'https://rubygems.org'
gem 'aws-sdk', '~> 2.0.22'
gem 'uuid', '~> 2.3.7'

s3_sample.rb

代码语言:javascript
复制
# 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!")
EN

回答 1

Stack Overflow用户

发布于 2015-03-26 01:16:14

最后,我找到了一个解决方案,这个堆栈溢出帖子为AWS添加了一些内容。

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

凭据

代码语言:javascript
复制
[default]
aws_access_key_id = XXXXXredcatedXXX
aws_secret_access_key = XXXXXredactedXXXX

配置

代码语言:javascript
复制
[default]
#region = us-west-2
#output = json

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

代码语言:javascript
复制
gem 'carrierwave'
gem 'aws-sdk', '~> 2'

我修改后的PostsController,载波确实有一种方法来确定保存后文件的路径,但我无法让它工作。

代码语言:javascript
复制
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

它起作用的证据

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28958152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档