# 기존 업로드 파일을 S3로 마이그레이션

**URL:** https://meta.discourse.org/t/migrating-old-uploads-to-s3/134150
**Category:** Self-hosting
**Tags:** s3
**Created:** [11월 22, 2019, 12:34오후 UTC](https://meta.discourse.org/t/migrating-old-uploads-to-s3/134150 "2019-11-22T12:34:49Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Bathinda](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bathinda/32/135888_2.png) [@Bathinda](https://meta.discourse.org/u/Bathinda)
#### Post date: [11월 22, 2019, 12:34오후 UTC](https://meta.discourse.org/t/migrating-old-uploads-to-s3/134150/1 "2019-11-22T12:34:49Z")

</div>

새로 업로드한 파일은 Amazon S3에 정상적으로 업로드되고 있지만, 기존 파일을 로컬에서 S3로 이동하는 이 명령은 실패합니다:

```plaintext
DISCOURSE_S3_BUCKET="bucketname" DISCOURSE_S3_REGION="ap-south-1" DISCOURSE_S3_ACCESS_KEY_ID="AKIchangedabitUNM2" DISCOURSE_S3_SECRET_ACCESS_KEY="rX6Pf3C_changedabit_YKKtdVrnbw6B" DISCOURSE_S3_CDN_URL="https://bucketname.s3.ap-south-1.amazonaws.com" rake uploads:migrate_to_s3

```

15일 전에는 이 명령이 완벽하게 작동했습니다.  
오늘은 다음과 같은 오류가 발생합니다:

```plaintext
Checking if default already migrated...
7 of 14 uploads are not migrated to S3. S3 migration failed for db 'default'.
1 posts are not remapped to new S3 upload URL. S3 migration failed for db 'default'.
Looking for missing uploads on: default

0 post uploads are missing.

No posts require rebaking
Migrating uploads to S3 for 'default'...
Some uploads were not migrated to the new scheme. Please run these commands in the rails console

SiteSetting.migrate_to_new_scheme = true
Jobs::MigrateUploadScheme.new.execute(nil)

```

또한 rails 콘솔에서 다음 두 명령을 실행해 보았지만, 아무런 효과가 없었습니다:

```plaintext
SiteSetting.migrate_to_new_scheme = true
Jobs::MigrateUploadScheme.new.execute(nil)

```

참고: 업로드 파일은 버킷 `bucketname/uploads`의 하위 폴더에 저장되도록 설정되어 있습니다.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [11월 22, 2019, 4:59오후 UTC](https://meta.discourse.org/t/migrating-old-uploads-to-s3/134150/2 "2019-11-22T16:59:55Z")

</div>

여기서 @vinothkannans 님, 혹시 아이디어가 있으신가요?

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [11월 22, 2019, 7:40오후 UTC](https://meta.discourse.org/t/migrating-old-uploads-to-s3/134150/3 "2019-11-22T19:40:03Z")

</div>

> [@Bathinda](#):
>
> 이 명령이 로컬에서 S3로 이전하는 데 실패하고 있습니다. 15일 전까지만 해도 이 명령은 완벽하게 작동했습니다.

오류가 반환되고 있나요? `uploads:migrate_to_s3` 레이크 태스크 코드에서 최근 큰 변경 사항을 찾아볼 수 없습니다.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [11월 22, 2019, 8:01오후 UTC](https://meta.discourse.org/t/migrating-old-uploads-to-s3/134150/4 "2019-11-22T20:01:12Z")

</div>

저도 비슷한 문제를 겪었습니다. 업로드 경로가 해당 테스트가 기대하는 경로와 일치하지 않는 문제였어요(예: 경로에서 'default’가 빠져 있는 경우).

제가 한 일은 (대략적이고 아마도 도움이 안 될 표현으로) 검색이 어떤 방식으로 ‘나쁜’ 업로드라고 판단되는 업로드를 찾고 있는지 확인하고, 그것들을 수정하는 것이었습니다.

---

<div class="post-metadata">

### Author: ![Bathinda](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bathinda/32/135888_2.png) [@Bathinda](https://meta.discourse.org/u/Bathinda)
#### Post date: [11월 23, 2019, 6:38오전 UTC](https://meta.discourse.org/t/migrating-old-uploads-to-s3/134150/5 "2019-11-23T06:38:12Z")

</div>

우연히 해결했는데, 어떻게 해결된 건지 아직도 정확히 모르겠어요.

명령어를 몇 번이나 바꿔봤는데, 대부분 명령어에 ‘dualstack’ 단어를 추가하거나 제거하는 정도였고, Discourse 설정의 CDN URL 필드도 `https://bxyzbucket1.s3.dualstack.ap-south-1.amazonaws.com`으로 변경해 봤어요.  
하지만 여전히 작동하지 않는 것 같았죠. 그러다 갑자기 OP에 언급된 두 개의 명령어를 rails 콘솔에서 다시 실행해 봤어요.  
그때쯤 우연히 한 단계 더 진행한 것이, 명령어의 버킷 이름 뒤에 `/uploads`를 붙인 것이었어요.

그리고 ‘우연히’ (여기서 또 다른 '우연’이라는 단어가 필요하네요 :)) 성공했어요.  
그래서 누가 이 문제 해결의 영웅인지 말하기 어렵네요.  
그래도 들어줘서 감사합니다.

---

<div class="post-metadata">

### Author: ![Bathinda](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bathinda/32/135888_2.png) [@Bathinda](https://meta.discourse.org/u/Bathinda)
#### Post date: [11월 23, 2019, 7:04오전 UTC](https://meta.discourse.org/t/migrating-old-uploads-to-s3/134150/6 "2019-11-23T07:04:27Z")

</div>

어떤 오류가 발생했든, 이미 원본 게시글에 포함시켰습니다. 하지만 그 오류/메시지가 진단에 큰 도움이 되지 않았다는 점에는 동의합니다.

아쉬운ことに, 오늘도 마이그레이션 문제를 이해할 수 없어 로컬에 업로드를 하나 생성했습니다. 그런 다음 다음 명령을 사용하여 S3로 다시 옮기려 했습니다:

```plaintext
DISCOURSE_S3_BUCKET="bxyzbucket1/uploads" DISCOURSE_S3_REGION="ap-south-1" DISCOURSE_S3_ACCESS_KEY_ID="AKIAchangedBRF" DISCOURSE_S3_SECRET_ACCESS_KEY="H0P6Oo8changed1AuLkRDCQK8" DISCOURSE_S3_CDN_URL="https://bxyzbucket1.s3.ap-south-1.amazonaws.com/uploads" rake uploads:migrate_to_s3

```

새 업로드 1개는 S3로 이동했지만, 다음과 같은 오류가 포함되어 있었습니다:

```plaintext
1 of 14 uploads are not migrated to S3. S3 migration failed for db 'default'.
1 posts are not remapped to new S3 upload URL. S3 migration failed for db 'default'.

```

이 오류는 무엇을 의미합니까? '로컬 \>\> S3’로 수천 장의 이미지를 이동해야 하는 경우 문제가 될 수 있습니까? @vinothkannans

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [11월 25, 2019, 7:32오후 UTC](https://meta.discourse.org/t/migrating-old-uploads-to-s3/134150/7 "2019-11-25T19:32:42Z")

</div>

다음 명령을 rails 콘솔에서 실행한 후 출력을 여기에 올려주실 수 있나요?

```plaintext
Upload.by_users.where("url NOT LIKE '//%' AND url NOT LIKE '#{GlobalSetting.relative_url_root}/uploads/default/original/_X/%'")

```

---

<div class="post-metadata">

### Author: ![Bathinda](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bathinda/32/135888_2.png) [@Bathinda](https://meta.discourse.org/u/Bathinda)
#### Post date: [12월 20, 2019, 12:29오후 UTC](https://meta.discourse.org/t/migrating-old-uploads-to-s3/134150/8 "2019-12-20T12:29:15Z")

</div>

> [@vinothkannans](#):
>
> ```plaintext
> Upload.by_users.where("url NOT LIKE '//%' AND url NOT LIKE '#{GlobalSetting.relative_url_root}/uploads/default/original/_X/%'")
> 
> ```

오래간만에 이 작업에 다시 돌아왔습니다.  
보내주신 rails 명령어를 실행 중인데, 아무 출력도 나오지 않습니다:

 ![image](https://global.discourse-cdn.com/meta/original/3X/5/8/587b0c6139e82147962b25748df042d0905d7103.png)

여전히 " S3 migration failed for db ‘default’" 오류가 발생합니다.  
게시글의 업로드/이미지는 버킷과 사이트에서 정상적으로 표시되지만, 사이트 로고 등(관리자 설정 \> 브랜딩)은 누락되어 있고, 새 이미지로 업데이트하려고 해도 되지 않습니다. 단순히 빈 상태로만 남아 있습니다.

또한 (오류 없이) `rake posts:rebake_uncooked_posts`와 `rake posts:rebake`도 실행해 보았습니다.

---

<div class="post-metadata">

### Author: ![Bathinda](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bathinda/32/135888_2.png) [@Bathinda](https://meta.discourse.org/u/Bathinda)
#### Post date: [5월 11, 2021, 10:47오전 UTC](https://meta.discourse.org/t/migrating-old-uploads-to-s3/134150/10 "2021-05-11T10:47:25Z")

</div>

@vinothkannans 1년이 지난 후에도 어떤 이유로든 해당 과정을 다시 수행하면 여전히 동일한 오류가 발생합니다.
