Rails 4,Carrierwave-aws,在本地上传到Amazon s3的图像,但不在生产中(Openshift) [英] Rails 4, Carrierwave-aws, images uploaded to Amazon s3 locally, but not in production (Openshift)

查看:82
本文介绍了Rails 4,Carrierwave-aws,在本地上传到Amazon s3的图像,但不在生产中(Openshift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在openshift上托管了一个Rails 4应用程序.我正在使用carrierwave和carrierwave-aws gem处理图像上传.当我在本地对其进行测试时,图像将按预期上传并显示到Amazon S3.但是,在Openshift托管的生产服务器上,图像会上传到"/uploads/images"而不是Amazon. 这是我的配置和Gemfile:

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'

在initializers/carrierwave.rb

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

在image_uploader.rb中,我也放了

In image_uploader.rb I also put

 storage :aws

以防万一这有帮助:我以前使用过Fog,在本地也可以正常使用.但是,在生产中会出现Excon错误.经过一番谷歌搜索后,我得出的结论是,载波电波振荡器是一个更好的选择.

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.

推荐答案

使用gem'fog'和gem'carrierwave'使用此代码段

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

这篇关于Rails 4,Carrierwave-aws,在本地上传到Amazon s3的图像,但不在生产中(Openshift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆