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/%'")
1 Like

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.


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.

I got the same error, on 64 post. Impossible to find them on 80K files…

What would happen if I delete local files (most of them are alredy migrated)?

I confirm that files are migrated, but I got the same error.

Any suggestions? Uploads are on s3 and work fine, but the task: migrate to s3 fails.

If you are okay with losing those 64 files, what I did when this same rake task kept failing on me is to edit it, and make it error inly if there are 100 errors instead of 1.

It would be fine for me.
What Shoud I do to edit the task?

You can first try to run it with: SKIP_FAILED=1 rake uploads:migrate_to_s3.

If that doesn’t work edit this line here and try again

Tried with SKIP_FAILED=100 rake uploads:migrate_to_s3

=> 0 files

  • Listing S3 files
    … … => 81070 files
  • Syncing files to S3

Updating the URLs in the database…
Removing old optimized images…
Flagging all posts containing lightboxes for rebake…
54453 posts were flagged for a rebake
53 posts are not remapped to new S3 upload URL. S3 migration failed for db ‘default’.

I didn’t understand how to put the modified file on my discourse script

I may have misunderstood, but I think you need to run SKIP_FAILED=1 to turn the SKIP_FAILED on, rather than assign an amount.

1 Like

Tried with this command. … same

Ah, that’s unfortunate. There’s @Falco’s other suggestion of editing the rake task - but unfortunately I don’t know how to do that, and I can’t find any instructions when I search. :slightly_smiling_face:

Hopefully someone else can help? :crossed_fingers:

1 Like

I managed to create myscript.rb on my /shared folder but I can’t run it.

Any other suggestions? @vulkanino I noticed that you had a similar problem