播种远程图像 Carrierwave

Seeding remote images Carrierwave

我想在 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

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

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

在本地示例中,您在读取文件的文件路径上调用 .open 和 returns 一个 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