In the development environment, I was able to save the file to S3 using the carrier wave gem, but when I moved it in the production environment, I got an error.
ec2-user terminal:
"Missing required arguments: aws_access_key_id, aws_secret_access_key (ArgumentError)"
environment
ruby 2.5.7
ruby on rails 5.2.4
I did a lot of research without knowing it, but I saw how to solve this error by writing aws_key in credentials.yml.enc and deploy.rb, but deploy in my file It wasn't helpful because there is no .rb.
Also, carrierwave.rb uses environment variables instead of credentials, so this wasn't helpful either.
I also changed the bucket settings and looked at it, but I think that this is also okay because nothing is blocked in the current state.
I want to eliminate this error so that the image can be displayed normally in the production environment.
fog-core-2.2.0/lib/fog/core/service.rb:244: in `validate_options' I would like to know if there is a way to put aws_access_key_id directly here. Thank you.
Bucket settings
Block all public access
off
Block public access to buckets and objects allowed through new access control lists (ACLs)
off
Block public access to buckets and objects granted through any access control list (ACL)
off
Block public access to buckets and objects granted through a new public bucket policy or access point policy
off
Block public and cross-account access to buckets and objects through any public bucket policy or access point policy
off
Bucket policy
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt000001",
"Effect": "Allow",
"Principal": {
"AWS": "arn: aws: iam :: **********: user/******"
},
"Action": "s3 *",
"Resource": "arn: aws: s3 ::: ********"
}
]
}
.env file
DB_USERNAME = "root"
DB_PASSWORD = "En ***********"
DB_HOST = "rds-mysql-server.cum50tloc8cp.ap-northeast-1.rds.amazonaws.com"
DB_DATABASE = "t ******"
AWS_ACCESS_KEY_ID = "AKI ********"
AWS_SECRET_ACCESS_KEY = "wt7 ***********************"
carrierwave.rb
require'carrierwave/storage/abstract'
require'carrierwave/storage/file'
require'carrierwave/storage/fog'
CarrierWave.configure do | config |
config.storage: fog
config.fog_provider ='fog/aws'
config.fog_directory ='*******' # Describe the created bucket name
config.fog_credentials = {
provider:'AWS',
aws_access_key_id: ENV ['AWS_ACCESS_KEY_ID'], #environment variables
aws_secret_access_key: ENV ['AWS_SECRET_ACCESS_KEY'], #environment variables
region:'ap-northeast-1', # When Asia Pacific (Tokyo) is selected
path_style: true
}
end
include CarrierWave :: MiniMagick
storage: fog
def store_dir
"uploads/# {model.class.to_s.underscore}/# {mounted_as}/# {model.id}"
end
def extension_whitelist
% w (jpg jpeg gif png)
end
process: resize_to_limit =>[700, 700]
version: thumb do
process resize_to_fit: [62.5, 62.5]
end
version: thumb50 do
process resize_to_fit: [62.5, 62.5]
end
def filename
"# {secure_token}. # {File.extension}" if original_filename.present?
end
protected
def secure_token
var =: "@ # {mounted_as} _secure_token"
model.instance_variable_get (var) or model.instance_variable_set (var, SecureRandom.uuid)
end
end
-
Answer # 1
-
Answer # 2
If you are successful in the development environment, the cause is narrowed down to the environment-dependent part
For the time being as per the error message
Environment variableAWS_ACCESS_KEY_ID
If you check if is set
What are the consequences?config/environments/production.rb:
config.log_level =: error
if ENV ["AWS_ACCESS_KEY_ID"]. present? logger.debug "AWS_ACCESS_KEY_ID is present!" else else logger.debug "AWS_ACCESS_KEY_ID is not present!" end
If this environment variable is not set
.env
Indicates that is not loadedGemfile
sodotenv-rails
Ordotenv
But
Check for which environment the installation is setHowever, if you are using the cloud in a production environment
Environment variables are often provided in a different way than .env.
Check how to set environment variables for each cloud and follow the steps
Related articles
- ruby on rails - missing required keys: i'm having trouble resolving the [: id] error
- ruby on rails - [aws production environment] regarding http error 406
- ruby - i can't "rails db: create rails_env = production" on aws
- ruby on rails - [aws/ec2] when i try to run a rails application in a production environment, an error occurs in the image displa
- ruby - rails cannot be started in the production environment when building an ec2 environment
- ruby on rails - bootstap 4 does not work in rails6 production environment
- ruby on rails - i get a template is missing error in render
- ruby on rails - i can't log in to the app in the production environment (the app that was successfully deployed on heroku)
- ruby on rails - activemodel :: unknownattributeerror in production environment
- ruby on rails - the required parameters are reversed
- ruby on rails - i want to be able to send emails from the rails production environment
- ruby on rails 5 - if you get stuck in the required input, you don't want to see other validations
- ruby on rails - no route matches , missing required keys: [: id] cannot be resolved
- ruby on rails - i don't understand template is missing
- ruby - i want to get the information of the useragent in rails
- ruby on rails - rails s can't
- ruby on rails - it is not saved in the database after registering the product
- ruby on rails - mvc rails
- ruby on rails - rails6 rspec model run-time error nomethoderror: undefined method `valid?'for nil: nilclass
- ruby - rails cannot be started in the production environment when building an ec2 environment
- ruby - behavior that i do not know if rails can be started with unicorn
- ruby - it works fine in my local environment, but when i deploy it to aws, i get an error in mysql2 :: error: unknown column
- ruby - unable to deploy on capistrano
- ruby - passing variables using render partial
- ruby - output the total integer value held by each user
- html - i can't set the regular expression well
- ruby - rails i want to make the created_at time the same when creating multiple data at once
- ruby - dynamic form implementation in nested_form
- javascript - questions about the open method for asynchronous communication
If there are no mistakes in the description or settings no matter where you review
Please type the following command directly into the access_key and secret_key set on ec2-user.
I solved it with this.