失踪地区;使用:区域选项或导出区域名称到ENV ['AWS_REGION']

时间:2016-12-13 10:18:32

标签: ruby-on-rails ruby amazon-web-services paperclip

我知道还有其他问题是相同的,但它们没有解决我的问题。

我一直收到错误:Aws::Errors::MissingRegionError in BooksController#create

missing region; use :region option or export region name to ENV['AWS_REGION']。但是,这是我在

中的配置

Development.rb:

config.paperclip_defaults = {
        storage: :s3,
        s3_host_name: "s3-us-west-2.amazonaws.com",
        s3_credentials: {
            bucket: ENV['AWS_BUCKET'],
            access_key_id: ENV['AWS_ACCESS_KEY_ID'],
            secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
            s3_region: ENV['us-west-2']
        }

    }

Production.rb:

config.paperclip_defaults = {
        storage: :s3,
        s3_host_name: "s3-us-west-2.amazonaws.com",
        s3_credentials: {
            bucket: ENV['AWS_BUCKET'],
            access_key_id: ENV['AWS_ACCESS_KEY_ID'],
            secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
            s3_region: ENV['us-west-2']
        }

    }

和Application.rb:

config.paperclip_defaults = {
        storage: :s3,
        s3_host_name: "s3-us-west-2.amazonaws.com",
        s3_credentials: {
            bucket: ENV['AWS_BUCKET'],
            access_key_id: ENV['AWS_ACCESS_KEY_ID'],
            secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
            s3_region: ENV['us-west-2']
        }

    }

然而,它不断出现错误。我从其他问题中听取了其他人的建议。希望有人可以提供帮助。

3 个答案:

答案 0 :(得分:8)

您应该将ENV['AWS_REGION'] env变量设置为"us-west-2"并将其用作

s3_region: ENV['AWS_REGION']

或使用字符串:

s3_region: 'us-west-2'

另外,s3_region选项应该移出credentials中的config/environments/{development|production}哈希:

config.paperclip_defaults = {
  storage: :s3,
  s3_host_name: "s3-us-west-2.amazonaws.com",
  s3_region: 'us-west-2', # or ENV['AWS_REGION']
  s3_credentials: {
    bucket: ENV['AWS_BUCKET'],
    access_key_id: ENV['AWS_ACCESS_KEY_ID'],
    secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
  }
}

答案 1 :(得分:2)

Rails 5 Way:

根据更新的文档区域是必要的。非us-region所需的地方。使用paperclip设置s3的推荐方法是:

  1. 将您的秘密信息放在yml文件中,让我们说aws.yml(必须是git 忽略)
  2. 将您的全局配置放在环境文件中,即 development/production.rb
  3. 中的config/environments/
  4. 将您的文件相关设置放入模型中。就我而言,我正在定义 User模型
  5. 中的图像属性

    示例步骤1:config/aws.yml

    development:
      access_key_id: AWS_ACCESS_KEY_ID # without quotation marks
      secret_access_key: AWS_SECRET_KEY_ID # without quotation marks
    
    production:
      access_key_id: <%= ENV["AWS_ACCESS_KEY_ID"] %> # get it from terminal environment
      secret_access_key: <%= ENV["AWS_SECRET_KEY_ID"] %> # get it from terminal environment
    

    示例步骤2:config/environments/development.rb

    # US West (N. California)  us-west-2   apigateway.us-west-2.amazonaws.com  HTTPS
    
    config.paperclip_defaults = {
      :storage => :s3,
      :s3_region => 'us-west-2',
      :bucket => 'production-bucket',
      :path => '/:class/:attachment/:id_partition/:style/:filename',
      :s3_credentials => "#{Rails.root}/config/aws.yml",
    }
    

    示例步骤3:app/models/user.rb

    has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }
    validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/
    

    最后您可以上传文件:

    def upload_to_s3
        response = {JSON_KEY_STATUS_MESSAGE: "OK", server_time: DateTime.current.to_i}
        response[:user] = []
        response[:status] = '0'
    
        unless params[:avatar].present?
            response[:message] = 'either user avatar image file [avatar]'
            render json: response and return
        end
    
        begin
            user = User.new # create new user
            user.avatar = params[:avatar] # params[:avatar] is a file posted by form in mutli-part true over json api
            s = user.save # save it # will through error if you have more than one required attributes
            if(s != false)
                response[:message] = 'file Successfully upload to s3'
            else
                response[:message] = 'fail to upload file to s3'
            end
        rescue => e
            response[:message] = e.message # this guy will help debug a lot!
        end
        render json: response and return
    end
    

答案 2 :(得分:-1)

创建一个名为:

的文件
config/initializers/paperclip.rb

并添加以下内容:

Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
Paperclip::Attachment.default_options[:s3_host_name] = 's3-eu-west-1.amazonaws.com'

在第3行,我有eu-west-1取代你所在的地区。