Copy ARN of IAM user
Bucket policy
{
"Version": "2012-10-17",
"Id": "Policy1544152951996",
"Statement": [
{
"Sid": "Stmt1544152948221",
"Effect": "Allow",
"Principal": {
"AWS": "************①****************"
},
"Action": "s3:*",
"Resource": "arn:aws:s3:::************②**********"
}
]
}
Describe the "user's ARN" that you wrote down earlier in (1) above, and the "bucket name" that you created in (2).
gemfile
gem 'fog-aws'
bundle install
app/uploaders/image_uploader.rb
if Rails.env.development? || Rails.env.test?
storage :file
else
storage :fog
end
Create a file called carrierwave.rb from the root of your application directly under config / initializers.
config/initializers/carrierwave.rb
require 'carrierwave/storage/abstract'
require 'carrierwave/storage/file'
require 'carrierwave/storage/fog'
CarrierWave.configure do |config|
if Rails.env.development? || Rails.env.test?
config.storage = :file
else
config.storage = :fog
config.fog_provider = 'fog/aws'
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
region: 'ap-northeast-1'
}
config.fog_directory = 'Bucket name'
config.asset_host = 'https://s3-ap-northeast-1.amazonaws.com/Bucket name'
end
end
ec2 server
#Production environment
$ ssh -i [pem key name].pem ec2-user@[Elastic IP associated with the created EC2 instance]
(Using the downloaded key, ec2-Login as user)
$ sudo vim /etc/environment
#Press i to switch to insert mode and add the following. Do not delete the existing description.
AWS_ACCESS_KEY_ID='Copy the value of Access key ID in the CSV file here'
AWS_SECRET_ACCESS_KEY='Copy the Secret access key value to the CSV file here'
#After editing, press the escape key:Type wq to save and exit
AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are described in the CSV file downloaded when IAM user was created.
ec2 server
#Production environment
#Log out once to apply the edited environment variable.
$ exit
$ ssh -i [pem key name].pem ec2-user@[Elastic IP associated with the created EC2 instance]
#Make sure the environment variables are applied.
$ env | grep AWS_SECRET_ACCESS_KEY
$ env | grep AWS_ACCESS_KEY_ID
① Push to GitHub ② Execute "bundle exec cap production deploy"
Recommended Posts