我正在使用carrierwave gem來管理我的rails 3應用程序中的文件上傳,但是,我無法連接到我的amazon s3存儲桶。Carrierwave和amazon s3
我按照維基上的說明,但他們不夠詳細,例如我在哪裏存儲我的s3憑證?
我正在使用carrierwave gem來管理我的rails 3應用程序中的文件上傳,但是,我無法連接到我的amazon s3存儲桶。Carrierwave和amazon s3
我按照維基上的說明,但他們不夠詳細,例如我在哪裏存儲我的s3憑證?
把這樣的東西放在初始化器中。
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
如果你想(而且代碼是私人的),你可以將你的憑證存儲在文件中。或者從一個單獨的文件或數據庫,由您決定。以下將加載配置文件並允許基於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']
}
退房this quick blog post我寫了關於如何做到這一點。基本上有幾個步驟,其中的每一個相當複雜:
希望這有助於!