8
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

heroku+rails+ckeditorで画像やファイルをアップロードすると、herokuにpushする度にファイルが消えてしまう問題

Posted at

(当たり前な話だけど初herokuだったので一瞬あれ?って思った)

##掲題の通りなのでAWSのS3にアップロードすればいいかなという事で以下を参考にした

gem 'fog', '~> 1.3.1'

fogを追加してbundle install

app/uploaders/ckeditor_attachment_file_uploader.rb
  # Choose what kind of storage to use for this uploader:
 # storage :fileと記述されているのをfogに変更
  storage :fog
app/uploaders/ckeditor_picture_uploader.rb
  # これも同じく
  storage :fog

initializersにcarrierwave.rbというファイルを作成して

config/initializers/carrierwave.rb
CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',
    :aws_access_key_id      => '[id]',
    :aws_secret_access_key  => '[key]',
    :region                 => 'ap-northeast-1' #Tokyo
  }
  config.fog_directory = '[bucket-name]'
end

こんな感じでローカルでは動作。

herokuのENVにも追加しておく。

heroku config:add AWS_S3_KEY_ID="[id]" AWS_S3_SECRET_KEY="[key]" AWS_S3_BUCKET="[bucket-name]"
8
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?