I recently implemented file uploading using carrier-wave with Amazon S3 storage.
I want to generate, or make available, the S3 URL only for one hour. After that the link should expire.
How can I do this using carrier-wave?
I recently implemented file uploading using carrier-wave with Amazon S3 storage.
I want to generate, or make available, the S3 URL only for one hour. After that the link should expire.
How can I do this using carrier-wave?
The way to handle this is to use a presigned URL for the S3 file. Once you upload the file using carrier wave, you access the actual S3 URL and use AWS::S3 to presign it with an expiration time. For example, if the key (file name) in your S3 bucket is "my_file", you could do this:
# Your Model
def presigned_url
s3 = AWS::S3.new
bucket = s3.buckets["MyBucket"]
object = bucket.objects["my_file"]
object.url_for(:read, secure:true, expires:1.hour)
end
The URL returned will be valid for 1 hour and then will never work again.
To use this, you will need to include the aws-s3 gem in your Gemfile:
# Gemfile
gem "aws-s3"
AccessDenied