Migrate_to_s3 fails

I’m trying to move uploads to s3 on a relatively busy site.
Everything goes as planned except when I run rake uploads:migrate_to_s3, I am presented with the following error:

Updating the URLs in the database...
Removing old optimized images...
Flagging all posts containing lightboxes for rebake...
2023 posts were flagged for a rebake
rake aborted!
FileStore::ToS3MigrationError: 1 of 9629 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-4.0.0/lib/rails_multisite/connection_management.rb:80:in `with_connection'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rails_multisite-4.0.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>'
/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)

This line in particular caught my attention FileStore::ToS3MigrationError: 1 of 9629 uploads are not migrated to S3. S3 migration failed for db 'default'.

I’m assuming it means there is one problematic file that is causing the entire take task to fail? is there a way to figure out which file is causing the problem and maybe just remove it so the rest of the task completes peacefully?

1 Like

Some Update:
Site seems to be loading normally and from what I can see, uploaded images are being served from the CDN.
The local copy of all uploads still exists on the server. I assume this is because the migration is marked as failed.
here is an output of rake uploads:s3_migration_status if it is helpful in any way?

# rake uploads:s3_migration_status
1 of 9630 uploads are not migrated to S3. S3 migration failed for db 'default'.
19 posts are not remapped to new S3 upload URL. S3 migration failed for db 'default'.
No posts require rebaking
Site is not ready for migration

You might find a reference to which file failed via Admin > Logs > Error Logs

If that doesn’t help, I think searching the database for posts with cooked that contain yourdomain/uploads/ will get you a list of those 19 posts mentioned in the migration status. Worst case, you could then manually look at those and compare any uploads with what’s in S3.

Something like:

rails c
Post.where("cooked like '%discourse.example.com/uploads/%'")

Hi @Simon_Manning

I tried your suggestions, Sadly, no luck finding anything relevant in error logs. I do see an entry like
Failed to optimize image: unknown reason but it doesn’t give me any more info than what it says.

I tried your suggested query as well, I do get a bunch of posts as a result but actually visiting those posts, they contain a bunch of thumbnails from from onebox previews. when I try to open image in new tab on those thumbnails, those appear to be loading from the CDN.

I think I would expect to see something like that for 18 of those 19 posts, maybe even all 19 if the one failed upload happens to be in a post with multiple uploads.

What might be easier is to pull out all the filenames from the query result (e.g. f8a2d9381889b8693db2777acac566bd7b134fa5.png) and search for them in S3. In theory, exactly one should be missing. Ignore the derivatives, just look for the ones with /originals/ in the path.

A bit tedious but shouldn’t take too long if that query did only select those 19 posts.

1 Like

So I found the culprit, How do I proceed?

What seems logical to me might not be the best way out
Here are the options that I see:

  1. Remove reference of file from the offending post
  2. Delete file from the server
  3. remap the file link to something generic

or maybe I’m thinking totally wrong?

If there’s not an obvious solution to get it to upload successfully, either of options 1 or 3 seem like good ways to go. 2 I would probably leave to Discourse, specifically the job to clean up orphaned uploads, the interval for which is controlled by the clean orphan uploads grace period hours setting.

It might be a good idea to grab the file before it’s deleted though. If it doesn’t contain any sensitive data, it could be useful for testing why it can’t be uploaded during the migration.

Something else you could try after having removed the file and completed the migration (and having downloaded the failing file) is to edit the post again to put the file back in. It’d be interesting to know if it fails to upload to S3 entirely or only during the migration task.