Imagine the app with the ability to upload files by users. Only the author (the person who uploaded this file) and the author's friends should be able to see this uploaded file.
I am using "carrierwave" gem to handle uploading files. It gives me the ability to get a file URL.
# models/user.rb
class User
mount_uploader :secret_file, SecretFileUploader
end
# uploaders/secret_file_uploader.rb
class SecretFileUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
def extension_white_list
%w[txt]
end
def store_dir
"secret_files"
end
end
u = User.last.secret_file.url # => "https://bucket-name.s3.amazonaws.com/secret_files/secret_file_name.txt"
If someone stoles this S3 URL, then they can access this file at any time.
How to handle this situation? How to authorize only the author and the author's friends to open this file?
To ensure uploaded content is only visible to the users and its associated friends, you need to make sure few things -
once done, you can use something in your views like - @user.images to view all images of a user. you can extend this with any logic to show all list of images to the users friends.
Try it out and let me know if it helps.
To know more about how to avoid unwanted access to your files in AWS S3, CHECK THIS LINK