到目前为止,我一直在使用Amazon S3存储用户的文件。
这里需要做的就是:
'aws-sdk'
gem添加到Gemfile中 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服务器上?
好的,所以我这样做了:
的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"
对于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