save_and_process后处理403 Forbidden Carrierwave_direct S3 Fog

nde*_*eau 5 ruby-on-rails carrierwave fog

我正在尝试为我的应用程序开发直接文件上传到S3.我正在关注github教程,所有内容都或多或少都可以,但在尝试进行后期处理时会收到错误消息.

我做了以下事情:

我有一个名为clip.rb的activerecord模型:

class Clip < ActiveRecord::Base
  belongs_to :attachable, :polymorphic => true
  mount_uploader :avatar, AvatarUploader

  attr_accessible :id, :avatar, :name, :clipat_file_name, :attachable_id, :attachable_type, :clipat, :project_id, :user_id, :path, :parent_id, 

  def save_and_process_avatar(options = {})
    if options[:now] or 1==1
      self.remote_avatar_url = avatar.direct_fog_url(:with_path => true)
      save
    else
      Resque.enqueue(AvatarProcessor, attributes)
    end
  end
Run Code Online (Sandbox Code Playgroud)

然后我有一个上传者:avatar_uploader.rb

class AvatarUploader < CarrierWave::Uploader::Base
   include CarrierWave::RMagick
   include CarrierWaveDirect::Uploader
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}"  #I removed /#{model.id} from the template because it creates an empty directory on the server. But If I put it back, the same problem remains
  end
  version :thumb do
     process :resize_to_limit => [50, 50]
   end
end
Run Code Online (Sandbox Code Playgroud)

和一个头像控制器:

class AvatarsController < ApplicationController
  def new
    @uploader = Clip.new.avatar
    @uploader.success_action_redirect = 'http://localhost:3000/clips'
  end
end
Run Code Online (Sandbox Code Playgroud)

最后我的clip_controller:

class ClipsController < ApplicationController
  def index
    if params[:key]
      key=params[:key].split("/")
      clip = Clip.new
      clip.attachable_id = key[3]
      clip.attachable_type = "Pmdocument"
      clip.key = params[:key]
#      clip.save
      clip.save_and_process_avatar
    end
    @clips = Clip.where("avatar is not null")

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @clips.collect { |p| p.to_jq_upload }.to_json }
    end
  end
Run Code Online (Sandbox Code Playgroud)

当我上传文件时,如果我只保存我的"剪辑",一切都还可以.但是,如果我使用save_and_process方法,则会出现错误:self.remote_avatar_url = avatar.direct_fog_url(:with_path => true)

这是错误消息:

OpenURI::HTTPError (403 Forbidden):
  app/models/clip.rb:38:in `save_and_process_avatar'
  app/controllers/clips_controller.rb:22:in `index'

Rendered /Users/nico/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.4ms)
Rendered /Users/nico/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.2ms)
Rendered /Users/nico/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (5.2ms)
Run Code Online (Sandbox Code Playgroud)

我已经挂了两天所以,任何帮助将不胜感激!!! 谢谢!!!尼古拉斯.

Fir*_*gon 2

我敢打赌,提供的 URLself.remote_avatar_url不正确。我遇到了同样的问题,CWDirect gem 提供的代码对我不起作用,并给了我一个不正确的 URL,因此 CarrierWave 无法下载和处理图像。整个 403 Forbidden 错误消息是来自 Amazon 的垃圾消息——这会让人们相信权限有问题;就我而言,权限绝对没有任何问题。只是那里没有图像。这是让我工作的代码,请注意我已经更改了 URL 的形成方式:

def save_and_process_image(options = {})
if options[:now]
  # debugger
  self.remote_image_url = image.direct_fog_url+self.key # OLD CODE THAT AINT WORKIN! --> image.direct_fog_url(:with_path => true)
  save
else
  # Resque.enqueue(AvatarProcessor, attributes)
  # TODO: Implement background processing
end
end
Run Code Online (Sandbox Code Playgroud)

请注意,我安装的字段的名称是image而不是avatar

我是如何到达这一点并修复它的——尝试一下,使用rails调试器(只需取消注释上面的调试器行)来冻结该行之前的程序self.remote_image_url,然后在调试模式下键入irb启动控制台。然后您可以打印出来并查看 'image.direct_fog_url(:with_path => true)' 给您带来的真正价值。您可以将其复制并粘贴到浏览器中。如果错误(可能是),那么您将收到愚蠢的权限错误(即使这不是权限问题),但是当正确时,您将看到上传的图像或将下载图像。

最好打开您的 Amazon S3 控制台并查看您的开发存储桶,以便您可以找到刚刚上传的图像。在控制台中找到该图像并转到其属性,您可以看到您应该使用的网址/url 。

希望这可以帮助。由于误导性错误,这对我来说很难追踪,我花了很多时间尝试纠正 S3 存储桶上的权限,但这不是问题,只是 CWDirect github 页面中的代码对我不起作用(宝石版??)。