# Migrate\_to\_s3 fails

**URL:** <https://meta.discourse.org/t/migrate-to-s3-fails/209499>\
**Category:** Self-hosting\
**Tags:** s3\
**Created:** [November 18, 2021, 6:54am UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499 "2021-11-18T06:54:02Z")\
**Posts on this page:** 20\
**Page:** 1

## AI-generated summary

The discussion revolves around the error "Migrate to S3 fails" when running the `rake uploads:migrate_to_s3` task. The error occurs due to a single file failing to migrate, causing the entire task to fail.

[itsbhanusharma](/t/-/209499/1) initially reported the issue and provided the error message. [Simon_Manning](/t/-/209499/4) suggested checking the error logs and searching the database for posts with `cooked` that contain `yourdomain/uploads/` to identify the problematic file.

After identifying the culprit, [itsbhanusharma](/t/-/209499/7) asked for advice on how to proceed. [Simon_Manning](/t/-/209499/8) suggested removing the reference of the file from the offending post or remapping the file link to something generic.

Another user, [Saiano](/t/-/209499/10), reported a similar issue with 64 posts failing to migrate. [Falco](/t/-/209499/13) suggested editing the rake task to ignore a certain number of failures. [Simon_Manning](/t/-/209499/26) provided instructions on how to do this by entering the container and using `sed` to make an in-place substitution on the specific line.

Ultimately, [Saiano](/t/-/209499/28) was able to resolve the issue by following [Simon_Manning's](/t/-/209499/26) instructions and editing the `to_s3_migration.rb` file.

<div class="post-metadata">

**Author:** ![itsbhanusharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/itsbhanusharma/32/180717_2.png) [@itsbhanusharma](https://meta.discourse.org/u/itsbhanusharma)\
**Post date:** [November 18, 2021, 6:54am UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/1 "2021-11-18T06:54:03Z")

</div>

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:

```plaintext
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?

---

<div class="post-metadata">

**Author:** ![itsbhanusharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/itsbhanusharma/32/180717_2.png) [@itsbhanusharma](https://meta.discourse.org/u/itsbhanusharma)\
**Post date:** [November 18, 2021, 12:25pm UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/3 "2021-11-18T12:25:57Z")

</div>

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?

```plaintext
# 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

```

---

<div class="post-metadata">

**Author:** ![Simon\_Manning](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon_manning/32/198596_2.png) [@Simon\_Manning](https://meta.discourse.org/u/Simon_Manning)\
**Post date:** [November 18, 2021, 12:50pm UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/4 "2021-11-18T12:50:45Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![itsbhanusharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/itsbhanusharma/32/180717_2.png) [@itsbhanusharma](https://meta.discourse.org/u/itsbhanusharma)\
**Post date:** [November 18, 2021, 1:41pm UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/5 "2021-11-18T13:41:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Simon\_Manning](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon_manning/32/198596_2.png) [@Simon\_Manning](https://meta.discourse.org/u/Simon_Manning)\
**Post date:** [November 18, 2021, 2:15pm UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/6 "2021-11-18T14:15:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![itsbhanusharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/itsbhanusharma/32/180717_2.png) [@itsbhanusharma](https://meta.discourse.org/u/itsbhanusharma)\
**Post date:** [November 18, 2021, 4:47pm UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/7 "2021-11-18T16:47:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Simon\_Manning](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon_manning/32/198596_2.png) [@Simon\_Manning](https://meta.discourse.org/u/Simon_Manning)\
**Post date:** [November 18, 2021, 9:24pm UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/8 "2021-11-18T21:24:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Simon\_Manning](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon_manning/32/198596_2.png) [@Simon\_Manning](https://meta.discourse.org/u/Simon_Manning)\
**Post date:** [November 20, 2021, 10:38pm UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/9 "2021-11-20T22:38:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Saiano](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/saiano/32/153394_2.png) [@Saiano](https://meta.discourse.org/u/Saiano)\
**Post date:** [February 17, 2022, 10:02am UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/10 "2022-02-17T10:02:58Z")

</div>

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)?

---

<div class="post-metadata">

**Author:** ![Saiano](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/saiano/32/153394_2.png) [@Saiano](https://meta.discourse.org/u/Saiano)\
**Post date:** [February 17, 2022, 2:24pm UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/11 "2022-02-17T14:24:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Saiano](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/saiano/32/153394_2.png) [@Saiano](https://meta.discourse.org/u/Saiano)\
**Post date:** [February 18, 2022, 6:32am UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/12 "2022-02-18T06:32:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
**Post date:** [February 18, 2022, 12:38pm UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/13 "2022-02-18T12:38:41Z")

</div>

> [@Saiano](#):
>
> I got the same error, on 64 post. Impossible to find them on 80K files

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.

---

<div class="post-metadata">

**Author:** ![Saiano](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/saiano/32/153394_2.png) [@Saiano](https://meta.discourse.org/u/Saiano)\
**Post date:** [February 18, 2022, 2:30pm UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/14 "2022-02-18T14:30:19Z")

</div>

> [@Falco](#):
>
> f 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?

---

<div class="post-metadata">

**Author:** ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
**Post date:** [February 18, 2022, 3:04pm UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/15 "2022-02-18T15:04:39Z")

</div>

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

> <https://github.com/discourse/discourse/blob/main/lib/file_store/to_s3_migration.rb#L77>

---

<div class="post-metadata">

**Author:** ![Saiano](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/saiano/32/153394_2.png) [@Saiano](https://meta.discourse.org/u/Saiano)\
**Post date:** [February 18, 2022, 4:04pm UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/16 "2022-02-18T16:04:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)\
**Post date:** [February 18, 2022, 4:28pm UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/17 "2022-02-18T16:28:31Z")

</div>

> [@Saiano](#):
>
> Tried with SKIP\_FAILED=100 rake uploads:migrate\_to\_s3

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.

---

<div class="post-metadata">

**Author:** ![Saiano](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/saiano/32/153394_2.png) [@Saiano](https://meta.discourse.org/u/Saiano)\
**Post date:** [February 18, 2022, 4:44pm UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/18 "2022-02-18T16:44:20Z")

</div>

> [@Saiano](#):
>
> SKIP\_FAILED=1 rake uploads:migrate\_to\_s3

Tried with this command. … same  
Result

---

<div class="post-metadata">

**Author:** ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)\
**Post date:** [February 19, 2022, 6:25pm UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/19 "2022-02-19T18:25:19Z")

</div>

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. 🙂

Hopefully someone else can help? 🤞

---

<div class="post-metadata">

**Author:** ![Saiano](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/saiano/32/153394_2.png) [@Saiano](https://meta.discourse.org/u/Saiano)\
**Post date:** [February 20, 2022, 3:06pm UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/20 "2022-02-20T15:06:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Saiano](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/saiano/32/153394_2.png) [@Saiano](https://meta.discourse.org/u/Saiano)\
**Post date:** [February 21, 2022, 12:05pm UTC](https://meta.discourse.org/t/migrate-to-s3-fails/209499/21 "2022-02-21T12:05:31Z")

</div>

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

[Next page](https://meta.discourse.org/t/migrate-to-s3-fails/209499.md?page=2)
