Carrierwave/Fog Ruby on Rails gem 不会将文件上传到 Amazon S3 [英] Carrierwave/Fog Ruby on Rails gem will not upload files to Amazon S3
问题描述
我已经浏览了 railscast 并在 StackOverflow 上进行了研究.虽然我发现其他人也有同样的问题,但我研究过的任何其他解决方案似乎都不起作用.我已经尝试了其他帖子中建议的代码的多种变体......没有运气.
I have gone through the railscast and researched here on Stack Overflow. Although I have found others with the same issue, it doesn't appear that any of the other solutions I have researched seem to work. I have tried multiple variations of the code as suggested in other posts... No luck.
我正在尝试通过 将 PDF 文件上传和存储到 Amazon S3Carrierwave 和 Fog 宝石无济于事.当我在生产中使用 Heroku 时,我还没有尝试将它部署到 Heroku,因为我甚至不能让它在本地工作.返回的错误是
I am attempting to upload and store PDF files to Amazon S3 via the Carrierwave and Fog gems to no avail. While I am using Heroku in production, I have not attempted to deploy this to Heroku yet since I cannot even get this to work locally. The error returned is
缺少必需的参数:aws_access_key_id、aws_secret_access_key
Missing required arguments: aws_access_key_id, aws_secret_access_key
我该如何解决这个问题?
How can I fix this problem?
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: ENV['MY_KEY_ID'],
aws_secret_access_key: ENV['MY_KEY']
}
config.fog_directory = ENV['MY_BUCKET']
config.fog_public = false
end
pdf_uploader.rb:
pdf_uploader.rb:
class PdfUploader < CarrierWave::Uploader::Base
storage :fog
include CarrierWave::MimeTypes
process :set_content_type
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
推荐答案
将 key_id、key AND 存储桶设置为环境变量时似乎存在问题.一旦我将所有三个更改为字符串常量,它似乎工作正常.我还部署到 Heroku 并在生产中验证了功能.
It looks like there was an issue with setting the key_id, key AND bucket as environmental variables. Once I changed all three to string constants, it seemed to work fine. I also deployed to Heroku and verified functionality in production as well.
最终的初始化文件:
CarrierWave.configure do |config|
config.storage = :fog
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: 'MY_KEY_ID_HERE',
aws_secret_access_key: 'MY_KEY_HERE'
}
config.fog_directory = 'MY_S3_BUCKET_HERE'
config.fog_public = false
end
这篇关于Carrierwave/Fog Ruby on Rails gem 不会将文件上传到 Amazon S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!