簡體   English   中英

Heroku CarrierWave S3-無法弄清楚如何保存圖片

[英]Heroku CarrierWave S3 - can't figure out how to save picture

我需要使用“ aws.amazon.s3”服務保存圖片。 在gem文件中,我有:

gem 'carrierwave'
gem 'rmagick'
gem 'fog'
gem 'sprockets-rails'

我想使用devise注冊控制器向用戶添加頭像。 我這樣做:

1)rails g migration add_avatar_to_users頭像:字符串;
耙db:migrate

2)添加了這一行:

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
end

3)創建應用程序/上傳器/ avatar_uploader

# encoding: utf-8

class AvatarUploader < CarrierWave::Uploader::Base

include CarrierWave::RMagick

# Choose what kind of storage to use for this uploader:
#storage :file
 storage :fog

# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
 def store_dir
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
 end

# Create different versions of your uploaded files:
 version :thumb do
 process resize_to_fill: [140, 140]
 process convert: 'jpeg'
# process :resize_to_fit => [140, 140]
end
# For images you might use something like this:
 def extension_white_list
   %w(jpg jpeg gif png)
 end
end

4)創建app / config / init / carrierwawe.rb

CarrierWave.configure do |config|

config.storage = :fog
config.fog_directory  = "app.#{Rails.env}"  # required,
if Rails.env.production?
  config.fog_credentials = {
    :provider               => 'AWS',                                              # required
    :aws_access_key_id      => ENV["AWS_ACCESS_KEY"],                        # required
    :aws_secret_access_key  => ENV["AWS_SECRET_KEY"],                        # required
    :region                 => 'eu-west-1'
}
config.cache_dir = "#{Rails.root}/tmp/uploads"
config.fog_directory  = ENV["AWS_BUCKET"]                                    # required
config.fog_public     = false


config.fog_attributes = {'Cache-Control'=>'max-age=60'}
config.fog_public     = false               # (optional) public readability,       defaults to true
config.cache_dir = Rails.root.join('tmp','uploads')
config.root = Rails.root.join('tmp')
else
config.fog_credentials = {
    :provider               => 'AWS',
    :aws_access_key_id      => '',
    :aws_secret_access_key  => '',
    :region                 => 'eu-west-1'
}
config.cache_dir = '/home/vagrant/uploads_tmp/tmp/uploads'
config.root = '/home/vagrant/uploads_tmp/tmp'
 end
end

5)我的application_controller(添加-:用戶名和:avatar,:avatar_cache):

def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password,
                                                         :password_confirmation, :remember_me , :avatar, :avatar_cache ) }
devise_parameter_sanitizer.for(:account_update) {|u| u.permit(:username, :email, :password,
                                                               :password_confirmation, :current_password , :avatar, :avatar_cache ) }
end

6)查看:views / devise / registration / new.html.haml

= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name),html: { role: "form" , :multipart => true }) do |f|
      .form-group
    = f.label :avatar do
      = f.file_field :avatar , class: "form-control"
      = f.hidden_field :avatar_cache

7)查看:views / devise / registration / edit.html.haml

      .form-group
    - if current_user.avatar.url.present?
      = image_tag(current_user.avatar.url)
      -#= f.label :remove_avatar do
      -#  = f.check_box :remove_avatar
    = f.file_field :avatar,  class: "form-control"
    = f.hidden_field :avatar_cache

8)路線

devise_for :users
resources  :dashboard
resources  :users
root to: "users#show"

當我轉到http://127.0.0.1:3000/users/sign_in

並在此推送提交后填寫所有字段。 控制台告訴我...

在此處輸入圖片說明

我弄清楚了它應如何在Heroky和S3文件上工作:u_proj \\ config \\ initializers \\ carrierwave.rb

CarrierWave.configure do |config|

if Rails.env.production?
  config.fog_credentials = {
      # Configuration for Amazon S3
      :provider              => 'AWS',
      :aws_access_key_id     => ENV['AWS_ACCESS_KEY'],
      :aws_secret_access_key => ENV['AWS_SECRET_KEY']
  }
  config.fog_directory     =  ENV['S3_BUCKET_NAME']
  config.storage           =  :fog
else
  config.fog_credentials = {
      :provider               => 'AWS',
      :aws_access_key_id      => '',
      :aws_secret_access_key  => '',
      :region                 => 'eu-west-1'
  }
  config.storage = :file
  config.cache_dir = "#{Rails.root}/public/tmp"
end
end

用這個:

heroku config:set AWS_ACCESS_KEY_ID= add a-key
heroku config:set AWS_SECRET_ACCESS_KEY= add s-a-key
heroku config:set S3_BUCKET_NAME= add b-name

然后提交並推送到heroku就是這樣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM