ruby-on-rails - Rails – 如何将文件从本地主机导入到 heroku db

标签 ruby-on-rails heroku amazon-s3 carrierwave thor

我有 2 个模型——文档和附件。我正在尝试将大量文件导入到我的 heroku 数据库中。我也有雷神任务。当我用我的本地数据库运行这个任务时它工作正常,但是当我尝试在 stage env 中运行它时,我在“document.save”操作中出错:

$ RAILS_ENV=stage thor import:documents '/path/to/files/'

/../gems/fog-1.10.0/lib/fog/core/hmac.rb:23:in `digest': can't convert Fixnum into String (TypeError)

此外,当我通过 heroku 应用程序上的表单上传文件时,一切正常。那有什么问题呢?

这里是模型:

附件:

class Attachment < ActiveRecord::Base
  IMAGE_TYPES = ['jpg', 'jpeg', 'gif', 'png']
  DOC_TYPES = ['pdf', 'doc', 'docx', 'rtf', 'pages', 'txt']

  belongs_to :attachable, polymorphic: true

  attr_accessible :attachment_file, :attachment_file_cache, :attachment_type

  mount_uploader :attachment_file, AttachmentUploader

  validates :attachment_file, presence: true

  ...

 end

文档:

class Document < ActiveRecord::Base
  attr_accessible :description, :title, :visible, :attachment_attributes
  has_one :attachment, as: :attachable, dependent: :destroy, class_name: 'Attachment', conditions: { attachment_type: 'document' }
  ...
end

雷神任务:

class Import < Gearup::TasksBase
  desc "documents <DOCUMENTS_FOLDER>", 'Upload documents'
  def documents(dir_path)
    dir_path += "#{dir_path.last == '/' ? '*' : '/*'}"
    print_color_message('! Directory is empty !', 'red') if Dir[dir_path].empty?
    Dir[dir_path].each do |file_path|
      document = Document.new(title: 'document_title')
      document.build_attachment(attachment_type: 'document', attachment_file: File.open(file_path))

      if document.save  ### HERE IS A PROBLEM ###
        print_color_message("-= document \"#{document.title}\" successfully created =-", 'green')
      else
        print_color_message("! document not saved !", 'red')
        print_color_message(document.errors.messages.inspect, 'red')
      end
    end
  end
end

而且我没有忘记使用

编辑我的 database.yml
heroku pg:credentials HEROKU_POSTGRESQL_ORANGE_URL

数据库.yml

...

stage:
  adapter: postgresql
  encoding: unicode
  database: <database_name>
  host: <host_name>
  sslmode: <sslmode>
  port: <port>
  pool: <pool>
  username: <username>
  password: <password>

谢谢。

最佳答案

可能您忘记在配置中设置 s3 凭据。 Heroku 从 ENV 中获取它。

关于ruby-on-rails - Rails – 如何将文件从本地主机导入到 heroku db,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18826642/

相关文章:

git - Heroku:如何将不同的本地 Git 分支推送到 Heroku/master

javascript - 使用 AdonisJS 5 的文件存储 (S3)

html - CSS响应高度失败

ruby-on-rails - 如何将连续的相同元素插入子数组?

ruby-on-rails - rails/ ruby : TimeWithZone comparison inexplicably failing for equivalent values

node.js - (貌似)遵循 Node.js 的 Heroku 说明后出现错误

node.js - Heroku Node js错误: 'Internal Server Error'

amazon-web-services - Athena 在太多 S3 文件上的性能

python - 一段时间后,将 Django 的 collectstatic 与 boto S3 一起使用会抛出 "Error 32: Broken Pipe"

ruby-on-rails - 创建成为可执行 chmod 777 gihook 文件的文件