On a Rails 6 app I use aws-sdk-s3 with ActiveStorage and I want to upload images.
Steps:
- I created a IAM User on AWS with S3FullAccess
- I configured storage.yml (see below)
- I created an config/aws.rb file with the following
Here is my configuration
# Gemfile
gem 'aws-sdk-s3', '~> 1', require: false
# storage.yml
amazon:
service: S3
access_key_id: ENV['AWS_ACCESS_KEY_ID']
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
region: eu-west-1
bucket: mybucketname
# config/aws.rb
require 'aws-sdk-core'
Aws.config.update(
region: 'eu-west-1',
credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
)
I have a simple model with:
has_one_attached :picture, dependent: :destroy
On development everything works fine, but not on production.
For eg..
> model_object.picture.attach(io: File.open(Rails.root.join('testfile.png')), filename: 'testfile.png')
> Aws::S3::Errors::InvalidAccessKeyId (The AWS Access Key Id you provided does not exist in our records.)
Environment variables are correctly visible from console, so I don't think it's a configuration issue...
ENV['AWS_ACCESS_KEY_ID']
ENV['AWS_SECRET_ACCESS_KEY']
What could I do?
To make sure I had the correct AWS access policies I uploaded an image using APIs directly from console like this:
require 'aws-sdk-core'
Aws.config.update(
region: 'eu-west-1',
credentials: Aws::Credentials.new(myakid, mysecret_key)
)
s3 = Aws::S3::Client.new
resp = s3.list_buckets
resp.buckets.map(&:name) # He gave me the list of my buckets
resp = s3.list_objects(bucket: 'MYBUCKET', max_keys: 2)
resp.contents.each do |object|
puts "#{object.key} => #{object.etag}"
end
# upload
resource = Aws::S3::Resource.new
object = resource.bucket(MYBUCKET).object("completed.png")
object.upload_file(Rails.root.join('app/assets/images/completed.png'))
And the file was correctly uploaded.
So, I cannot understand why ActiveStorage doesn't work...