Home>

Currently, I am creating an image posting site like intagram.
I was able to save the image with activestorage and display the image locally.
I was implementing to save images using AWS S3
I got an error if I couldn't save the posted image.

Aws :: Sigv4 :: Errors :: MissingCredentialsError in ItemsController # create
Cannot load `Rails.config.active_storage.service`: missing credentials, provide credentials with one of the following options:-: access_key_id and: secret_access_key-: credentials-: credentials_provider
Corresponding source code
def create
    Item.create (item_params)
    # Save item model
    Specify what is quoted in item_params with # praivate
    @ item.image.attach (params [: image])
  end
  private
  def item_params
    params.require (: item) .permit (: text,: image) .merge (user_id: current_user.id)
    #Allow only item names and photos
    What is the #merge method? Combine two hashes into one
  end
What I tried

def create
Item.create (item_params)
Add code here ➡︎@item.image.attach (params [: image])
end

NoMethodError occurs even before writing the code of ➡︎

Supplementary information (FW/tool version, etc.)

config/environment/development.rb
config.active_storage.service =: amazon

config/envilorments/production.rb
config.active_storage.service =: amazon

➡︎ Change save destination from local to amazon

config/storage.yml

amazon:
service: S3
access_key_id: "<% = ENV ['AWS_ACCESS_KEY_ID']%>"
secret_access_key: "<% = ENV ['AWS_SECRET_ACCESS_KEY']%>"
region: ap-northeast-1
bucket: nostalgia2020

Environment variables in the terminal
vim ~/.zshrc
source ~/.zshrc
source ~/.bash_profile
I implemented and arranged the above.

  • Answer # 1

    access_key_id: "<% = ENV ['AWS_ACCESS_KEY_ID']%>"
    secret_access_key: "<% = ENV ['AWS_SECRET_ACCESS_KEY']%>"

    Delete unnecessary items with "" above.

    Run vim ~/.zshrc in terminal

    export AWS_SECRET_ACCESS_KEY =
    Where you have to
    AWS_SECRET_ACCESS_KEYS =
    It had become.

    Once when I was running vim ~/.zshrc
    I don't know how to use it when I accidentally delete it
    It was because I had deleted it.