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

2

12

Until now, I have been using Amazon S3 for storing users' files.

All what has been needed to do here was:

  1. specify Amazon S3 credentials to the bucket
  2. add 'aws-sdk' gem to the Gemfile
  3. and in the model:

  has_attached_file :avatar, 
                    :styles => { :big => "100x100#", :thumb => "25x25#" },
                    :storage => :s3,
                    :s3_credentials => "#{Rails.root}/config/s3.yml",
                    :path => ":rails_root/public/users/:id/:style/:basename.:extension",
                    :url => "/users/:id/:style/:basename.:extension"

To set the Amazon S3 adapter. That was all.

But how to set up Google cloud engine? So far I found only the fog gem , which I could use.

However, how should I configure the model to automatically store all uploaded files on Google servers?

Wadley answered 31/7, 2014 at 12:21 Comment(0)
W
29

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"
Wadley answered 31/7, 2014 at 13:20 Comment(3)
For anyone wondering how to get the keys: fog.io/storage/#google-cloud-storageIntercellular
@user984621, I tried your solution, it works locally on dev environment, however, it might fail when you deploy it over to Heroku if you don't have the root setup correctly. For future "stackoverflow" users, try #33112028, if the above solution doesn't workLandman
I have some problems with paperclip and google cloud storage #36241372 can you help me please ? :(Rheumatic
S
0

For Rails > 5.2, Active Storage is available. The docs are a great place to start with.

In config/environments/production.rb :

# Store files on Google cloud storage.
config.active_storage.service = :google

In config/storage.yml :

google:
  service: GCS
  credentials: <%= Rails.root.join("path/to/keyfile.json") %>
  project: ""
  bucket: ""

In your User Model :

class User < ApplicationRecord
  has_one_attached :avatar
end
Spinney answered 11/3, 2019 at 9:38 Comment(1)
For information this is for ActiveStorage and not for Paperclip.Lattice

© 2022 - 2024 — McMap. All rights reserved.