本文介绍了载波和亚马逊S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用carrierwave gem在Rails 3应用程序中管理文件上传,但是,我无法连接到Amazon s3存储桶.

I am using the carrierwave gem to manage file uploads in my rails 3 app, however, I am not able to connect to my amazon s3 bucket.

我已按照Wiki上的说明进行操作,但不够详细,例如,我在哪里存储s3凭据?

I have followed the instructions on the wiki yet they are not quite detailed enough, for example where do I store my s3 credentials?

推荐答案

在初始化程序中放入类似的内容.

Put something like this in an initializer.

CarrierWave.configure do |config|
  config.storage = :fog
  config.fog_directory = 'your_bucket'

  config.fog_credentials = {
    :provider => 'AWS',
    :aws_access_key_id => 'your_access_key'
    :aws_secret_access_key => 'your_secret_key',
    :region => 'your_region'
  }
end

如果需要,您可以将凭据直接存储在文件中(并且代码是私有的).或从单独的文件或数据库,由您决定.以下将加载配置文件并允许基于环境的不同配置.

You can store your credentials right in the file, if you want (and the code is private). Or from a separate file, or the database, up to you. The following would load a config file and allow different configurations based on the env.

# some module in your app
module YourApp::AWS
  CONFIG_PATH = File.join(Rails.root, 'config/aws.yml')

  def self.config
    @_config ||= YAML.load_file(CONFIG_PATH)[Rails.env]
  end
end

# config/aws.yml
base: &base
  secret_access_key: "your_secret_access_key"
  access_key_id: "your_access_key_id"
  region: your_region
development:
  <<: *base
  bucket_name: your_dev_bucket
production:
  <<: *base
  bucket_name: your_production_bucket

# back in the initializer
config.fog_directory = YourApp::AWS.config['bucket_name']
# ...
config.fog_credentials = {
  :provider => 'AWS',
  :aws_access_key_id => YourApp::AWS.config['access_key_id'],
  :aws_secret_access_key => YourApp::AWS.config['secret_access_key'],
  :region => YourApp::AWS.config['region']
}

这篇关于载波和亚马逊S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:18