Rails 4, Carrierwave-aws, images uploaded to Amazo

2019-07-14 00:42发布

I have a Rails 4 application hosted on openshift. I am using carrierwave and the carrierwave-aws gem to handle image upload. When I test it locally, the images are uploaded and displayed as expected to Amazon S3. However, on production server, which is hosted on Openshift, the images are uploaded to '/uploads/images' instead of Amazon. Here are my configurations and Gemfile:

gem 'carrierwave'
gem 'carrierwave-aws'

In initializers/carrierwave.rb

#config/initializers/carrierwave.rb
CarrierWave.configure do |config|

  config.storage    = :aws
  config.aws_bucket = 'mybucketname'
  config.aws_acl    = :public_read
  config.asset_host = 'https://mybucketname.s3-us-west-1.amazonaws.com'
  config.aws_authenticated_url_expiration = 60 * 60 * 24 * 365

  config.aws_credentials = {

    # Configuration for Amazon S3
    :provider              => 'AWS',
    :access_key_id     => 'myaccessid',
    :secret_access_key => 'mysecretkey',
    :region                => 'us-west-1',
  }

   config.storage = :aws
   config.cache_dir = "#{Rails.root}/tmp/uploads"                

end

In image_uploader.rb I also put

 storage :aws

Just in case this helps: I used Fog before and locally it also works fine. However on production it gives an Excon error. After some googling I come to the conclusion that carrierwave-aws is a better choice.

1条回答
小情绪 Triste *
2楼-- · 2019-07-14 01:04

use gem 'fog' and gem 'carrierwave' use this snippet

CarrierWave.configure do|config|
 config.fog_credentials = {
  provider:              'AWS',
  aws_access_key_id:     'AWS_ACCESS_KEY',
  aws_secret_access_key: 'AWS_SECRET_KEY',
  region:                'region-name',
  host:                  's3.example.com',
  endpoint:              'https://s3.example.com'
 }
 config.fog_directory = 'name of the bucket'
 config.fog_public = 'false'
 config.fog_attributes = {'Cache-Control' => "max-age=#{365.to_i}" }
end
查看更多
登录 后发表回答