Migrate_to_S3 Fails on Rebake

With the help of @itsbhanusharma, I’ve been working to move my old uploads over to S3.

I followed the steps in this guide:

After a rebuild, the uploads sitll had not moved to S3, which is odd. Discourse-doctor reported a DNS problem:

> 
> ========================================
> Discourse version at : NOT FOUND
> Discourse version at localhost: Discourse 2.5.0.beta4
> **> ==================== DNS PROBLEM ====================**
> **> This server reports Discourse 2.5.0.beta4 , but reports NOT FOUND.**
> **> This suggests that you have a DNS problem or that an intermediate proxy is to blame.**
> **> If you are using Cloudflare, or a CDN, it may be improperly configured.**

I ran a manual migrate to S3 and got the following:

 > root@discourse-app:/var/www/discourse# rake --trace uploads:migrate_to_s3
> ** Invoke uploads:migrate_to_s3 (first_time)
> ** Invoke environment (first_time)
> ** Execute environment
> ** Execute uploads:migrate_to_s3
> Migrating uploads to S3 for 'default'...
> Uploading files to S3...
>  - Listing local files
> .. => 2980 files
>  - Listing S3 files
> .... => 3156 files
>  - Syncing files to S3
> ....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
> Updating the URLs in the database...
> Warning: no type cast defined for type "name" with oid 19. Please cast this type explicitly to TEXT to be safe for future changes.
> Removing old optimized images...
> Flagging all posts containing lightboxes for rebake...
> 781 posts were flagged for a rebake
> rake aborted!
> FileStore::ToS3MigrationError: 68 posts are not remapped to new S3 upload URL. S3 migration failed for db 'default'.
> /var/www/discourse/lib/file_store/to_s3_migration.rb:131:in `raise_or_log'
> /var/www/discourse/lib/file_store/to_s3_migration.rb:86:in `migration_successful?'
> /var/www/discourse/lib/file_store/to_s3_migration.rb:351:in `migrate_to_s3'
> /var/www/discourse/lib/file_store/to_s3_migration.rb:65:in `migrate'
> /var/www/discourse/lib/tasks/uploads.rake:239:in `migrate_to_s3'
> /var/www/discourse/lib/tasks/uploads.rake:218:in `block in migrate_to_s3_all_sites'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rails_multisite-2.1.2/lib/rails_multisite/connection_management.rb:64:in `with_connection'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rails_multisite-2.1.2/lib/rails_multisite/connection_management.rb:74:in `each_connection'
> /var/www/discourse/lib/tasks/uploads.rake:216:in `migrate_to_s3_all_sites'
> /var/www/discourse/lib/tasks/uploads.rake:212:in `block in <top (required)>'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:281:in `block in execute'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:281:in `each'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:281:in `execute'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
> /usr/local/lib/ruby/2.6.0/monitor.rb:235:in `mon_synchronize'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:199:in `invoke_with_call_chain'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/task.rb:188:in `invoke'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:160:in `invoke_task'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:116:in `block (2 levels) in top_level'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:116:in `each'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:116:in `block in top_level'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:125:in `run_with_threads'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:110:in `top_level'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:83:in `block in run'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:186:in `standard_exception_handling'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-13.0.1/lib/rake/application.rb:80:in `run'
> bin/rake:13:in `<top (required)>'
> /usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:63:in `load'
> /usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:63:in `kernel_load'
> /usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:28:in `run'
> /usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli.rb:476:in `exec'
> /usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
> /usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
> /usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor.rb:399:in `dispatch'
> /usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli.rb:30:in `dispatch'
> /usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/base.rb:476:in `start'
> /usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/cli.rb:24:in `start'
> /usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/exe/bundle:46:in `block in <top (required)>'
> /usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/lib/bundler/friendly_errors.rb:123:in `with_friendly_errors'
> /usr/local/lib/ruby/gems/2.6.0/gems/bundler-2.1.4/exe/bundle:34:in `<top (required)>'
> /usr/local/bin/bundle:23:in `load'
> /usr/local/bin/bundle:23:in `<main>'
> Tasks: TOP => uploads:migrate_to_s3
> root@discourse-app:/var/www/discourse#

Any thoughts on this @gerhard?

1 Like

It fails because the following check fails:

https://github.com/discourse/discourse/blob/master/lib/file_store/to_s3_migration.rb#L82-L88

I suggest you find out why those paths aren’t remapped correctly. Try running the following in the rails console to look at cooked of those posts. Somewhere in there might be a clue…

current_db = RailsMultisite::ConnectionManagement.current_db
cdn_path = SiteSetting.cdn_path("/uploads/#{current_db}/original").sub(/https?:/, "")
Post.where("cooked LIKE '%#{cdn_path}%'").pluck(:cooked)
4 Likes

Here’s the output from the rails console: (this is only part of it and it is ugly)

root@discourse-app:/var/www/discourse# rails console
[1] pry(main)> current_db = RailsMultisite::ConnectionManagement.current_db
=> "default"
[2] pry(main)> cdn_path = SiteSetting.cdn_path("/uploads/#{current_db}/original").sub(/https?:/, "")
=> "/uploads/default/original"
[3] pry(main)> Post.where("cooked LIKE '%#{cdn_path}%'").pluck(:cooked)
=> ["<p>U.C. Tricolor frag $30<br>\nRed Stylocoeniella $30<br>\nBranching Cyphastrea $40<br>\nOrenji Mont frag $30</p>\n<hr>\n<p><div class=\"lightbox-wrapper\"><a class=\"lightbox\" href=\"//brcuploads.s3.dualstack.us-east-1.amazonaws.com/original/2X/2/282821b1e16dce48ac11f1d607412e1bfed61534.jpeg\" data-download-href=\"/uploads/short-url/5Jf4Z7WaQqYix5IDWN12IlLoYfi.jpeg?dl=1\" title=\"DSCN0069-005.JPG\"><img src=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/2/282821b1e16dce48ac11f1d607412e1bfed61534_2_524x500.jpeg\" alt=\"DSCN0069-005.JPG\" data-base62-sha1=\"5Jf4Z7WaQqYix5IDWN12IlLoYfi\" width=\"524\" height=\"500\" srcset=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/2/282821b1e16dce48ac11f1d607412e1bfed61534_2_524x500.jpeg, https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/2/282821b1e16dce48ac11f1d607412e1bfed61534_2_786x750.jpeg 1.5x, /uploads/default/original/2X/2/282821b1e16dce48ac11f1d607412e1bfed61534.jpeg 2x\" data-small-upload=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/2/282821b1e16dce48ac11f1d607412e1bfed61534_2_10x10.png\"><div class=\"meta\"><svg class=\"fa d-icon d-icon-far-image svg-icon\" aria-hidden=\"true\"><use xlink:href=\"#far-image\"></use></svg><span class=\"filename\">DSCN0069-005.JPG</span><span class=\"informations\">800×762 181 KB</span><svg class=\"fa d-icon d-icon-discourse-expand svg-icon\" aria-hidden=\"true\"><use xlink:href=\"#discourse-expand\"></use></svg></div></a></div></p>\n<p><div class=\"lightbox-wrapper\"><a class=\"lightbox\" href=\"//brcuploads.s3.dualstack.us-east-1.amazonaws.com/original/2X/9/960e0f184917ad7e0bbf2d6c31cf546fa9e124f5.jpeg\" data-download-href=\"/uploads/short-url/lprJZk3t4LfijslovcQ9kgaqzcN.jpeg?dl=1\" title=\"DSCN0119-003.JPG\"><img src=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/9/960e0f184917ad7e0bbf2d6c31cf546fa9e124f5_2_513x500.jpeg\" alt=\"DSCN0119-003.JPG\" data-base62-sha1=\"lprJZk3t4LfijslovcQ9kgaqzcN\" width=\"513\" height=\"500\" srcset=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/9/960e0f184917ad7e0bbf2d6c31cf546fa9e124f5_2_513x500.jpeg, https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/9/960e0f184917ad7e0bbf2d6c31cf546fa9e124f5_2_769x750.jpeg 1.5x, /uploads/default/original/2X/9/960e0f184917ad7e0bbf2d6c31cf546fa9e124f5.jpeg 2x\" data-small-upload=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/9/960e0f184917ad7e0bbf2d6c31cf546fa9e124f5_2_10x10.png\"><div class=\"meta\"><svg class=\"fa d-icon d-icon-far-image svg-icon\" aria-hidden=\"true\"><use xlink:href=\"#far-image\"></use></svg><span class=\"filename\">DSCN0119-003.JPG</span><span class=\"informations\">800×779 186 KB</span><svg class=\"fa d-icon d-icon-discourse-expand svg-icon\" aria-hidden=\"true\"><use xlink:href=\"#discourse-expand\"></use></svg></div></a></div></p>\n<p><div class=\"lightbox-wrapper\"><a class=\"lightbox\" href=\"//brcuploads.s3.dualstack.us-east-1.amazonaws.com/original/2X/5/548f0324f3b0e8dd482729c1d1f5c450b5556919.jpeg\" data-download-href=\"/uploads/short-url/c42vC8e8IRT52htW2RX7YedLpaN.jpeg?dl=1\" title=\"DSCN0072-004.JPG\"><img src=\"//brcuploads.s3.dualstack.us-east-1.amazonaws.com/original/2X/5/548f0324f3b0e8dd482729c1d1f5c450b5556919.jpeg\" alt=\"DSCN0072-004.JPG\" data-base62-sha1=\"c42vC8e8IRT52htW2RX7YedLpaN\" width=\"468\" height=\"500\" data-small-upload=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/5/548f0324f3b0e8dd482729c1d1f5c450b5556919_2_10x10.png\"><div class=\"meta\"><svg class=\"fa d-icon d-icon-far-image svg-icon\" aria-hidden=\"true\"><use xlink:href=\"#far-image\"></use></svg><span class=\"filename\">DSCN0072-004.JPG</span><span class=\"informations\">750×800 180 KB</span><svg class=\"fa d-icon d-icon-discourse-expand svg-icon\" aria-hidden=\"true\"><use xlink:href=\"#discourse-expand\"></use></svg></div></a></div></p>\n<p><div class=\"lightbox-wrapper\"><a class=\"lightbox\" href=\"//brcuploads.s3.dualstack.us-east-1.amazonaws.com/original/2X/d/dc75e2b57072ff260f99cfa9a7cc1e4f39c207f8.jpeg\" data-download-href=\"/uploads/short-url/vshBXgkYrsRhRqNFakI1jgIE5Sg.jpeg?dl=1\" title=\"DSCN0060-005.JPG\"><img src=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/d/dc75e2b57072ff260f99cfa9a7cc1e4f39c207f8_2_396x500.jpeg\" alt=\"DSCN0060-005.JPG\" data-base62-sha1=\"vshBXgkYrsRhRqNFakI1jgIE5Sg\" width=\"396\" height=\"500\" srcset=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/d/dc75e2b57072ff260f99cfa9a7cc1e4f39c207f8_2_396x500.jpeg, https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/d/dc75e2b57072ff260f99cfa9a7cc1e4f39c207f8_2_594x750.jpeg 1.5x, /uploads/default/original/2X/d/dc75e2b57072ff260f99cfa9a7cc1e4f39c207f8.jpeg 2x\" data-small-upload=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/d/dc75e2b57072ff260f99cfa9a7cc1e4f39c207f8_2_10x10.png\"><div class=\"meta\"><svg class=\"fa d-icon d-icon-far-image svg-icon\" aria-hidden=\"true\"><use xlink:href=\"#far-image\"></use></svg><span class=\"filename\">DSCN0060-005.JPG</span><span class=\"informations\">635×800 196 KB</span><svg class=\"fa d-icon d-icon-discourse-expand svg-icon\" aria-hidden=\"true\"><use xlink:href=\"#discourse-expand\"></use></svg></div></a></div></p>",
 "<p>U.C. Raspberry delight frag $39 “radion”<br>\nORA Kelly green psammacora $39 \"radion<br>\nU.C. Red Dragon Frag $30<br>\nU.C. Red dragon Smaller frag $20</p>\n<hr>\n<p><div class=\"lightbox-wrapper\"><a class=\"lightbox\" href=\"//brcuploads.s3.dualstack.us-east-1.amazonaws.com/original/2X/d/deb035a3639a3a3bb8b04ebd9da2b97fb5d8652d.jpeg\" data-download-href=\"/uploads/short-url/vLZwAqgUqcslrwos7Q27gheEVSR.jpeg?dl=1\" title=\"DSCN9908-003.JPG\"><img src=\"//brcuploads.s3.dualstack.us-east-1.amazonaws.com/original/2X/d/deb035a3639a3a3bb8b04ebd9da2b97fb5d8652d.jpeg\" alt=\"DSCN9908-003.JPG\" data-base62-sha1=\"vLZwAqgUqcslrwos7Q27gheEVSR\" width=\"561\" height=\"500\" data-small-upload=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/d/deb035a3639a3a3bb8b04ebd9da2b97fb5d8652d_2_10x10.png\"><div class=\"meta\"><svg class=\"fa d-icon d-icon-far-image svg-icon\" aria-hidden=\"true\"><use xlink:href=\"#far-image\"></use></svg><span class=\"filename\">DSCN9908-003.JPG</span><span class=\"informations\">800×712 165 KB</span><svg class=\"fa d-icon d-icon-discourse-expand svg-icon\" aria-hidden=\"true\"><use xlink:href=\"#discourse-expand\"></use></svg></div></a></div></p>\n<p><div class=\"lightbox-wrapper\"><a class=\"lightbox\" href=\"//brcuploads.s3.dualstack.us-east-1.amazonaws.com/original/2X/e/e579dab30a023f12755b22e8bb1c077b34225110.jpeg\" data-download-href=\"/uploads/short-url/wK2pz5DNtgezJhhQuxaFE52CvnO.jpeg?dl=1\" title=\"DSCN0059-005.JPG\"><img src=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/e/e579dab30a023f12755b22e8bb1c077b34225110_2_450x500.jpeg\" alt=\"DSCN0059-005.JPG\" data-base62-sha1=\"wK2pz5DNtgezJhhQuxaFE52CvnO\" width=\"450\" height=\"500\" srcset=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/e/e579dab30a023f12755b22e8bb1c077b34225110_2_450x500.jpeg, https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/e/e579dab30a023f12755b22e8bb1c077b34225110_2_675x750.jpeg 1.5x, /uploads/default/original/2X/e/e579dab30a023f12755b22e8bb1c077b34225110.jpeg 2x\" data-small-upload=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/e/e579dab30a023f12755b22e8bb1c077b34225110_2_10x10.png\"><div class=\"meta\"><svg class=\"fa d-icon d-icon-far-image svg-icon\" aria-hidden=\"true\"><use xlink:href=\"#far-image\"></use></svg><span class=\"filename\">DSCN0059-005.JPG</span><span class=\"informations\">721×800 231 KB</span><svg class=\"fa d-icon d-icon-discourse-expand svg-icon\" aria-hidden=\"true\"><use xlink:href=\"#discourse-expand\"></use></svg></div></a></div></p>\n<p><div class=\"lightbox-wrapper\"><a class=\"lightbox\" href=\"//brcuploads.s3.dualstack.us-east-1.amazonaws.com/original/2X/e/e5d72072aee537852120d7106522b543e6af9085.jpeg\" data-download-href=\"/uploads/short-url/wNgfm2qrVEyTWGfUV3QAc2EGKfX.jpeg?dl=1\" title=\"DSCN0066-005.JPG\"><img src=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/e/e5d72072aee537852120d7106522b543e6af9085_2_506x500.jpeg\" alt=\"DSCN0066-005.JPG\" data-base62-sha1=\"wNgfm2qrVEyTWGfUV3QAc2EGKfX\" width=\"506\" height=\"500\" srcset=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/e/e5d72072aee537852120d7106522b543e6af9085_2_506x500.jpeg, https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/e/e5d72072aee537852120d7106522b543e6af9085_2_759x750.jpeg 1.5x, /uploads/default/original/2X/e/e5d72072aee537852120d7106522b543e6af9085.jpeg 2x\" data-small-upload=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/e/e5d72072aee537852120d7106522b543e6af9085_2_10x10.png\"><div class=\"meta\"><svg class=\"fa d-icon d-icon-far-image svg-icon\" aria-hidden=\"true\"><use xlink:href=\"#far-image\"></use></svg><span class=\"filename\">DSCN0066-005.JPG</span><span class=\"informations\">800×790 200 KB</span><svg class=\"fa d-icon d-icon-discourse-expand svg-icon\" aria-hidden=\"true\"><use xlink:href=\"#discourse-expand\"></use></svg></div></a></div></p>\n<p><div class=\"lightbox-wrapper\"><a class=\"lightbox\" href=\"//brcuploads.s3.dualstack.us-east-1.amazonaws.com/original/2X/3/3cd28b0f2704180632ca5e714241b6b62f63459e.jpeg\" data-download-href=\"/uploads/short-url/8G3Jz9KwzbbXzYx3PYei4ayTmSa.jpeg?dl=1\" title=\"DSCN0068-005.JPG\"><img src=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/3/3cd28b0f2704180632ca5e714241b6b62f63459e_2_395x500.jpeg\" alt=\"DSCN0068-005.JPG\" data-base62-sha1=\"8G3Jz9KwzbbXzYx3PYei4ayTmSa\" width=\"395\" height=\"500\" srcset=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/3/3cd28b0f2704180632ca5e714241b6b62f63459e_2_395x500.jpeg, https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/3/3cd28b0f2704180632ca5e714241b6b62f63459e_2_592x750.jpeg 1.5x, /uploads/default/original/2X/3/3cd28b0f2704180632ca5e714241b6b62f63459e.jpeg 2x\" data-small-upload=\"https://d2hneyr8lp58j4.cloudfront.net/optimized/2X/3/3cd28b0f2704180632ca5e714241b6b62f63459e_2_10x10.png\"><div class=\"meta\"><svg class=\"fa d-icon d-icon-far-image svg-icon\" aria-hidden=\"true\"><use xlink:href=\"#far-image\"></use></svg><span class=\"filename\">DSCN0068-005.JPG</span><span class=\"informations\">633×800 139 KB</span><svg class=\"fa d-icon d-icon-discourse-expand svg-icon\" aria-hidden=\"true\"><use xlink:href=\"#discourse-expand\"></use></svg></div></a></div></p>",

I think I fixed the issue. I noticed I couldn’t run a backup and eventually found that the backup bucket name was spelled wrong in app.yml. The upload bucket was fine. I fixed the spelling and backups worked again. Just tried this for giggles and it went through.

Running a rebake on the posts right now.

2 Likes

I am seeing the same issue with a relatively low number of 17 posts out of 49595 causing the rebake to abort.

49595 posts were flagged for a rebake
rake aborted!
FileStore::ToS3MigrationError: 17 posts are not remapped to new S3 upload URL. S3 migration failed for db 'default'.
/var/www/discourse/lib/file_store/to_s3_migration.rb:131:in `raise_or_log'
/var/www/discourse/lib/file_store/to_s3_migration.rb:86:in `migration_successful?'
/var/www/discourse/lib/file_store/to_s3_migration.rb:357:in `migrate_to_s3'
/var/www/discourse/lib/file_store/to_s3_migration.rb:65:in `migrate'
/var/www/discourse/lib/tasks/uploads.rake:245:in `migrate_to_s3'
/var/www/discourse/lib/tasks/uploads.rake:224:in `block in migrate_to_s3_all_sites'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rails_multisite-2.5.0/lib/rails_multisite/connection_management.rb:76:in `with_connection'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rails_multisite-2.5.0/lib/rails_multisite/connection_management.rb:86:in `each_connection'
/var/www/discourse/lib/tasks/uploads.rake:222:in `migrate_to_s3_all_sites'
/var/www/discourse/lib/tasks/uploads.rake:218:in `block in <main>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'
Tasks: TOP => uploads:migrate_to_s3
(See full trace by running task with --trace)

I’m not sure what I should look for in the output of the above rails command @gerhard - do you have some hints for me?

Getting the same error due to 3 posts :confused:

Updating the URLs in the database...
Warning: no type cast defined for type "name" with oid 19. Please cast this type explicitly to TEXT to be safe for future changes.
Removing old optimized images...
Flagging all posts containing lightboxes for rebake...
123456 posts were flagged for a rebake
rake aborted!
FileStore::ToS3MigrationError: 3 posts are not remapped to new S3 upload URL. S3 migration failed for db 'default'.
/var/www/discourse/lib/file_store/to_s3_migration.rb:131:in `raise_or_log'
/var/www/discourse/lib/file_store/to_s3_migration.rb:86:in `migration_successful?'
/var/www/discourse/lib/file_store/to_s3_migration.rb:357:in `migrate_to_s3'
/var/www/discourse/lib/file_store/to_s3_migration.rb:65:in `migrate'
/var/www/discourse/lib/tasks/uploads.rake:245:in `migrate_to_s3'
/var/www/discourse/lib/tasks/uploads.rake:224:in `block in migrate_to_s3_all_sites'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rails_multisite-2.5.0/lib/rails_multisite/connection_management.rb:76:in `with_connection'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rails_multisite-2.5.0/lib/rails_multisite/connection_management.rb:86:in `each_connection'
/var/www/discourse/lib/tasks/uploads.rake:222:in `migrate_to_s3_all_sites'
/var/www/discourse/lib/tasks/uploads.rake:218:in `block in <main>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'
Tasks: TOP => uploads:migrate_to_s3
(See full trace by running task with --trace)

@gerhard - If we get an error like this, do you know if the meaning is “success! - except for 3 posts” or “major migration failure, because of 3 posts”?

To attempt to resume, should I run rake posts:rebake, rake posts:rebake_uncooked_posts or rake uploads:migrate_to_s3 again?

Would appreciate any help :slight_smile:

From running this, can’t say I see anything that seems common between them all. They’re all being inside <p> tags is about the only thing, if that means anything.

Something weird was one with a link to a forum emoji. e.g.
https://www.example.com/images/emoji/twitter/slight_smile.png?v=9


While running rake posts:rebake_uncooked_posts manually after the migration error from my previous post (not sure if rebake is what I should be doing), this error came up:

Failed to report error: "\x8B" from ASCII-8BIT to UTF-8 3 Job exception: 403 Error:

With a bunch of unreadable text underneath. It’s still running now.

1 Like

@gerhard Any chance you know how to fix the issue of all custom avatars displaying as default avatars after migrating to s3?

Everything else is displaying great on s3. Post images, user cards, user background images. Just avatars aren’t displaying. Looks like the original avatars were migrated over, can see them in the db and the files exist on s3, so it seems it’s just an issue with the optimized ones. I see quite a lot of people have had this problem, but I have not been able to find a solution yet. :sweat_smile:


Update/Edit:

So just going to roll the dice on this one and complete the migration without the avatars working. New avatar uploads are going to s3, but they won’t display either :confused:

Hopefully it can be corrected later at some point.

Looking in /logs I see some records like this for avatar urls:

Could not find file in the store located at url: //bucketname.s3.fr-par.scw.cloud/original/2X/d/123456.JPG

The file does exist though if I visit the url and the file path looks fine. Have another instance with the same settings (only difference is the bucket name) and avatars are working really well. The path is in the identical format on the other instance also (from looking up the uploads in rails).

Hopefully that is a clue as to what is happening.

1 Like

I ran a manual migrate to S3 and got the same, but I find tombstone folder on the S3 bucket, Is this normal @gerhard?

Why those paths aren’t remapped correctly:

> current_db = RailsMultisite::ConnectionManagement.current_db
=> "default"
> cdn_path = SiteSetting.cdn_path("/uploads/#{current_db}/original").sub(/https?:/, "")
=> "/uploads/default/original"
> Post.where("cooked LIKE '%#{cdn_path}%'").pluck(:cooked)
=> []

> Updating the URLs in the database...
> Warning: no type cast defined for type "name" with oid 19. Please cast this type explicitly to TEXT to be safe for future changes.
> Removing old optimized images...
> Flagging all posts containing lightboxes for rebake...
> 10855 posts were flagged for a rebake
> rake aborted!
> FileStore::ToS3MigrationError: 1889 of 6492 uploads are not migrated to S3. S3 migration failed for db 'default'.
> /var/www/discourse/lib/file_store/to_s3_migration.rb:131:in `raise_or_log'
> /var/www/discourse/lib/file_store/to_s3_migration.rb:78:in `migration_successful?'
> /var/www/discourse/lib/file_store/to_s3_migration.rb:357:in `migrate_to_s3'
> /var/www/discourse/lib/file_store/to_s3_migration.rb:65:in `migrate'
> /var/www/discourse/lib/tasks/uploads.rake:123:in `migrate_to_s3'
> /var/www/discourse/lib/tasks/uploads.rake:102:in `block in migrate_to_s3_all_sites'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rails_multisite-2.5.0/lib/rails_multisite/connection_management.rb:76:in `with_connection'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rails_multisite-2.5.0/lib/rails_multisite/connection_management.rb:86:in `each_connection'
> /var/www/discourse/lib/tasks/uploads.rake:100:in `migrate_to_s3_all_sites'
> /var/www/discourse/lib/tasks/uploads.rake:96:in `block in <main>'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:281:in `block in execute'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:281:in `each'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:281:in `execute'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:199:in `synchronize'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:199:in `invoke_with_call_chain'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/task.rb:188:in `invoke'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:160:in `invoke_task'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:116:in `block (2 levels) in top_level'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:116:in `each'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:116:in `block in top_level'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:125:in `run_with_threads'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:110:in `top_level'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:83:in `block in run'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:186:in `standard_exception_handling'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:80:in `run'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/exe/rake:27:in `<top (required)>'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/bin/rake:23:in `load'
> /var/www/discourse/vendor/bundle/ruby/2.7.0/bin/rake:23:in `<top (required)>'
> /usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.7/lib/bundler/cli/exec.rb:63:in `load'
> /usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.7/lib/bundler/cli/exec.rb:63:in `kernel_load'
> /usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.7/lib/bundler/cli/exec.rb:28:in `run'
> /usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.7/lib/bundler/cli.rb:494:in `exec'
> /usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.7/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
> /usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.7/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
> /usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.7/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
> /usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.7/lib/bundler/cli.rb:30:in `dispatch'
> /usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.7/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
> /usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.7/lib/bundler/cli.rb:24:in `start'
> /usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.7/exe/bundle:49:in `block in <top (required)>'
> /usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.7/lib/bundler/friendly_errors.rb:130:in `with_friendly_errors'
> /usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.7/exe/bundle:37:in `<top (required)>'
> /usr/local/bin/bundle:23:in `load'
> /usr/local/bin/bundle:23:in `<main>'
> Tasks: TOP => uploads:migrate_to_s3
1 Like

Edit: It looks like your error was different to mine, sounds like some of your images didn’t get moved over to s3. Sorry, I misread. Maybe some of the info below is still useful. Did you set up your s3 uploads from this guide? Using Object Storage for Uploads (S3 & Clones)

@evenif - Sorry to hear you’re having issues with this. I was planning to write a guide for this for people getting the same issues, as a fair bit of info needed to be pieced together from different threads. But waiting to hopefully fix avatars, as you can see above I’m still having issues with that.

If you run this in rails do you get true or false?

SiteSetting.migrate_to_new_scheme

If it’s false, then you can try setting it to true like:

SiteSetting.migrate_to_new_scheme = true

Then either wait for a bit and come back later (I believe this runs every 15 minutes IIRC), but if you want to run it immediately:

Jobs::MigrateUploadScheme.new.execute(nil)

Then check again afterwards/later if SiteSetting.migrate_to_new_scheme is now false (means it should have completed).

Then run:
Upload.by_users.where("url NOT LIKE '//%' AND url NOT LIKE '/uploads/default/original/_X/%'").to_a

This should find uploads that still have issues and couldn’t be migrated. In my case, all of these uploads existed as records in the db, but the actual images were not existing.

Depending on how big the list is, you can copy page then find + replace to make a list of commands to remove these problem records via the list of upload ids.

Upload.find(1).destroy
Upload.find(2).destroy
Upload.find(3).destroy

With the upload ids replacing 1, 2, 3 etc. Copy and paste the entire list into rails and press enter. That should delete these problem records.

Then, exit rails (type exit) and all you should need to do is run is:

rake posts:rebake
or
rake posts:rebake_uncooked_posts

Uncooked allows you to resume the rebake if it fails. I’d recommend just using normal rebake unless you have tons of uploads.

Then everything should work fine, hopefully. But there is a good chance that your optimized avatars will be broken like mine, but the originals should exist on s3.

You can check if avatars moved over successfully (or at least for some users) by running this in rails for a user who is displaying a default avatar:
User.find_by_username('username').uploaded_avatar

Also can check if optimized versions exist by using this:
OptimizedImage.where(upload_id: upload_id).where(version: 2)

3 Likes

@markersocial Thanks for your help, I think my issue because I used S3 service before (from year 2005), then local storage, and then use S3 storage again. Now when I want to uploads my local images to S3 caused a lot of trouble. My S3 bucket does’t have /uploads/default.

After uploads:migrate_to_s3 I found some /images/transparent.png broken images, When I use this rake uploads:recover_from_tombstone

I got

Warning /t/topic/6216/4 had an incorrect 338c64ada8cbb0fb99bff79e833a4cc492ead00c should be c95e4b7c08702db4593332f29b40ca07fb1d9db1 storing in custom field ‘rake uploads:fix_relative_upload_links’ can fix this

then rake aborted and:

rake uploads:fix_relative_upload_links --trace
** Invoke uploads:fix_relative_upload_links (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute uploads:fix_relative_upload_links
Upload not found /uploads/files/general/111/cours-offerts.pdf in Post 23033 - /t/certificate-technique-de-soudage-college-mathieu/11060/1

I’m confused now.

2 Likes

Ah :confused: That issue is beyond my understandings, hope you can find a way to fix it or someone more knowledgeable can chime in.

Regarding not having /uploads/default in your bucket, I believe that is normal and shouldn’t be the problem. It’s the same case for my buckets (on both instances that have migrated to s3 and instances that have used s3 from day 1). Can look at the path of your embedded image in this thread and see the path does not have /uploads/default in it also.

1 Like

I too am suffering from exact same problem.

Did you happen to find at least some way!!!???

If not, I’d request you, if you happen to find some way, pls do share with me. Would be highly obliged.

I ran into a similar problem, possibly due to migrating from AWS S3 → local → DO Spaces. I ended up up writing some ruby code to manually replace old broken upload urls with the working updated url format and it seems to have fixed my problem.

In my situation, all files were successfully moved to DO Spaces in my first migration, but the migrate_to_s3 logged them as failures. This didn’t break attachments at first, but running a rebake caused them to break. More details in the linked topic.

1 Like

I too am getting this error after importing a smf2 database, enabling S3, and running rake --trace uploads:migrate_to_s3

Migrating uploads to S3 for 'default'...
Uploading files to S3...
 - Listing local files
.. => 2244 files
 - Listing S3 files
... => 2383 files
 - Syncing files to S3
....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Updating the URLs in the database...
Removing old optimized images...
Flagging all posts containing lightboxes for rebake...
454 posts were flagged for a rebake
rake aborted!
FileStore::ToS3MigrationError: 7165 of 9554 uploads are not migrated to S3. S3 migration failed for db 'default'.
/var/www/discourse/lib/file_store/to_s3_migration.rb:132:in `raise_or_log'
/var/www/discourse/lib/file_store/to_s3_migration.rb:79:in `migration_successful?'
/var/www/discourse/lib/file_store/to_s3_migration.rb:373:in `migrate_to_s3'
/var/www/discourse/lib/file_store/to_s3_migration.rb:66:in `migrate'
/var/www/discourse/lib/tasks/uploads.rake:123:in `migrate_to_s3'
/var/www/discourse/lib/tasks/uploads.rake:102:in `block in migrate_to_s3_all_sites'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rails_multisite-3.1.0/lib/rails_multisite/connection_management.rb:80:in `with_connection'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rails_multisite-3.1.0/lib/rails_multisite/connection_management.rb:90:in `each_connection'
/var/www/discourse/lib/tasks/uploads.rake:100:in `migrate_to_s3_all_sites'
/var/www/discourse/lib/tasks/uploads.rake:96:in `block in <main>'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `block in execute'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `each'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `execute'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/task.rb:188:in `invoke'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:160:in `invoke_task'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `each'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block in top_level'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:125:in `run_with_threads'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:110:in `top_level'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:83:in `block in run'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/lib/rake/application.rb:80:in `run'
bin/rake:13:in `<top (required)>'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/cli/exec.rb:58:in `load'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/cli/exec.rb:58:in `kernel_load'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/cli/exec.rb:23:in `run'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/cli.rb:477:in `exec'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/cli.rb:31:in `dispatch'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/cli.rb:25:in `start'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/exe/bundle:49:in `block in <top (required)>'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/friendly_errors.rb:128:in `with_friendly_errors'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/exe/bundle:37:in `<top (required)>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'
Tasks: TOP => uploads:migrate_to_s3

markersocial, when I run Upload.by_users.where("url NOT LIKE '//%' AND url NOT LIKE '/uploads/default/original/_X/%'").to_a the output is []

I’m presuming that means my issue is different than yours?

ipoopfool, I think I might be experiencing the same issue as you. Would you mind sharing your ruby code that fixed this for you? :blue_heart:

Thank you.

When I run the script, I get this error:

NameError: undefined local variable or method `upload_regex' for main:Object
from /var/www/discourse/script/discourse-fix-old-broken-uploads.rb:30:in `update_post_upload_links'

Any suggestions?

sorry for the necro, removing the linked I shared before, so I thought I’d just copy paste the code directly here

Posts from before Sept 2020 have broken uploads after migrating to object stores and rebaking. It seems like the uploads are fine, the urls in the raw were just incorrectly formatted, so I'm manually fixing them.

# Note: doing an update this way does not seem to add revision history to posts. Make a manual database backup before running just in case.

# https://rubular.com/
# Capture groups: /uploads/..., sha1 file name
upload_regex = /\((\/uploads\/[\/\w]+\/)([0-9a-f]+)\.\w+\)/


def to_short_name(sha1)
  # REQUIRES RUNNING IN DISCOURSE RAILS CONSOLE
  Upload.base62_sha1(sha1)
end

# a sanity check. You can go through all posts and pull out how many times /uploads/ appears.
# then run the regex match to see if the number of matches equals it.
def count_num_broken_upload_links(posts)
  num_string_match = 0
  num_regex_match = 0

  posts.each do |post|
    num_string_match += post.raw.count("(/uploads/") # THIS IS BROKEN. COUNT ONLY WORKS FOR CHARACTERS.
    num_regex_match += post.raw.scan(upload_regex).count
  end

  puts "Sanity check number of times old upload links appear"
  puts "Simple string matches " + num_string_match
  puts "Regex matches " + num_regex_match
end

def update_post_upload_links(post)
  matches = post.raw.scan(upload_regex)
  fixed_raw = matches.reduce(post.raw) do |accumulator, match|
    prefix = match[0]
    sha1 = match[1]
    # sub, not gsub, so we sequentially replace each attachment one by one
    accumulator.sub(prefix, "upload://").sub(sha1, to_short_name(sha1))
  end

  if post.raw != fixed_raw
    # Updating the post.raw appears to also trigger a rebake. But I still want to do a full rebake later anyway just to be safe.
    post.update(raw: fixed_raw)
  end

  puts "Updated post id #{post.id}"
end

# The problem I'm fixing occurs up until the end of 2020-9-5, but I'll add a few extra days just to be safe.
posts = Post.where("created_at < ?", Date.new(2020,9,7))

posts.each { |post| update_post_upload_links(post) }

And another person commented

In my scenario, the upload_regex variable need to be global, not a local. So, upload_regex need to changed to $upload_regex.