I'm working on a Ruby on Rails web app. The user can upload files and they get stored to Amazons S3. For file uploads I use the paperclip gem.
How can I encrypt files with AES256 before they get saved? I know S3 has server side encryption, but that doesn't really work for me because I'm opening the site in the mobile app and would like to handle decryption on the client.
I know I can use the paperclip processors or the before_post_process methods but how can I get the file that is being uploaded and change it?
Look at this paperclip recipe on asynchronous upload to S3. You could use that and then change the callback code to:
def upload_to_s3
self.remote_avatar = encrypt(local_avatar.to_file)
self.local_avatar = nil
self.save!
end
Where the method encrypt is the AES256 function.
It may be worth looking into this add-on gem for CarrierWave if you are not set on paperclip, it might save you some time.
Related
I'm using vanilla Rails Active Storage file upload with multiple:true option. The files are stored on S3. The setup is working well. However, I was thinking for very large files it would be beneficial to implement Multipart Upload for optimal speed and reliability.
I found a description of AWS S3 multipart upload here: https://docs.aws.amazon.com/AmazonS3/latest/dev/mpuoverview.html
I also found a Ruby specific page: https://docs.aws.amazon.com/AmazonS3/latest/dev/uploadobjusingmpu-ruby-sdk.html
However, I couldn't find any reference on how to implement this feature with Rails and Active Storage.
I would like to receive some direction on how best to go about implementing multipart upload without ripping out Active Storage if possible.
In case somebody is looking for an answer on this. Active Storage will support multipart upload starting from Rails 6.1. Active Storage direct upload automatically switches to multipart for large files. No settings changes are required.
You can customise the threshold for what is considered a large file. The default is 100MB, and you can change the default by adding this to your storage.yml under the amazon settings:
upload:
multipart_threshold: <%= 250.megabytes %>
Reference: https://github.com/rails/rails/blob/master/activestorage/CHANGELOG.md
Is it possible to use carrier wave to upload directly to amazon's S3 without using my server?
What i mean is, I don't want the images first going to my ec2 instance, and then uploaded to s3. I believe there is a way to upload directly to S3 to save my server's resources from having to process/stream the file.
I am just looking into carierwave, does it support nice html5 uploads where the user can just drag and drop the file on the web page?
If you want to upload directly to S3 from the browser you must do it with Javascript.
Heroku provides a nice tutorial : https://devcenter.heroku.com/articles/direct-to-s3-image-uploads-in-rails
Once uploaded, you can pass the finale S3 public URL of the image in a hidden field and download it server-side with carrierwave for further manipulation (resizing, ...)
i am using paperclip gem to upload images on the server side, i need to get updated with the bytes loaded on server through the upload process.
my aim is to update the progress bar accordingly. how do I do this?
You can only do this by using a Flash-based uploading tool which has direct access to the file and the upload stream. Just integrate it with Paperclip (and there are a ton of examples on google showing how to)
I would recommend one of these:
http://plupload.com/
http://www.uploadify.com/
i am having some problem integrating Paperclip with non-us S3 server. Paperclip seem to assume that the S3 server is in us and return back a url that is at http://s3.amazonaws.com/path/to/my/file.
My question is how to change it to point to a non-us S3 server(singapore for example)? The files are uploading, i just need to get paperclip to return a correct path.
Using :
paperclip-2.4.5
aws-s3-0.6.2
Tian Wei, check this out:
http://techspry.com/ruby_and_rails/amazons-s3-european-buckets-and-paperclip-in-rails-3/
But a better answer would be just use the US one, and avoid the hassle.
Server side is Rails.
Client side is Flash, users will upload directly to S3
I need a flexible way to generate S3 policy files, base64 encode them, and then distribute the resulting signed policy to the client.
Is there a good library/gem for this, or do I need to roll my own?
I'll be using paperclip to store the file, as per:
http://www.railstoolkit.com/posts/fancyupload-amazon-s3-uploader-with-paperclip
I've had a look at:
https://github.com/geemus/fog
https://github.com/jnicklas/carrierwave
https://github.com/marcel/aws-s3
These look like they'll help me get bits done, but I can't tell if they'll help me generate flexible policies.
EDIT: Going to give the "Generate an upload signature..." bit here a shot:
http://www.kiakroas.com/blog/44/
Here is a sample project for how to do this using Rails 3, Flash/Silverlight/GoogleGears/BrowserPlus and jQuery-based Plupload to upload directly to S3: https://github.com/iwasrobbed/Rails3-S3-Uploader-Plupload