module S3AssetSync

Constants

VERSION

Public Class Methods

purge() click to toggle source

Loops through specified S3 Bucket and checks to see if the object exists in our /public/assets folder. Deletes it from the bucket if it doesn’t exist.

# File lib/s3_asset_sync.rb, line 52
def self.purge
  puts "Cleaning assets in S3...".yellow

  Aws.config.update({
    credentials: Aws::Credentials.new(
      Rails.application.config.s3_asset_sync.s3_access_key,
      Rails.application.config.s3_asset_sync.s3_secret_access_key
    ),
    region: Rails.application.config.s3_asset_sync.s3_region
  })

  s3 = Aws::S3::Client.new

  keys = []

  s3.list_objects(bucket:Rails.application.config.s3_asset_sync.s3_bucket).each do |response|
    keys += response.contents.map(&:key)
  end

  keys.each do |key|
    if !File.exists?(Rails.root.join('public', 'assets', key))
      self.s3_delete_object(s3, key) 
      puts "DELETED: #{key}"
    end
  end

  puts "Asset clean successfully completed...".green
end
s3_delete_object(client, key) click to toggle source

Deletes an object from the specified S3 Bucket.

# File lib/s3_asset_sync.rb, line 112
def self.s3_delete_object(client, key)
  resp = client.delete_object(
    bucket: Rails.application.config.s3_asset_sync.s3_bucket,
    key: key
  )
end
s3_object_exists?(client, key) click to toggle source

Check if a key exists in the specified S3 Bucket.

# File lib/s3_asset_sync.rb, line 84
def self.s3_object_exists?(client, key)
  begin
    client.head_object(
      bucket: Rails.application.config.s3_asset_sync.s3_bucket,
      key: key
    )
    return true
  rescue
    return false
  end
end
s3_upload_object(client, key) click to toggle source

Uploads an object to the specified S3 Bucket.

# File lib/s3_asset_sync.rb, line 99
def self.s3_upload_object(client, key)
  resp = client.put_object(
    acl: "public-read",
    bucket: Rails.application.config.s3_asset_sync.s3_bucket,
    body: File.open(Rails.root.join('public','assets', key)),
    key: key
  )
  puts resp
end
sync() click to toggle source

Loops through /public/assets directory and sync’s each file with the specified S3 Bucket.

# File lib/s3_asset_sync.rb, line 11
def self.sync
  puts "Syncing assets to S3...".yellow

  Aws.config.update({
    credentials: Aws::Credentials.new(
      Rails.application.config.s3_asset_sync.s3_access_key,
      Rails.application.config.s3_asset_sync.s3_secret_access_key
    ),
    region: Rails.application.config.s3_asset_sync.s3_region
  })

  s3 = Aws::S3::Client.new

  self.sync_directory(s3, '')

  puts "Asset sync successfully completed...".green
end
sync_directory(s3, path) click to toggle source
# File lib/s3_asset_sync.rb, line 29
def self.sync_directory(s3, path)
  assets_dir = Rails.root.join('public','assets')
  current_dir = "#{assets_dir}#{path}"
  Dir.foreach(current_dir) do |file|
    next if file == '.' || file == '..'
    file_path = "#{path}/#{file}"
    file_key = file_path[1..-1]
    full_file_path = "#{assets_dir}#{path}/#{file}"
    
    if File.file?(full_file_path)
      puts "SYNC: #{file_path}"
      self.s3_upload_object(s3, file_key) unless self.s3_object_exists?(s3, file_key)
    elsif File.directory?(full_file_path)
      self.sync_directory(s3, file_path)
    end
  end
end