播种远程图像Carrierwave

问题描述 投票:0回答:2

我想在heroku上填充我的应用程序,所以我需要播放我在Amazon S3上传的图像,我无法弄清楚如何做...

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

以下是我的强参数:

 private

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

以下是我尝试为heroku播种的方法,如何将网址设为种子?

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

EDIT

在卡洛斯拉米雷斯三世的建议之后,它仍然失败:(

➜  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
2个回答
2
投票

You should pass I/O objects to CarrierWave uploader attributes

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

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

Reading a remote file

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

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"

Seeding multiple remote files using 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

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.