如何使用Rails和Paperclip在Google云端存储上存储照片?

问题描述 投票:12回答:2

到目前为止,我一直在使用Amazon S3存储用户的文件。

这里需要做的就是:

  1. 指定存储桶的Amazon S3凭据
  2. 'aws-sdk' gem添加到Gemfile中
  3. 在模型中:

  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"

设置Amazon S3适配器。这就是全部了。

但是如何设置Google云引擎呢?到目前为止,我发现只有fog gem ,我可以使用。

但是,我应该如何配置模型以自动将所有上传的文件存储在Google服务器上?

ruby-on-rails ruby paperclip google-cloud-storage
2个回答
28
投票

好的,所以我这样做了:

的Gemfile:

gem 'fog'

config / ce.yml:

development:
  provider: Google
  google_storage_access_key_id: XXX
  google_storage_secret_access_key: XXX

模型:

  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"

0
投票

对于Rails> 5.2,可以使用Active Storage。文档是一个很好的开始。

在config / environments / production.rb中:

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

在config / storage.yml中:

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

在您的用户模型中:

class User < ApplicationRecord
  has_one_attached :avatar
end
© www.soinside.com 2019 - 2024. All rights reserved.