aar*_*ell 5 ruby ruby-on-rails amazon-s3
比方说,我有一个允许用户上传图像和文档的Web应用程序,我的应用程序将所有这些资产存储在S3上,有没有办法监控资源使用情况的PER用户帐户?
例如,如果用户帐户的存储空间限制为1GB,我该如何监控任何个人使用的限额?
此外(但这对我来说不是问题)如果该用户帐户也有5GB带宽限制,是否有可用的工具可以监控他们的S3带宽?
是的,这是可能的。您可以使用papreclip来管理文件上传(或任何其他信誉良好的上传管理插件/gem)。大多数这些工具都可以让您访问上传文件的文件大小。您可以简单地将这些文件与 asset_uri 一起存储在数据库中(我想您已经存储了),并检查用户是否可以上传另一个文件,只需将所有资产的所有大小与适当的 user_id 相加即可。
Users:
id
email_address
first_name
upload_limit
Assets:
id
user_id
uri
filesize
filename
Run Code Online (Sandbox Code Playgroud)
然后,要获取特定用户上传文件的总大小,您可以执行以下操作:
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's 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
Run Code Online (Sandbox Code Playgroud)
您可以看到我在这里使用ActiveRecord sum函数来进行计算。您可以轻松地使用地图或其他一些基于 Ruby 的解决方案。
| 归档时间: |
|
| 查看次数: |
699 次 |
| 最近记录: |