Can I control the use of Amazon S3 resources with the Ruby on Rails app?

Let's say I have a web application that allows users to upload images and documents, and my application stores all these assets on S3, is there any way to control the use of PER user resources?

For example, if a user account has a storage limit of 1 GB, how can I control how much of this benefit anyone uses?

Also (but this is not a problem for me), if this account also has a 5 GB bandwidth limit, are there any tools available that allow me to control only their S3 bandwidth?

+5
source share
1 answer

Yes it is possible. You can use papreclip to control file downloads (or any other authoritative plugin / download control stone). Most of these tools give you access to the size of the downloaded file. You can just save these files in the database along with asset_uri (which I assume you already have) and check if the user can upload another file, just sum all the sizes of all assets with the corresponding user_id.

Users:
  id
  email_address
  first_name
  upload_limit

Assets:
  id
  user_id
  uri
  filesize
  filename

Then, to capture the total size of the downloaded files by a specific user, you can do:

class User < ActiveRecord::Base
  has_many :assets

  #Check if the user can upload another file
  def can_upload?
    if Asset.sum('filesize', :conditions => 'user_id = #{self.id}') >= self.upload_limit
      return false
    else
      return true
    end
  end

  #See the user used storage space
  def used_storage
    return Asset.sum('filesize', :conditions => 'user_id = #{self.id}')
  end

  #See how much space the user has remaining
  def available_storage
    if self.can_upload?
      return self.upload_limit - Asset.sum('filesize', :conditions => 'user_id = #{self.id}')
    else
      return 0
    end
  end
end

, ActiveRecord sum , . Ruby.

+2

All Articles