title: Limit total size (MBs) of uploads per user to AWS S3 (using Shrine)
body: <p>I am working on a Rails application that allows users to upload their images to AWS S3 via shrine. The setup so far works just fine - however, I was wondering if there is any straight forward of extracting the total size of uploads per user.</p>
  
  <p>For example, I do want free users only to be allowed uploading max 500MB, while members get say 4GB.</p>
  
  <p>Haven't found anything useable on Github or Google, in general, perhaps someone can share their knowledge. Cheers !</p>
  
  <p>Current code: </p>
  
  <p><strong>photo.rb</strong></p>
  
  <pre><code>class Photo &lt; ApplicationRecord
    include ImageUploader[:image]
  
    before_create :set_name
  
    belongs_to :employee
    belongs_to :user
  
    private
  
      def set_name
        self.name = "Photo"
      end
  end
  </code></pre>
  
  <p><strong>image_uploader.rb</strong></p>
  
  <pre><code>require "image_processing/mini_magick"
  
  class ImageUploader &lt; Shrine
    include ImageProcessing::MiniMagick
    plugin :determine_mime_type
    plugin :remove_attachment
    plugin :store_dimensions
    plugin :validation_helpers
    plugin :pretty_location
    plugin :processing
    plugin :versions
  
    Attacher.validate do
      validate_max_size 5.megabytes, message: "is too large (max is 5 MB)"
      validate_mime_type_inclusion %w[image/jpeg image/png], message: " extension is invalid. Please upload JPEGs, JPGs and PNGs only."
      validate_min_width 400, message: " must have a minimum width of 400 pixel."
      validate_min_height 250, message: " must have a minimum height of 250 pixel."
    end
  
    process(:store) do |io, context|
  
      size_800 = resize_to_limit!(io.download, 800, 800)
      size_300 = resize_to_limit!(io.download, 300, 300)
  
      {original: io, large: size_800, small: size_300}
    end
  end
  </code></pre>