Set images to public-read

Defining an alternate s3 configuration set to `public: true` means we can use it selectively. It sets the objects to `acl: "public-read"` by default (read-only) and means any image tags for those assets can use direct public links in the src attribute (without hitting the ActiveStorage::Representation endpoint). The default non-public service will still be used by default for any other files on instances using s3.
This commit is contained in:
Matt-Yorkley
2023-05-03 14:38:06 +01:00
parent 989a88e252
commit e54cff2274
5 changed files with 18 additions and 6 deletions

View File

@@ -10,4 +10,8 @@ class ApplicationRecord < ActiveRecord::Base
include ArelHelpers::JoinAssociation
self.abstract_class = true
def self.image_service
ENV["S3_BUCKET"].present? ? :amazon_public : :local
end
end

View File

@@ -87,10 +87,10 @@ class Enterprise < ApplicationRecord
}
accepts_nested_attributes_for :custom_tab
has_one_attached :logo
has_one_attached :promo_image
has_one_attached :logo, service: image_service
has_one_attached :promo_image, service: image_service
has_one_attached :terms_and_conditions
has_one_attached :white_label_logo
has_one_attached :white_label_logo, service: image_service
validates :logo,
processable_image: true,

View File

@@ -25,8 +25,8 @@ class EnterpriseGroup < ApplicationRecord
delegate :phone, :address1, :address2, :city, :zipcode, :state, :country, to: :address
has_one_attached :logo
has_one_attached :promo_image
has_one_attached :logo, service: image_service
has_one_attached :promo_image, service: image_service
validates :logo,
processable_image: true,

View File

@@ -9,7 +9,7 @@ module Spree
large: { resize_to_limit: [600, 600] },
}.freeze
has_one_attached :attachment
has_one_attached :attachment, service: image_service
validates :attachment,
attached: true,

View File

@@ -19,3 +19,11 @@ amazon:
secret_access_key: <%= ENV["S3_SECRET"] %>
bucket: <%= ENV["S3_BUCKET"] %>
region: <%= ENV.fetch("S3_REGION", "us-east-1") %>
amazon_public:
service: S3
public: true
access_key_id: <%= ENV["S3_ACCESS_KEY"] %>
secret_access_key: <%= ENV["S3_SECRET"] %>
bucket: <%= ENV["S3_BUCKET"] %>
region: <%= ENV.fetch("S3_REGION", "us-east-1") %>