migrating legacy images into cloudinary using Rails console
I have a ruby on rails website that stores images via urls. I want to import these images into cloudinary using active storage using the console.
Currently, the cloudinary gem works on my console:
Cloudinary::Uploader.upload("https://website.com/image.jpg")
However, cloudinary using active storage does not:
profile = Profile.new
profile.avatar = profile.legacy_avatar.url(:full)
profile.save
My model for Profile is here:
class Profile < ApplicationRecord
has_one_attached :avatar
end
The above code does not work for some reason. Is there any way to store to active storage from the console?
-
@Steve
A few clarification questions, when you refer to console, you mean the rails console, and not the Cloudinary console?
Second, I assume this may have been done but I would rather check. When you added Active Storage, did you also reference Cloudinary as the storage service (https://cloudinary.com/documentation/rails_activestorage#active_storage_configuration) ?If you wish to share personal details about your project, please feel free to open a support ticket here:
https://support.cloudinary.com/hc/en-us/requests/new
I am looking forward to your update. -
> when you refer to console, you mean the rails console, and not the Cloudinary console?
I meant to say "rails console".> did you also reference Cloudinary as the storage service
Yes, I referenced cloudinary as the storage service in the config file. I am able to store cloudinary files from the rails console file. For instance, "Cloudinary::Uploader.upload('https://website.com/image.jpg')" works fine. -
I was able to save to cloudinary using active storage on ruby on rails from a weblink by using the following code. I am putting something here so others can benefit:
require 'open-uri'
theImage = open("https://website.com/image.png")
profile = Profile.new
profile.avatar.attach(io: theImage, filename: 'image.png')
profile.saveDoes anyone know how to set the public key from commandline though?
-
Hi Steve,
You cannot specify an upload preset or other upload parameters on a per-upload basis while using Active Storage. There are however two ways to globally specify upload parameters for all uploads:
- As an upload preset (or upload parameters) in the Rails service configuration (as top-level parameters)
- As a default upload preset specified via the console which will be applied to all uploads without any extra code on the Rails side
Please note It is possible to pass Upload API parameters in your ActiveStorage calls by configuring those parameters in your
config/storage.yml
file - https://cloudinary.com/documentation/rails_activestorage#active_storage_configuration - such as passing "folder: testing" on a new line below "service: Cloudinary" - will apply that for all uploads and store the assets in that folder. You can also pass an upload preset that contains different settings to apply for each upload.Thanks, Wissam
Please sign in to leave a comment.
Comments
6 comments