error: undefined method `presigned_post'

error: undefined method `presigned_post'

我正在编写一个 rails 应用程序,用户可以使用该应用程序上传图像。我正在使用 Heroku 进行部署,并使用 Carrierwave 和 S3 上传和存储图像。我已经按照这个 heroku guide 一步步进行了...不幸的是我仍然收到错误 "undefined method `presigned_post'",并且不知道如何解决它。似乎 S3_BUCKET 没有被识别为 aws 对象...

有没有人遇到过这个问题并解决了?这里有一些代码供参考:

图片控制器:

class PicturesController < ApplicationController
  before_action :set_s3_direct_post, only: [:new, :create]

  def index
    @pictures = Picture.all
  end

  def new
    @pictures = Picture.all
    @picture = Picture.new
  end

  def create
    @picture = Picture.new(picture_params)

    if @picture.save
      redirect_to new_picture_path, notice: "You just uploaded a picture!"
    else
      render "new"
    end
  end
                                                
  ...

  def picture_params
    params.require(:picture).permit(:attachment)
  end

private

  def set_s3_direct_post
    @s3_direct_post = S3_BUCKET.presigned_post(key: "uploads/#{SecureRandom.uuid}/${filename}", success_action_status: '201', acl: 'public-read')
  end
end

新图片浏览:

<h1>Upload a new picture</h1>
<br>
<div class="well">
  <%= form_for @picture, html: { class: 'directUpload', data: { 'form-data' => (@s3_direct_post.fields), 'url' => @s3_direct_post.url, 'host' => URI.parse(@s3_direct_post.url).host } } do |f| %>
    <%= f.file_field :attachment %>
    <%= f.submit "Upload", class: "btn btn-default" %>
  <% end %>
</div>

和config/environment.rb:

require File.expand_path('../application', __FILE__)

# Initialize the Rails application.
Rails.application.initialize!
# S3
S3_BUCKET='fotoes'
AWS_ACCESS_KEY_ID='secretxxxxxxxx'
AWS_SECRET_ACCESS_KEY='xxxxxxxsecretxxxxxx'

有什么想法吗?

@Jillian 我想您的期望是 S3_BUCKET class 应该调用应该定义的 presigned_post 方法。然而,似乎并非如此。我查看了包含您遵循的教程的 heroku 页面,并且您遵循了每条说明。我建议您就文档联系 heroku。但是,我会继续研究它

谢谢大家的帮助。最后我发现了一个不同的演练,它更简单有效。 (Heroku 有点复杂,容易出错 - 算一下。)

This 解决了一切:)

编辑:

  1. 并非所有 - 在我获得网站之前必须完成最后一步 运行。 运行 终端中的这一行:$ heroku config:add AWS_ACCESS_KEY=value 和 $ heroku config:add AWS_SECRET_KEY=value,其中值分别是每个的 S3 凭据。

  2. 这是雾、载波、rails、雾-aws gem唯一有效的组合(经过数周的研究):

gem 'rails', '4.1.0'

gem 'carrierwave', '~> 0.10.0'

gem 'fog', '1.34.0'

gem 'fog-aws', '0.7.6'

我通过更新我的 aws-sdk 版本解决了这个问题

$ bundle update aws-sdk