# 清理上传并清除 S3 中的上传内容

**URL:** <https://meta.discourse.org/t/cleaning-up-uploads-and-purging-uploads-from-s3/248343>\
**Category:** Site Management\
**Tags:** reference, file-management\
**Created:** [2022年十二月9日 01:26 UTC](https://meta.discourse.org/t/cleaning-up-uploads-and-purging-uploads-from-s3/248343 "2022-12-09T01:26:48Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![elmuerte](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/elmuerte/32/456517_2.png) [@elmuerte](https://meta.discourse.org/u/elmuerte)\
**Post date:** [2024年十二月24日 08:08 UTC](https://meta.discourse.org/t/cleaning-up-uploads-and-purging-uploads-from-s3/248343/3 "2024-12-24T08:08:17Z")

</div>

启用“清理上传”功能，看到警告消息会让人感到不安。在将现有论坛迁移到 Discourse 时，此设置将保持禁用状态。并非所有导入脚本都能正确注册帖子中的所有上传内容，因此如果启用该设置，您可能会丢失大量附件。

您可以使用以下查询来检查上传内容是否已正确引用到帖子中：

```sql
select p.post_id, u.id as upload_id
from (select id post_id, (regexp_matches(cooked, 'data-download-href=[^\\s]+/default/([a-z0-9]+)', 'g'))[1] upload_sha from posts where raw like '%upload://%' order by created_at) as p
join uploads u on u.sha1 = p.upload_sha
where not exists(select * from upload_references r where r.upload_id = u.id)

```

如果一切正常，此查询不应返回任何行。如果您在 [Data Explorer 插件](https://meta.discourse.org/t/discourse-data-explorer/32566) 中使用此查询，它还将整齐地链接到包含未引用附件的帖子。

如果上述查询返回结果，您可以使用以下查询来修复缺失的上传引用：

```sql
insert into upload_references(upload_id, target_type, target_id, created_at, updated_at)
select u.id, 'Post', p.post_id, u.created_at, u.updated_at
from (select id post_id, (regexp_matches(cooked, 'data-download-href=[^\\s]+/default/([a-z0-9]+)', 'g'))[1] upload_sha from posts where raw like '%upload://%' order by created_at) as p
join uploads u on u.sha1 = p.upload_sha
on conflict do nothing;

```

您需要直接访问数据库才能进行更正。

---

_[View the full topic](https://meta.discourse.org/t/cleaning-up-uploads-and-purging-uploads-from-s3/248343)._
