Skip to content

Configure S3 data storage

E. Lynette Rayle edited this page Jun 22, 2021 · 3 revisions

Configure a Spotlight app to use AWS S3 for data storage.

This document describes the basic configuration of a Spotlight app related to using AWS S3 for data storage. It does not describe how to set up S3 itself.

The configurations use the original defaults if ENV['S3_KEY_ID'] is not present. This allows for local development and testing without requiring the S3 data storage connection.

Environment Setup

Setup environment variables to hold the S3 data location info. (For my implementation on Elastic Beanstalk, the variables were defined on AWS and referenced in the Spotlight app.)

ENV['S3_BUCKET']
ENV['S3_BUCKET_DOMAIN']
ENV['S3_BUCKET_REGION']
ENV['S3_KEY_ID']
ENV['S3_SECRET_KEY']

Carrierwave Setup

Edit Gemfile and add...

gem 'carrierwave-aws'

Add config/initializers/carrierwave.rb initializer with S3 info...

if ENV['S3_KEY_ID'].present?
  CarrierWave.configure do |config|
    config.storage = :aws
    config.aws_bucket = ENV['S3_BUCKET']
    config.aws_acl = 'bucket-owner-full-control'

    config.aws_credentials = {
      access_key_id:      ENV['S3_KEY_ID'],
      secret_access_key:  ENV['S3_SECRET_KEY'],
      region:             ENV['S3_BUCKET_REGION']
    }
  end
end

Tell spotlight to use AWS as uploader_storage by editing config/initializers/spotlight_initializer.rb and adding :aws as the uploader_storage near the current commented out line setting it to :file

# Spotlight::Engine.config.uploader_storage = :file
Spotlight::Engine.config.uploader_storage = :aws if ENV['S3_KEY_ID'].present?

RIIIF Setup

Update Riiif initializer at config/initializers/riiif.rb to work with S3. This replaced the entire file for me, as the else condition handles the non-S3 case for local development.

if S3.connected?
  Riiif::Image.file_resolver = Riiif::HTTPFileResolver.new
  Riiif::Image.file_resolver.id_to_uri = lambda do |id|
    aws_file = Spotlight::FeaturedImage.find(id).image.file
    raise Riiif::ImageNotFoundError, "unable to find file for #{id}" if aws_file.nil?

    aws_file.file.presigned_url(':get')
  end
else
  Riiif::Image.file_resolver = Spotlight::CarrierwaveFileResolver.new
end
# Riiif::Image.authorization_service = IIIFAuthorizationService

# Riiif.not_found_image = 'app/assets/images/us_404.svg'
#
Riiif::Engine.config.cache_duration_in_days = 365

MIGRATION NOTE: Previous documentation set the presigned_url to use 'GET'. A recent change brought to light that this was incorrect and just happened to work. The code above that uses :get should be used instead. When migrating to a later version of aws-sdk-ruby you will need to update the riiif.rb initializer. No other change was made to the setup. This was discovered during a migration from Spotlight 2.13 to 3.x.