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

强颜欢笑 提交于 2019-12-02 19:35:50

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"

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!