Upload system-generated file to S3 using ActiveStorage? - ruby-on-rails

I have a need to upload a system-generated file to S3. I'm aware that I could use something like the AWS Ruby SDK for this. However, I'm already using ActiveStorage elsewhere in my application and it seems simpler, if possible, to use ActiveStorage for everything rather than ActiveStorage plus some other library.
Is this possible using ActiveStorage?

I was able to find the answer in this part of the documentation:
#message.image.attach(io: File.open('/path/to/file'), filename: 'file.pdf')

Related

Processing file before upload using ActiveStorage

How would I process a file before it's uploaded using activestorage. I need to be able to modify an svg file's content before it actually gets uploaded to S3. Can't seem to find any callbacks.
There is no way to do this natively with ActiveStorage. It's the major drawback with using ActiveStorage.
As far as I know, the only way to modify an upload is to create a variant of the original upload after it is created...which creates a (completely different) variant image based on the image that was originally uploaded.
ActiveStorage is easy to setup but, after using it with a few applications, Carrierwave..etc seem like better options.
In addition, if you want to upload in a background job, ActiveStorage is a pain.

Can I use zipline gem to download from s3 without model associations with paperclip or carrierwave

I want to allow my user to download a bundle of files that are stored on s3 using the zipline gem. The files are already hosted on an s3 server but they aren't there as part of a paperclip or carrierwave attachment in my app. Will I need to create some records in my database to sort of trick zipline into thinking they are paperclip attachments, or is there a way I can send the zip file without bothering with an attachment gem? At the moment, trying to download the files with zipline doesn't throw an error message at all. It just seems to skip right over and nothing downloads.
See the part of the zipline README where an enumerator gets used to include remote files into the ZIP. It uses absolute URLs, to generate those from your S3 objects you will need to use presigned URLs (which Zipline is going to pass on to Curb):
Aws::S3::Bucket.new(your_bucket_name).object(your_key).presigned_url(:get)

Rake task creates CSV and then upload to S3. How?

I have a rake task that creates a CSV file. Right now I am storing this file into my /tmp folder (I am using Heroku). This CSV file is not associated with any model, it's just some data I pull from several APIs, combined with some data from my models.
I would like to download this file from Heroku, but this seems not possible. So, my question is: Which gem am I trying to look for in order to upload that file to Amazon S3? I have seen gems like Paperclip, but that seems to be associated with a model, and that is not my case. I just want to upload that CSV file that I will have in /tmp, into my Amazon S3 bucket.
Thanks
You can use aws-s3 gem
S3Object.store('filename_in_s3.txt', open("source_file.tmp"), 'bucket_name')
You should define the exact path of your tmp file, for example:
open("#{Rails.root}/tmp/source_file.tmp")
CarrierWave can directly interface your Ruby application with S3 directly via the Fog library. Rather than operating on the model level, CarrierWave utilizes a Uploader class where you can cull from across your APIs and datasources, which is precisely what you're trying to accomplish.

What should i use for rails file attachment and jruby?

Normally I'd use carrierwave, but they do not officially support jruby, and I've been running in to bugs, possibly related. (https://github.com/jnicklas/carrierwave/issues/620, Image corruption on upload to s3, production only. (carrierwave, engineyard))
Have others had success here?
I'm considering trying out paperclip, but it looks like that may not be perfect either-- https://github.com/thoughtbot/paperclip/issues/100
Try dragonfly it's a really great library to manage you file attachment.
check out this guide, it's cake
http://webtempest.com/how-to-allow-image-uploads-in-rails-on-heroku/
It uses paperclip and amazon web s3 gems

Uploading to s3, using s3 servers

Does anyone have any sample code (preferrably in rails) that uploads to s3, using s3's servers.
Again, uploading directly to s3, where the actual upload/streaming is also preformed on amazon's servers.
Requirements:
Plupload, jQuery
Idea:
Authorize Upload via your app (sign it on server-side)
Use the signed request to upload the file to S3
Notify your app that the upload is done
Check whether S3 has received the file
I posted the code as a gist at https://gist.github.com/759939, it misses commments and you might run into some issues due to missing methods (had to rip it from our codebase).
stored_file.rb contains a model for your DB. Has many of paperclips helper methods inlined (which we used before we switched to direct upload to S3).
I hope you can use it as a sample to get your stuff running.
If you are using Rails 3, please check out my sample projects:
Sample project using Rails 3, Flash and MooTools-based FancyUploader to upload directly to S3: https://github.com/iwasrobbed/Rails3-S3-Uploader-FancyUploader
Sample project using Rails 3, Flash/Silverlight/GoogleGears/BrowserPlus and jQuery-based Plupload to upload directly to S3: https://github.com/iwasrobbed/Rails3-S3-Uploader-Plupload
To simply copy files, this is easy to use:
Smart Copy Script into S3
Amazon wrote a Ruby library for the S3 REST API. I haven't used it yet.
http://amazon.rubyforge.org/

Resources