# 尝试使用自定义 S3 存储时出错

**URL:** https://meta.discourse.org/t/errors-trying-to-use-custom-s3-storage/200552
**Category:** Support
**Created:** [2021年八月16日 05:39 UTC](https://meta.discourse.org/t/errors-trying-to-use-custom-s3-storage/200552 "2021-08-16T05:39:55Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![schungx](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/schungx/32/70989_2.png) [@schungx](https://meta.discourse.org/u/schungx)
#### Post date: [2021年八月16日 05:39 UTC](https://meta.discourse.org/t/errors-trying-to-use-custom-s3-storage/200552/1 "2021-08-16T05:39:55Z")

</div>

由于 `Azure Blob Storage` 插件频繁出错，我感到十分沮丧，因此我搭建了一台 `Flexify.IO` 服务器，用于将 Azure 协议转换为 S3 协议。

然而，在将其配置到 Discourse 后，它无法正常工作。报错信息如下：

```plaintext
Failed to open TCP connection to support.xxx.xxx.xxx.xxx:443 (getaddrinfo: Name or service not known)

```

其中 `xx.xx.xx.xx` 是 S3 网关的 IP 地址，而 `support` 是存储桶名称……

奇怪的是……我用 `S3 Browser` 测试过该端点，一切正常。

所以，有没有好心人能告诉我，我哪里做错了？

我怀疑问题可能出在区域上，因为该端点暴露的区域是 `eastasia`（一个有效的 Azure 区域），但在列表中我只能选择标准的 AWS 区域。不过，这仍然很奇怪，因为错误信息似乎表明无法连接到端点本身，而不是区域不匹配的问题。

---

<div class="post-metadata">

### Author: ![schungx](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/schungx/32/70989_2.png) [@schungx](https://meta.discourse.org/u/schungx)
#### Post date: [2021年八月16日 09:47 UTC](https://meta.discourse.org/t/errors-trying-to-use-custom-s3-storage/200552/2 "2021-08-16T09:47:21Z")

</div>

我总觉得我的 `s3_bucket` 设置有问题，因为它把存储桶名称直接附加到了端点 URL 上。

> <https://github.com/discourse/discourse/blob/9b7c17b925ef185b139c5fd53cd6ea3207066036/app/models/site_setting.rb#L167>

我是否应该改用 `bucket/folder` 格式？这两个字段分别应该填什么？

编辑：从源代码来看，这似乎是硬编码的。如果我的存储提供商不使用存储桶名称作为前缀怎么办？

---

<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: [2021年八月16日 15:04 UTC](https://meta.discourse.org/t/errors-trying-to-use-custom-s3-storage/200552/3 "2021-08-16T15:04:41Z")

</div>

对于您的使用场景，建议您参考 [使用对象存储进行上传（S3 及克隆）](https://meta.discourse.org/t/using-object-storage-for-uploads-s3-clones/148916) 的文档。这种方式更加灵活。一旦配置成功，您也欢迎将其添加到 Wiki 中！

---

<div class="post-metadata">

### Author: ![schleifer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/schleifer/32/86416_2.png) [@schleifer](https://meta.discourse.org/u/schleifer)
#### Post date: [2021年八月17日 01:19 UTC](https://meta.discourse.org/t/errors-trying-to-use-custom-s3-storage/200552/4 "2021-08-17T01:19:38Z")

</div>

鉴于此：

> [@schungx](#):
>
> `xx.xx.xx.xx` 是 S3 网关的 IP 地址

那么以下内容绝对是错误的：

> [@schungx](#):
>
> `support.xxx.xxx.xxx.xxx:443`

错误信息已经说明了这一点。`getaddrinfo: Name or service not known` 表示 DNS 查找失败；根本不可能存在像 “support.303.303.303.303” 这样的主机名。

你从哪里得到的 “support” 部分？

> [@schungx](#):
>
> 我应该改用 `bucket/folder` 格式吗？这两个部分分别应该填什么？

[flexify 文档](https://flexify.io/configure-application/) 指出：

> 将应用程序使用的 S3 端点更改为 `s3.flexify.io`（或设置中显示的其他主机名）。

因此，你无法通过管理控制台来更改它，而是应该阅读 @falco 提到的主题 ⏫，并从以下内容开始：

```plaintext
  DISCOURSE_S3_ENDPOINT: https://s3.flexify.io

```

---

<div class="post-metadata">

### Author: ![schungx](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/schungx/32/70989_2.png) [@schungx](https://meta.discourse.org/u/schungx)
#### Post date: [2021年八月17日 03:19 UTC](https://meta.discourse.org/t/errors-trying-to-use-custom-s3-storage/200552/5 "2021-08-17T03:19:38Z")

</div>

> [@schleifer](#):
>
> 你提到的“支持”部分是从哪里来的？

哦，抱歉。`support` 是存储桶名称。我可以看到 S3 只是将存储桶名称添加到域名前面，而 Discourse 正是基于这一假设工作的。

> [@schleifer](#):
>
> 所以你无法通过管理控制台来更改它，而是需要阅读 @falco 提到的主题 ⏫，并从以下内容开始：

是的，我已经想到这一点了。所以我必须在 `app.yaml` 中硬编码端点，对吗？

---

<div class="post-metadata">

### Author: ![schungx](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/schungx/32/70989_2.png) [@schungx](https://meta.discourse.org/u/schungx)
#### Post date: [2021年八月17日 04:14 UTC](https://meta.discourse.org/t/errors-trying-to-use-custom-s3-storage/200552/6 "2021-08-17T04:14:45Z")

</div>

@Falco @schleifer 好的，我试过了，但不幸的是，它仍然报出完全相同的错误。

因此，我认为即使将其硬编码到 `app.yaml` 中也无法正常工作。它们似乎进入了同一个代码流。

这是我的 `app.yaml` 设置：

```yaml
  DISCOURSE_USE_S3: true
  DISCOURSE_S3_REGION: eastasia
  DISCOURSE_S3_ENDPOINT: https://??.??.??.??
  DISCOURSE_S3_ACCESS_KEY_ID: ???
  DISCOURSE_S3_SECRET_ACCESS_KEY: ???
  DISCOURSE_S3_BUCKET: support

```

它仍然试图访问 `support.??.??.??.??:443`。

所以，我猜想 Discourse 中的存储桶 _必须_ 构成一个子域名？

## 编辑 1

好的，我已经废弃了 `app.yaml` 设置，并创建了一个带有 `support` 的子域名，指向 `xx.xx.xx.xx`。现在，当我上传时，它会长时间旋转，然后返回一个通用错误：

```plaintext
Aws::S3::Errors::BadRequest

```

有没有办法获取确切的错误消息？

---

<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: [2021年八月17日 16:30 UTC](https://meta.discourse.org/t/errors-trying-to-use-custom-s3-storage/200552/7 "2021-08-17T16:30:26Z")

</div>

> [@schungx](#):
>
> 有没有办法获取确切的错误消息？

你试过查看 `/logs` 页面吗？

---

<div class="post-metadata">

### Author: ![schungx](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/schungx/32/70989_2.png) [@schungx](https://meta.discourse.org/u/schungx)
#### Post date: [2021年八月18日 06:05 UTC](https://meta.discourse.org/t/errors-trying-to-use-custom-s3-storage/200552/8 "2021-08-18T06:05:46Z")

</div>

是的，但那里什么都没有……

---

<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: [2021年八月18日 14:11 UTC](https://meta.discourse.org/t/errors-trying-to-use-custom-s3-storage/200552/9 "2021-08-18T14:11:08Z")

</div>

您为 endpoint 环境变量使用的确切值是什么？

---

<div class="post-metadata">

### Author: ![schungx](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/schungx/32/70989_2.png) [@schungx](https://meta.discourse.org/u/schungx)
#### Post date: [2021年八月19日 02:26 UTC](https://meta.discourse.org/t/errors-trying-to-use-custom-s3-storage/200552/10 "2021-08-19T02:26:04Z")

</div>

好的，我在 [Flexify.IO](http://Flexify.IO) 中找到了一个设置子域名模式的选项。现在它已经可以正常工作了。🍾

我会更新 Wiki！

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [2021年九月18日 02:27 UTC](https://meta.discourse.org/t/errors-trying-to-use-custom-s3-storage/200552/11 "2021-09-18T02:27:03Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
