使用在Windows Azure VM上运行的Paperclip的Rails应用程序

时间:2022-05-03 09:00:18

I have a Ruby (1.9.3) on Rails (3.2.9) app running on a Windows Azure Virtual Machine (using a Ruby-Stack-1-9-3-6-Ubuntu-12-10 image). I can successfully deploy to the VM using Capistrano but have reached a wall when it comes to file uploads and Azure Storage.

我有一个在Windows Azure虚拟机上运行的Rails(3.2.9)应用程序上的Ruby(1.9.3)(使用Ruby-Stack-1-9-3-6-Ubuntu-12-10映像)。我可以使用Capistrano成功部署到VM,但在文件上传和Azure存储方面已经到了墙。

I'm attempting to implement the paperclip-azure-storage gem, which uses the waz-storage gem, but can't seem to get past this error:

我正在尝试实现paperclip-azure-storage gem,它使用waz-storage gem,但似乎无法通过这个错误:

undefined method 'new' for nil:NilClass

nil的未定义方法'new':NilClass

I get this error whenever I try to create/update/destroy a Video object (using simple Rails forms). To clarify, I get this error on both my local machine as well as the VM instance.

每当我尝试创建/更新/销毁Video对象(使用简单的Rails表单)时,我都会收到此错误。为了澄清,我在本地计算机和VM实例上都出现此错误。

I know the error is with Paperclip because as soon as I comment out the following code in my Video model everything works:

我知道错误是Paperclip,因为只要我在我的视频模型中注释掉以下代码,一切正常:

has_attached_file :pic, :storage => :azure1

has_attached_file:pic,:storage =>:azure1

Following instructions from both gem sources (links above), I've added the following files:

按照两个gem源(上面的链接)的说明,我添加了以下文件:

  • azure.yml (contains my Azure storage account name and access key)
  • azure.yml(包含我的Azure存储帐户名和访问密钥)
  • storage.rb (copied from paperclip-azure-storage gem and added to /lib/paperclip)
  • storage.rb(从paperclip-azure-storage gem复制并添加到/ lib / paperclip)
  • paperclip.rb (also copied from the paperclip-azure-storage gem and added to the list of initializers)
  • paperclip.rb(也从paperclip-azure-storage gem复制并添加到初始化程序列表中)

To cover the basics:

涵盖基础知识:

  • Gemfile: gem 'paperclip' just updated to version 3.4.1
  • Gemfile:gem'paperclip'刚刚更新到3.4.1版
  • In the form: <%= form_for @video, html: { multipart: true } do |video_form| %> tried with and without the url option.
  • 格式为:<%= form_for @video,html:{multipart:true} do | video_form | %>尝试使用和不使用url选项。
  • In each of the environment files: Paperclip.options[:command_path] = "/opt/local/bin/convert"
  • 在每个环境文件中:Paperclip.options [:command_path] =“/ opt / local / bin / convert”
  • Schema: verified to make sure the pic attachment was added properly to the Videos table
  • 架构:经过验证,确保将pic附件正确添加到“视频”表中

I've researched tirelessly for the answer but I can't seem to find the root of the problem. Is it ImageMagick not properly installed? Is it incorrect placement and/or configuration of storage.rb and paperclip.rb files? Or is it something else altogether?

我已经不知疲倦地研究了答案,但我似乎无法找到问题的根源。是不是ImageMagick没有正确安装?是不正确的storage.rb和paperclip.rb文件的放置和/或配置?还是完全不同的东西?

Here's the code in my paperclip.rb initializer:

这是我的paperclip.rb初始化程序中的代码:

module Paperclip
  class Attachment
    def self.default_options
      @default_options ||= {
        :styles            => {},
        :processors        => [:thumbnail],
        :convert_options   => {},
        :default_url       => "/rails.png",
        :default_style     => :original,
        :whiny             => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails],
        :storage           => :azure1,
        :path              => ":modelname/:attachment/:id/:style/:filename",        
        :azure_credentials => "#{Rails.root}/config/azure.yml",
        :azure_container   => "system",
        :azure_host_alias  => "name_of_azure_storage.blob.core.windows.net",
        :url               => ':azure_domain_url',
      }
    end    
  end
end

I'll gladly post additional code if required.

如果需要,我很乐意发布其他代码。

Has anyone tried/succeeded implementing the aforementioned gems with Windows Azure? I would love any guidance/suggestions.

有没有人尝试/成功使用Windows Azure实现上述宝石?我很乐意任何指导/建议。

1 个解决方案

#1


0  

The problem seems to be that when you use the format:

问题似乎是当你使用格式时:

module Paperclip
  class attachment
    ...
  end
end

The internal default options for Paperclip don't get created. It then tries to instantiate the URL generator, which is normally defined in those internal default options, and this results in the error you are seeing.

无法创建Paperclip的内部默认选项。然后它尝试实例化URL生成器,该生成器通常在那些内部默认选项中定义,这会导致您看到的错误。

To get past this, I formatted my initializer as follows:

为了解决这个问题,我将初始化程序格式化如下:

Paperclip::Attachment.default_options[:processors] = [:thumbnail]
Paperclip::Attachment.default_options[:convert_options] = {}
Paperclip::Attachment.default_options[:styles] = {}
Paperclip::Attachment.default_options[:default_url] = "/rails.png"
Paperclip::Attachment.default_options[:default_style] = :original
Paperclip::Attachment.default_options[:whiny] = Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails]
Paperclip::Attachment.default_options[:storage] = :azure1
Paperclip::Attachment.default_options[:path] = ":modelname/:attachment/:id/:style/:filename"
Paperclip::Attachment.default_options[:azure_credentials] = "#{Rails.root}/config/azure.yml"
Paperclip::Attachment.default_options[:azure_container] = "system"
Paperclip::Attachment.default_options[:azure_host_alias] = "storageaccount.blob.core.windows.net"
Paperclip::Attachment.default_options[:url] = ':azure_domain_url'

This allowed the default options to successfully be created/merged with the options in my initializer, and got me past that error.

这允许使用我的初始化程序中的选项成功创建/合并默认选项,并让我超过该错误。

Since I'm on a recent version of rails, I ran into errors with RAILS_ROOT and RAILS_ENV next, so I adjusted the above initializer bits to use Rails.root and changed the paperclip-azure-storage to use Rails.env instead of RAILS_ENV.

由于我使用的是最近版本的rails,我接下来遇到了RAILS_ROOT和RAILS_ENV的错误,因此我调整了上面的初始化位以使用Rails.root并更改了paperclip-azure-storage以使用Rails.env而不是RAILS_ENV。

Hope this helps.

希望这可以帮助。

#1


0  

The problem seems to be that when you use the format:

问题似乎是当你使用格式时:

module Paperclip
  class attachment
    ...
  end
end

The internal default options for Paperclip don't get created. It then tries to instantiate the URL generator, which is normally defined in those internal default options, and this results in the error you are seeing.

无法创建Paperclip的内部默认选项。然后它尝试实例化URL生成器,该生成器通常在那些内部默认选项中定义,这会导致您看到的错误。

To get past this, I formatted my initializer as follows:

为了解决这个问题,我将初始化程序格式化如下:

Paperclip::Attachment.default_options[:processors] = [:thumbnail]
Paperclip::Attachment.default_options[:convert_options] = {}
Paperclip::Attachment.default_options[:styles] = {}
Paperclip::Attachment.default_options[:default_url] = "/rails.png"
Paperclip::Attachment.default_options[:default_style] = :original
Paperclip::Attachment.default_options[:whiny] = Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails]
Paperclip::Attachment.default_options[:storage] = :azure1
Paperclip::Attachment.default_options[:path] = ":modelname/:attachment/:id/:style/:filename"
Paperclip::Attachment.default_options[:azure_credentials] = "#{Rails.root}/config/azure.yml"
Paperclip::Attachment.default_options[:azure_container] = "system"
Paperclip::Attachment.default_options[:azure_host_alias] = "storageaccount.blob.core.windows.net"
Paperclip::Attachment.default_options[:url] = ':azure_domain_url'

This allowed the default options to successfully be created/merged with the options in my initializer, and got me past that error.

这允许使用我的初始化程序中的选项成功创建/合并默认选项,并让我超过该错误。

Since I'm on a recent version of rails, I ran into errors with RAILS_ROOT and RAILS_ENV next, so I adjusted the above initializer bits to use Rails.root and changed the paperclip-azure-storage to use Rails.env instead of RAILS_ENV.

由于我使用的是最近版本的rails,我接下来遇到了RAILS_ROOT和RAILS_ENV的错误,因此我调整了上面的初始化位以使用Rails.root并更改了paperclip-azure-storage以使用Rails.env而不是RAILS_ENV。

Hope this helps.

希望这可以帮助。