【问题标题】:How to use Rails and Paperclip to store photos on Google Cloud Storage?如何使用 Rails 和 Paperclip 在 Google Cloud Storage 上存储照片?
【发布时间】:2014-07-31 12:21:58
【问题描述】:

到目前为止,我一直在使用 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 适配器。仅此而已。

但是如何设置谷歌云引擎呢?到目前为止,我只找到了可以使用的fog gem

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

【问题讨论】:

    标签: ruby-on-rails ruby paperclip google-cloud-storage


    【解决方案1】:

    好的,所以我让它这样工作:

    宝石文件:

    gem 'fog'
    

    config/gce.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"
    

    【讨论】:

    【解决方案2】:

    对于 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
    

    【讨论】:

    • 此信息适用于 ActiveStorage,不适用于 Paperclip。
    猜你喜欢
    • 2016-12-09
    • 2020-01-17
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多