How to Use Rails and Paperclip to Store Photos on Google Cloud Storage

How to use Rails and Paperclip to store photos on Google Cloud Storage?

Ok, so I made it work this way:

Gemfile:

gem 'fog'

config/gce.yml:

development:
provider: Google
google_storage_access_key_id: XXX
google_storage_secret_access_key: XXX

model:

  has_attached_file :avatar, 
:styles => { :big => "100x100#", :thumb => "25x25#" },
:storage => :fog,
:fog_credentials => "#{Rails.root}/config/gce.yml",
:fog_directory => "your bucket name",
:path => ":rails_root/public/users/:id/:style/:basename.:extension",
:url => "/users/:id/:style/:basename.:extension"

Uploading an image to google cloud storage on a rails site

Mmm, assuming you've set up the forms correctly according to Paperclip readme, you should only need to assign the image_data to :avatar

itemcollection.avatar = params[:itemcollection_image_data]
itemcollection.save

Paperclip or Google Cloud Storage issue when renaming paths

Your guess is right. Changing the config is not enough, you need to move the files. This, is better left for a rake task or background job. I have some code for S3, but it should give you an idea of how to implement it for Google:

  def old_key(image, file_name_field)
# Previous `:path`: '/:class/:attachment/:id/:style/:filename'
klass = self.class.to_s.pluralize.downcase
attachment = image.pluralize
"#{klass}/#{attachment}/#{id}/original/#{send(file_name_field)}"
end

def re_path(image)
file_name_field = "#{image}_file_name"
return if send(file_name_field).blank?

old_object = bucket.object(old_key(image, file_name_field))
return unless old_object.exists?

Rails.logger.warn "Re-saving image attachment #{self.class}/#{id}"
send "#{image}=", URI.parse(old_object.public_url)
save
end

I'm basically building the old path using my own interpolation, finding the object in S3 (hence key/object lingo) and re-download every image from S3. Be careful with this, since you might incur in extra cost for downloading rather than just moving, if that's someone Google allows.

Then I just called this method on every image for every object:

Object.each do { |o| o.re_path(:logo); o.re_path(:background); }

uploading to google drive with paperclip in Rails

You'd likely have to implement a storage adapter for Paperclip. Take a look at something like https://github.com/dripster82/paperclipdropbox as an example. In that case, it looks like it only supports uploading to a single pre-authorized account. If that's OK, then modifying that to talk to Google Drive instead of Dropbox should be straightforward. If you need to support multiple users, you'd have to make sure the right credentials are being used for the current user.

Anyway, the paperclip docs are likely the best place to get started :)

Paperclip validation issue on production

Okay I found a result. I just created a initializers/paperclip.rb file

require 'paperclip/media_type_spoof_detector'
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end

Right now it work's perfectly for me.

If you have problems with ImageMagick on App Engine using Rails see this link



Related Topics



Leave a reply



Submit