save_and_process 后处理 403 禁止 Carrierwave_direct S3 雾
我正在尝试为我的应用程序开发直接文件上传到 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
然后我有一个上传器: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
和一个头像控制器:
class AvatarsController < ApplicationController
def new
@uploader = Clip.new.avatar
@uploader.success_action_redirect = 'http://localhost:3000/clips'
end
end
最后是我的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
当我上传文件时,如果我只保存我的“剪辑”,一切没问题。但是,如果我使用 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)
我已经坚持了两天了,所以,任何帮助将不胜感激!谢谢!!!尼古拉斯.
I'm trying to develop direct fileuploads to S3 for my app. I'm following the github tutorial for this and everything is more or less ok but get an error message when trying to make the post processing.
I did the following:
I have an activerecord model called clip.rb:
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
Then I have an uploader: 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
and an avatar controller:
class AvatarsController < ApplicationController
def new
@uploader = Clip.new.avatar
@uploader.success_action_redirect = 'http://localhost:3000/clips'
end
end
and finally my 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
When I upload a file, if I just save my "clip", everything is ok. If I use the save_and_process method however, an error arises at line:
self.remote_avatar_url = avatar.direct_fog_url(:with_path => true)
This is the error message:
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)
I've been hanging on this for two days so, any help would be greatly appreciated!!! Thanks!!! Nicolas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我敢打赌,提供给
self.remote_avatar_url
的 URL 不正确。我遇到了同样的问题,CWDirect gem 提供的代码对我不起作用,并给了我一个不正确的 URL,因此 CarrierWave 无法下载和处理图像。整个 403 Forbidden 错误消息是来自 Amazon 的垃圾消息——这会让人们相信权限有问题;就我而言,权限绝对没有任何问题。只是那里没有图像。这是让我工作的代码,请注意我已经更改了 URL 的形成方式:请注意,我安装的字段的名称是
image
而不是avatar
。我是如何到达这一点并修复它的——尝试一下,使用 Rails 调试器(只需取消注释上面的调试器行)在 self.remote_image_url 行之前冻结程序,然后在调试时模式输入
irb
来启动控制台。然后您可以打印出来并查看 'image.direct_fog_url(:with_path => true)' 给您带来的真正价值。您可以将其复制并粘贴到浏览器中。如果错误(可能是),那么您将收到愚蠢的权限错误(即使这不是权限问题),但是当正确时,您将看到上传的图像或将下载图像。最好打开 Amazon S3 控制台并查看您的开发存储桶,以便您可以找到刚刚上传的图像。在控制台中找到该图像并转到其属性,您可以看到您应该使用的网址/url。
希望这有帮助。由于误导性错误,这对我来说很难追踪,我花了很多时间尝试纠正 S3 存储桶上的权限,但这不是问题,只是 CWDirect github 页面中的代码对我不起作用(宝石版??)。
My bet is that the URL supplied to
self.remote_avatar_url
is incorrect. I had this same problem and the code that CWDirect gem provides didn't work for me and gave me an incorrect URL, thus CarrierWave couldn't download and process the image. The whole 403 Forbidden error message was a crap message from Amazon--this leads one to believe that there is something wrong with Permissions; in my case there was absolutely nothing wrong with permissions. It was just that there was no image there. Here is the code that got this working for me, notice I've changed how the URL is formed:Note that the name of my mounted field is
image
and notavatar
.How I got to this point and fixed it--try this out, use the rails debugger (just uncomment the debugger line above) to freeze the program just before the
self.remote_image_url
line, then while in debug mode typeirb
to start up the console. Then you can print out and see really what value 'image.direct_fog_url(:with_path => true)' is giving you. You can copy and paste this into a browser. If it's wrong (probably is) then you will get the silly Permissions error (even though it's not a permissions problem), but when it's correct either you will see the uploaded image or the image will download.It's a good idea to have your Amazon S3 console open and viewing your dev bucket so you can find the image that just uploaded. Find the image in the console and go to its properties and you can see the web address/url that you are supposed to be using.
Hope this helps. Because of the misleading error this was hard to track down for me, I spent a bunch of time trying to correct permissions on my S3 bucket but this wasn't the problem, just that code from the CWDirect github page doesn't work for me (gem version??).