I understand there are other questions that are the same, but they have no solved my problem.
I keep on receiving the error: Aws::Errors::MissingRegionError in BooksController#create
,
missing region; use :region option or export region name to ENV['AWS_REGION']
. However, this is my configuration in my
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']
}
}
And 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']
}
}
However, it keeps coming up with the error. I have followed other people's advice from other questions. Hope someone can help.
Ben
You should either set the ENV['AWS_REGION']
env variable to "us-west-2"
and use it as
s3_region: ENV['AWS_REGION']
Or use a string:
s3_region: 'us-west-2'
Also, s3_region
option should me moved out of credentials
hash in 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'],
}
}
Rails 5 Way:
according to updated documentation region is necessary. where as it was necessary for non us-region
. and the recommended way to setup paperclip
with s3
is:
yml
file let say aws.yml
(must be git
ignored)development/production.rb
in config/environments/
User
modelexample step-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
example step-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",
}
example step-3: (app/models/user.rb
)
has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/
finally you can upload file:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With