Upgrade Paperclip and AWS SDK

While building a new docker based staging server for a legacy rails 5.0 app, I started getting the following error:

Your bundle is locked to mimemagic

Clearly the gem is not found on rubygems.org. I recently read on ruby weekly about licencing issue so I need to upgrade the mimemagic gem to make progress.

Our app is using gem paperclip 4.3.7 which depends on mimemagic 0.3.0. So we have no way but to upgrade the paperclip to fix the mimemagic issue. As such paperclip upgrade is easy but can become complicated in some instances but we took a call and decided to upgrade it to the highest version of paperclip 6.1.0 and so the AWS-SDK as well.

I strongly recommed to check the below video found in README showing how to migrate from Paperclip 4.x to 5.x and aws-sdk 1.x to 2.x before you migrate the paperclip.

Here is our upgrade process:

1. Change the Gemfile and bundle

Here is a comparison of the changes made:

Gemfile comparision

2. Change the config/initializers/aws.rb

AWS.config({
  region: 'eu-central-1',
  access_key_id: Settings.aws_access_key,
  secret_access_key: Settings.aws_secret_key
})

to

require 'aws-sdk-core'

Aws.config.update(
  region: 'eu-central-1',
  credentials: Aws::Credentials.new(Settings.aws_access_key, Settings.aws_secret_key)
)

Notice the difference between AWS to Aws.

3. Change the paperclip config to add the s3_region

Paperclip config comparision

And that worked!

If you are using aws-sdk for uploading files then you need to make changes there as well. We were saving a couple of autogenerated files to S3. Here are simplified snippets of a file upload code before and after migration.

Before
filename = ...
local_file = '/path/to/your/file'

bucket = AWS::S3.new.buckets['bucket name']
bucket.objects[filename].write(file: local_file, acl: :public_read)
After
filename = ...
local_file = '/path/to/your/file'

object = Aws::S3::Resource.new.bucket('bucket name').object(filename)
File.open(local_file, 'rb') do |file|
  object.put(body: file, acl: 'public-read', content_type: 'text/xml')
end

Hope this helps!