在Rails 4.0.2中,我使用s3_direct_upload
和aws-sdk
gems直接为s3存储桶上传文件。在开发环境中,它工作正常,但在生产环境中,它会抛出如下错误,
ActionView::Template::Error (no implicit conversion of nil into String)
在观看中,
<%= s3_uploader_form :callback_url=>create_cv_url, :id=> "s3_uploader", :key=> "cv_uploads/{unique_id}/${filename}",
:key_starts_with=> "cv_uploads/", :callback_param=> "cv[direct_upload_url]", :max_file_size=> 1.megabytes,
:expiration=> 24.hours.from_now.utc.iso8601 do %>
<%= file_field_tag :file, multiple: true, :max_file_size => 1.megabytes, accept: 'application/pdf application/msword application/rtf application/doc application/docx' %>
<% end %>
<script id="template-upload" type="text/x-tmpl">
<div id="upload_{%=o.unique_id%}" class="upload">
<h5 class="mt1">Please Wait. <span style="color: #5f6fa0;"> {%=o.name%} </span>is processing...</h5>
<div class="progress"><div class="progress-bar progress-bar-striped active" style="width: 0%;"></div></div>
</div>
此处,问题主要指向s3_uploader_form
行(在视图中)。
此功能完全可以从http://blog.littleblimp.com/post/53942611764/direct-uploads-to-s3-with-rails-paperclip-and
引用在paperclip.rb
Paperclip::Attachment.default_options.merge!(
url: :s3_domain_url,
path: ':class/:attachment/:id/:style/:filename',
storage: :s3,
s3_credentials: Rails.configuration.aws,
s3_permissions: :private,
s3_protocol: 'http'
)
require 'paperclip/media_type_spoof_detector'
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end
在aws.rb
require 'aws-sdk'
# Rails.configuration.aws is used by AWS, Paperclip, and S3DirectUpload
Rails.configuration.aws = YAML.load(ERB.new(File.read("# {Rails.root}/config/aws.yml")).result)[Rails.env].symbolize_keys!
AWS.config(:logger=> Rails.logger)
AWS.config(Rails.configuration.aws)
在s3_direct_upload.rb
中S3DirectUpload.config do |c|
c.access_key_id = Rails.configuration.aws[:access_key_id]
c.secret_access_key = Rails.configuration.aws[:secret_access_key]
c.bucket = Rails.configuration.aws[:bucket]
c.region = "s3"
end
是否因为生产环境中的配置问题?请帮我解决这个问题。
答案 0 :(得分:6)
我有同样的问题,我解决了添加文件config.yml和我的S3凭据:
RailsApp / config.yml
# Fill in your AWS Access Key ID and Secret Access Key
# http://aws.amazon.com/security-credentials
access_key_id: xxxxxx
secret_access_key: xxxxxxx
更多信息:https://docs.aws.amazon.com/AWSSdkDocsRuby/latest/DeveloperGuide/ruby-dg-setup.html
答案 1 :(得分:2)
代码似乎没问题。正如skozz所述,其中一个问题可能是您的配置密钥可能未正确分配。请检查&#34; /config/aws.yml"。
中的aws生产密钥答案 2 :(得分:0)
通过遵循link,我也遭受了同样的痛苦。问题是我已经添加了初始化程序,但我需要重新启动rails(它正在运行但没有刷新)。
答案 3 :(得分:0)
执行此命令对我有用。 figaro heroku:set -e production