【问题标题】:Seeding remote images Carrierwave播种远程图像 Carrierwave
【发布时间】:2018-09-14 22:03:33
【问题描述】:

我想在 heroku 上填充我的应用程序,所以我需要为我在 Amazon S3 上上传的图像播种,但我不知道该怎么做...

我正在使用带有多个文件上传器的 Carrierwave

这是我的强大参数:

 private

    def article_params
      params.require(:article).permit(:id, :title, :description, {attachments:[]})
    end

这是我尝试为 heroku 播种的方法,我怎样才能使 url 被播种?

Article.create!( 
        title:  Faker::Music.instrument,
        description: Faker::Lorem.paragraph(3), 
        attachments: 
         [ "https://s3-eu-west-1.amazonaws.com/mysite/images_site/seeds/image_1.jpg", 
           "https://s3-eu-west-1.amazonaws.com/mysite/images_site/seeds/image_2.jpg"]
        )
end

这就是我在本地播种的方式,效果很好。

20.times do 
    Article.create!( 
        title:  Faker::Music.instrument,
        description: Faker::Lorem.paragraph(10),    
        attachments: 
         [ Rails.root.join("app/assets/images/seeds/image_1.jpg").open, 
           Rails.root.join("app/assets/images/seeds/image_2.jpg").open,
           Rails.root.join("app/assets/images/seeds/image_3.jpg").open, 
          ].shuffle)
    print "_"
end

编辑

在 Carlos Ramirez III 的建议之后,它仍然失败:(

➜  lesch git:(master) ✗ heroku run rails db:seed --trace                                                                                                          [2.4.4]
Running rails db:seed --trace on ⬢ new-lesch... up, run.1313 (Free)
** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke db:load_config (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:load_config
** Execute db:abort_if_pending_migrations
D, [2018-09-15T13:55:37.031136 #4] DEBUG -- :    (7.1ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
rails aborted!
ArgumentError: extra arguments
/app/vendor/ruby-2.4.4/lib/ruby/2.4.0/open-uri.rb:136:in `open_uri'
/app/vendor/ruby-2.4.4/lib/ruby/2.4.0/open-uri.rb:721:in `open'
/app/vendor/ruby-2.4.4/lib/ruby/2.4.0/open-uri.rb:35:in `open'
/app/db/seeds.rb:17:in `block in <main>'
/app/db/seeds.rb:12:in `times'
/app/db/seeds.rb:12:in `<main>'
/app/vendor/bundle/ruby/2.4.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
/app/vendor/bundle/ruby/2.4.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:281:in `block in load'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:253:in `load_dependency'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:281:in `load'
/app/vendor/bundle/ruby/2.4.0/gems/railties-5.2.1/lib/rails/engine.rb:551:in `load_seed'
/app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.2.1/lib/active_record/tasks/database_tasks.rb:281:in `load_seed'
/app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.2.1/lib/active_record/railties/databases.rake:194:in `block (2 levels) in <main>'
/app/vendor/bundle/ruby/2.4.0/gems/rake-12.3.1/lib/rake/task.rb:271:in `block in execute'
/app/vendor/bundle/ruby/2.4.0/gems/rake-12.3.1/lib/rake/task.rb:271:in `each'
/app/vendor/bundle/ruby/2.4.0/gems/rake-12.3.1/lib/rake/task.rb:271:in `execute'
/app/vendor/bundle/ruby/2.4.0/gems/rake-12.3.1/lib/rake/task.rb:213:in `block in invoke_with_call_chain'
/app/vendor/ruby-2.4.4/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
/app/vendor/bundle/ruby/2.4.0/gems/rake-12.3.1/lib/rake/task.rb:193:in `invoke_with_call_chain'
/app/vendor/bundle/ruby/2.4.0/gems/rake-12.3.1/lib/rake/task.rb:182:in `invoke'
/app/vendor/bundle/ruby/2.4.0/gems/rake-12.3.1/lib/rake/application.rb:160:in `invoke_task'
/app/vendor/bundle/ruby/2.4.0/gems/rake-12.3.1/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/app/vendor/bundle/ruby/2.4.0/gems/rake-12.3.1/lib/rake/application.rb:116:in `each'
/app/vendor/bundle/ruby/2.4.0/gems/rake-12.3.1/lib/rake/application.rb:116:in `block in top_level'
/app/vendor/bundle/ruby/2.4.0/gems/rake-12.3.1/lib/rake/application.rb:125:in `run_with_threads'
/app/vendor/bundle/ruby/2.4.0/gems/rake-12.3.1/lib/rake/application.rb:110:in `top_level'
/app/vendor/bundle/ruby/2.4.0/gems/railties-5.2.1/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/app/vendor/bundle/ruby/2.4.0/gems/rake-12.3.1/lib/rake/application.rb:186:in `standard_exception_handling'
/app/vendor/bundle/ruby/2.4.0/gems/railties-5.2.1/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/app/vendor/bundle/ruby/2.4.0/gems/railties-5.2.1/lib/rails/command.rb:48:in `invoke'
/app/vendor/bundle/ruby/2.4.0/gems/railties-5.2.1/lib/rails/commands.rb:18:in `<main>'
/app/vendor/bundle/ruby/2.4.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/app/vendor/bundle/ruby/2.4.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.4.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
/app/vendor/bundle/ruby/2.4.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.4.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `block in require'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:253:in `load_dependency'
/app/vendor/bundle/ruby/2.4.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `require'
/app/bin/rails:9:in `<main>'
Tasks: TOP => db:seed

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 carrierwave


    【解决方案1】:

    您应该将 I/O 对象传递给 CarrierWave 上传器属性

    在本地示例中,您在文件路径上调用.open,它读取文件并返回File 对象。

    在远程示例中,您只是传递 URI 路径,它们是字符串,而不是像 File 这样的 I/O 对象。

    读取远程文件

    要直接从 URI 路径读取远程文件,您可以使用 OpenURI 模块提供的 open 方法。

    open("https://s3-eu-west-1.amazonaws.com/mysite/images_site/seeds/image_1.jpg”)
    

    在此处阅读文档:
    https://ruby-doc.org/stdlib-2.1.0/libdoc/open-uri/rdoc/OpenURI.html

    注意:如果您不使用 Rails,则可能需要添加 require "open-uri"

    使用 CarrierWave 播种多个远程文件

    您可以通过使用open 包装远程 URI 字符串来修复 Heroku 示例,如下所示:

    Article.create!( 
        title:  Faker::Music.instrument,
        description: Faker::Lorem.paragraph(3), 
        attachments: 
         [ open("https://s3-eu-west-1.amazonaws.com/mysite/images_site/seeds/image_1.jpg”), 
           open("https://s3-eu-west-1.amazonaws.com/mysite/images_site/seeds/image_2.jpg”)]
        )
    end
    

    【讨论】:

    • 感谢您的建议,但它不起作用 :( 我尝试使用并使用 ou trequire "open uri" 我粘贴了上面的错误消息
    • 你能告诉我你正在尝试的导致你上面粘贴的错误的确切代码吗?
    • 上传的一张图片好像有问题,把所有东西都弄乱了……我刚把它删了,现在好了……谢谢你的帮助
    • 我明白了,这是有道理的。很高兴你能够让它工作!
    【解决方案2】:

    只需以更手动的方式创建数据库记录。您需要确保 S3 密钥与生成密钥结构的 CarrierWave 实例匹配。

    【讨论】:

      猜你喜欢
      • 2017-01-20
      • 1970-01-01
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 2015-11-29
      • 1970-01-01
      相关资源
      最近更新 更多