![](/img/trans.png)
[英]Using different S3 buckets for production and development in Carrierwave
[英]Different S3 buckets for staging app vs. production app
我有一個使用Amazon S3存儲Paperclip附件的Rails應用程序。 我的database.yml
文件為開發,測試和生產指定了不同的S3存儲桶。
我在Heroku上有兩個應用程序 - 一個生產應用程序和一個“臨時應用程序”,用於在平台預啟動時測試代碼。
目前的系統存在一個重要缺陷 - 它使用相同的S3桶進行分段和生產。 如何設置我的系統以便它使用不同的存儲桶,具體取決於我是否編寫git push production master
或git push staging master
?
Heroku允許您通過持久環境變量配置您喜歡的任何內容,應用程序中的每個dyno / process都可以使用它。 環境變量不在同一應用程序的登台版和生產版之間共享。 利用這一點。
has_attached_file :photo,
:styles => ...,
:path => ...,
:storage => :s3,
:bucket => ENV['S3_BUCKET'], # <--- over here
:s3_credentials => {
:access_key_id => ENV['S3_KEY'],
:secret_access_key => ENV['S3_SECRET']
}
然后:
# Configure the "staging" instance
$ heroku config:add \
RACK_ENV=production \
S3_KEY=my-staging-key \
S3_SECRET=my-staging-secret \
S3_BUCKET=my-staging-bucket \
--app my-staging-app-name
# Configure the "production" instance
$ heroku config:add \
RACK_ENV=production \
S3_KEY=my-production-key \
S3_SECRET=my-production-secret \
S3_BUCKET=my-production-bucket \
--app my-production-app-name
請注意,您的應用程序的每個實例都有一個RACK_ENV=production
。 不要使用Rails環境來區分應用程序的實例。 相反,您的應用程序應該期望使用環境變量來配置特定於實例的方面。
設置config:在您的暫存框中添加RACK_ENV = staging,然后在您的代碼中,您可以根據環境指定存儲桶。 例如:
if Rails.env.production?
has_attached_file :photo,
:styles => ...,
:path => ...,
:storage => :s3,
:bucket => 'your_prod_bucket',
:s3_credentials => {
:access_key_id => ENV['S3_KEY'],
:secret_access_key => ENV['S3_SECRET']
}
else
has_attached_file :photo,
:styles => ...,
:path => ...,
:storage => :s3,
:bucket => 'your_staging_bucket',
:s3_credentials => {
:access_key_id => ENV['S3_KEY'],
:secret_access_key => ENV['S3_SECRET']
}
end
這個heroku帖子似乎也暗示你可以通過使用不同的s3憑據進行分段和生產來實現這一點。 我猜你也必須在亞馬遜方面做一些配置。 無論如何,看看它是否有幫助。 http://devcenter.heroku.com/articles/config-vars
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.