缺失区域;使用 :region 选项或将区域名称导出到 ENV['AWS_REGION']
missing region; use :region option or export region name to ENV['AWS_REGION']
我知道还有其他相同的问题,但都没有解决我的问题。
我一直收到错误消息: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']
}
}
但是,它一直出现错误。我从其他问题中听取了其他人的建议。希望有人能帮忙。
本
您应该将 ENV['AWS_REGION']
环境变量设置为 "us-west-2"
并将其用作
s3_region: ENV['AWS_REGION']
或使用字符串:
s3_region: 'us-west-2'
此外,s3_region
选项是否应该从 config/environments/{development|production}
中的 credentials
散列中移出:
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'],
}
}
创建一个名为:
的文件
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 替换它,具体取决于您所在的区域。
Rails 5路:
根据更新的文档,区域是必需的。因为非 us-region
是必需的。使用 s3
设置 paperclip
的推荐方法是:
- 将您的秘密信息放入
yml
文件中,比如说 aws.yml
(必须是 git
忽略)
- 将您的全局配置放在环境文件中,即
development/production.rb
在 config/environments/
- 将您的文件相关设置放入模型中。就我而言,我正在定义
User
模型 中的图像属性
示例步骤 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
我知道还有其他相同的问题,但都没有解决我的问题。
我一直收到错误消息: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']
}
}
但是,它一直出现错误。我从其他问题中听取了其他人的建议。希望有人能帮忙。
本
您应该将 ENV['AWS_REGION']
环境变量设置为 "us-west-2"
并将其用作
s3_region: ENV['AWS_REGION']
或使用字符串:
s3_region: 'us-west-2'
此外,s3_region
选项是否应该从 config/environments/{development|production}
中的 credentials
散列中移出:
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'],
}
}
创建一个名为:
的文件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 替换它,具体取决于您所在的区域。
Rails 5路:
根据更新的文档,区域是必需的。因为非 us-region
是必需的。使用 s3
设置 paperclip
的推荐方法是:
- 将您的秘密信息放入
yml
文件中,比如说aws.yml
(必须是 git 忽略) - 将您的全局配置放在环境文件中,即
development/production.rb
在config/environments/
- 将您的文件相关设置放入模型中。就我而言,我正在定义
User
模型 中的图像属性
示例步骤 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