Home>
Background

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

/what you tried

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.

File that may be the cause of the error (* part is actually described)
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

    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.

    Example
    export AWS_ACCESS_KEY_ID = AKIAIOSFODNN7EXAMPLE
    export AWS_SECRET_ACCESS_KEY = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
    export AWS_DEFAULT_REGION = us-west-2

  • 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_IDIf 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
    .envIndicates that is not loaded

    Gemfilesodotenv-railsOrdotenvBut
    Check for which environment the installation is set

    However, 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