Rails carrierwave and cloudinary multiple file uploads - ruby-on-rails

So I'm trying to get carrierwave to work with cloudinary for multiple file uploads but it keeps giving me this error that says:
undefined method `all_versions_processors' for Array
I followed the carrierwave documentation where I added a listing_images attribute to my Listings table which is of type json.
I also set the multiple to true option in the form file input.
And in my ListingsController I have specified as one of the permitted params the following:
listing_images: []
I'm pretty sure everything is configured properly but I can't figure out why this error is thrown. Any help would be greatly appreciated.

It's on the road-map to officially support multiple uploads with Carrierwave on Cloudinary's GEM. In the meantime, as a workaround, you can accomplish multiple uploads a bit differently. Here's a basic sample project that demonstrates it:
https://github.com/taragano/Cloudinary_multiple_uploads

Related

Shrine upload_options for checksum_algorithm giving an error

I'm new to S3 and Shrine, and I'm working with a Shrine uploader in Ruby on Rails to upload files to Amazon S3, which has been in place for a couple of years on this Rails app.
The thing I'm working on has a goal to have S3 generate a checksum when uploading files, and according to these docs for adding a "trailing checksum" the ChecksumAlgorithm needs to be used: https://docs.aws.amazon.com/AmazonS3/latest/userguide/checking-object-integrity.html
In the ruby SDK docs, it lists checksum_algorithm as a param.
https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Object.html#put-instance_method
When I add the param in the Shrine uploader (plugin :upload_options, { checksum_algorithm: 'SHA256' }) and upload the file, I get the error ArgumentError: unexpected value at params[:checksum_algorithm] from aws-sdk-core/param_validator.rb:33:in 'validate!' https://github.com/aws/aws-sdk-ruby/blob/version-3/gems/aws-sdk-core/lib/aws-sdk-core/param_validator.rb#L14.
I've tried different cases, with and without the dash, and anything else I can think of syntax-wise, but no luck.
It turns out I was using an older version of aws-sdk-s3 and updating the gem solved the problem. Thanks #Janko

How to tweet a jpeg/mp4 with the twitter gem in Rails through ActiveStorage S3 uploads?

I am trying to Tweet from my Rails App (7.0.1), Ruby (3.0.2). I am using the "twitter" gem and others to connect the Twitter account and publish to Twitter. Everything works fine as long as I am Tweeting text only. The Text is from "Model.content".
There is another "Model.media" that is an attachment with "has_one_attached" which I want to use for the media upload to Twitter. It is stored in AWS S3.
This is the how to Tweet text using the gem (just "content" and not "Model.content" because I am tweeting from the Model.rb itself):
client.update(content)
It works and posts to Twitter fine.
To Tweet with media I can do it like that this according to the gem:
client.update_with_media(content, media)
The errors I am getting from the Rails logs are:
/Users/~/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/activesupport-7.0.1/lib/active_support/core_ext/module/delegation.rb:307:in `method_missing': undefined method `merge' for #<ActiveStorage::Attached::One:0x00007fca71579048 #name="media", #record=#<...">> (NoMethodError)
If I try this:
client.update_with_media(content, Rails.application.routes.url_helpers.rails_blob_path(media, only_path: true))
The errors I am getting from the Rails logs are (same happens for .jpeg):
/Users/~/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/twitter-7.0.0/lib/twitter/rest/upload_utils.rb:40:in `append_media': undefined method `eof?' for "/rails/active_storage/blobs/redirect/....mp4":String (NoMethodError)
I tried and looked for many solutions, they weren't specifically for this issue if I understand correctly. I feel I am missing a really small thing.
What is the best approach for this issue and how?
Links:
Gem link: https://rubygems.org/gems/twitter
Gem quick guide: https://github.com/sferik/twitter/blob/master/examples/Update.md#update
According to the docs, media must be either a File or an array of Files. This method doesn't support direct uploads so you have to download the file from S3 first and then upload it to twitter.
rails_blob_path returns a string and that's why it doesn't work. Also, this path is relative to your application domain, so you can't directly use it to reference files on your filesystem.
ActiveStorage has an open method that yields a Tempfile. Fortunately they behave just like regular Files so you should be able to pass it to update_with_media. You can use it like so:
media.open do |file|
client.update_with_media(content, file)
end
It will create a temporary file on the filesystem.
Alternatively, if your file is relatively small (which might not be the case with videos) you can use the download method. It will download the file and store it in memory:
client.update_with_media(content, media.download)
Side note: POST update_with_media is deprecated and shouldn't be used anymore.

Carrierwave/fog are uploading files to S3, but the app is still trying to use the local path to access images

I know this is a broad question, and I'm biting off a little more than I can chew for a first stab at a rails app, but here I am.
I tried to add an image upload/crop to a basic status app. It was working just fine uploading the images and cropping them with carrierwave, but as soon as I started using Fog to upload to S3, I ran into issues.
The image, and it's different sizes, appear to be ending up on S3 just fine, but the app is still trying to access the image as "/assets/uploads/entry/image/65/large_IMG_0035.jpg"
Locally, it just shows a broken image, but on Heroku it breaks the whole thing because
ActionView::Template::Error (uploads/entry/image/1/large_IMG_0035.jpg isn't precompiled
The heroku error makes sense to me because it shouldn't be there. I've combed through the app but don't know what's forcing this. I'll post any code anybody thinks will work? Thanks in advance!
Clarification:
Just to clarify, the images are uploading to S3 fine, the problem is how the app is trying to display the image_url
The app is using a local path in the asset pipeline, not the S3 path that it's actually uploading to.
I was having the same issue. In my Carrierwave Initializer I was setting host to s3.amazonaws.com but when I removed that line altogether urls started working.
I hope this helps you resolve your issue, I fought this for several hours!
I believe this issue is related to how you are accessing your image in your view.
If you have mounted an uploader on the field avatar in the following manner:
class User < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
end
You would access it in your ERB as follows:
<%= image_tag(#user.avatar_url) %>
I would also suggest watching the following Railscast on the topic.
http://railscasts.com/episodes/253-carrierwave-file-uploads
Re-reading issue, I bet it has to do with Carrierwave using Herkou.
Give this a glance and see if it helps.
https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Make-Carrierwave-work-on-Heroku
I am not clear what exactly do you want to achieve.
But for now I have 2 ideas:
For assets host in CDN, you can take a look at this:
https://devcenter.heroku.com/articles/cdn-asset-host-rails31
If you want the images to be part of a model-relation, here's my rough idea:
Put the images path in a table column.
For further information about this you can browse carrierwave github site.(It has many docs and tutorial)

Uploading Images Directly to Cloudinary

I'm trying to upload images directly to Cloudinary from my Rails app, since I'm on Heroku and can't use the server as an intermediary. They have support for this using jQuery, but when I follow their directions, I get the following error jquery.fileupload-fp.js:
Uncaught TypeError: Cannot read property 'fileupload' of undefined.
$.widget('blueimp.fileupload', $.blueimp.fileupload, {
Cloudinary replied with an email saying that I should include the js files in the correct order, but they are all included and in the right order. I asked them again, but they might take a while to get back to me and this is somewhat time sensitive.
Thanks!

Paperclip upload to S3 is failing silently...help!

I have an application which uploads images to an S3 bucket using Paperclip. It's been working fine for months, but suddenly my files are not being uploaded to the S3 bucket. Unfortunately, I've been doing a refactoring in a number of unrelated areas, and it's possible that something I changed broke my upload.
I'm using paperclip 2.3.1.
That said, there are a number of confusing aspects to this and frankly I am at a loss. First, there are no errors in the log indicating that the upload failed. The paperclip attachment attributes are populated in the database. The application thinks the upload occurred successfully. But when I look in S3, the file is not there.
Second, I have an almost identical attachment on a different model, which uploads to the same S3 bucket successfully -- the code is almost identical, and there clearly cannot be a permissions issue.
I found references in several places that suggested removing the right_aws game and instead only having the aws_s3 gem...which I did...but to no avail. Moreover, I never saw the (5 for 4) error in my log regardless.
Does anyone have any suggestions on how I can further diagnose this? Are there any options in paperclip to increase the verbosity of logging?
Thanks!
I had this issue as well and the cause was that my :multipart => true key/value hadn't been nested correctly in the :html key of the form_for helper.
It turns out the app was using Paperclip 2.3.4 which introduced some S3 issues.
Upgrading to 2.3.5 solved the issue for me.

Resources