# 上传个人资料照片时出现S3错误

**URL:** https://meta.discourse.org/t/s3-errors-uploading-profile-photos/203063
**Category:** General
**Created:** [2021年九月9日 10:00 UTC](https://meta.discourse.org/t/s3-errors-uploading-profile-photos/203063 "2021-09-09T10:00:40Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![bigfudge](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@bigfudge](https://meta.discourse.org/u/bigfudge)
#### Post date: [2021年九月9日 10:00 UTC](https://meta.discourse.org/t/s3-errors-uploading-profile-photos/203063/1 "2021-09-09T10:00:41Z")

</div>

我正在运行最新的测试版，并已启用 S3 来管理上传的文件。

帖子中的上传功能正常（文件已上传至 S3 并在帖子中显示），但我发现现在尝试上传个人资料照片时，UI 会提示“访问被拒绝”错误。管理员错误日志中没有任何记录。

我的一些早期用户似乎成功上传了头像图片，但这些图片是本地存储的，例如查看源代码：

```plaintext
<img alt="" width="45" height="45" src="/user_avatar/example.com/jackie/90/29_2.png" class="avatar" title="image title" aria-label="Imagery/FIT">

```

是否有方法将个人资料图片迁移到 S3，并确保新图片可以上传到那里？

编辑：应该补充说明，我已启用了安全媒体上传功能。

---

<div class="post-metadata">

### Author: ![markersocial](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/markersocial/32/170136_2.png) [@markersocial](https://meta.discourse.org/u/markersocial)
#### Post date: [2021年九月9日 11:25 UTC](https://meta.discourse.org/t/s3-errors-uploading-profile-photos/203063/2 "2021-09-09T11:25:02Z")

</div>

您是否正在使用 Cloudflare？我认为个人资料图片确实存储在 S3 上，但通过您的服务器进行代理。我之前遇到过类似的问题（所有上传的头像都显示为空白），解决方法是在 Cloudflare 中将服务器 IP 地址加入白名单。

---

<div class="post-metadata">

### Author: ![bigfudge](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@bigfudge](https://meta.discourse.org/u/bigfudge)
#### Post date: [2021年九月9日 11:28 UTC](https://meta.discourse.org/t/s3-errors-uploading-profile-photos/203063/3 "2021-09-09T11:28:13Z")

</div>

谢谢——不过不用，我们没有使用 CDN，只是使用普通的 S3，但启用了安全媒体。我不确定如何检查请求是否被代理，但请见下方 curl 头部输出：

这在我看来表明请求未被代理，但我不够了解，无法完全确定……

```plaintext
curl -I https://example.com/user_avatar/discourse.psy.plymouth.ac.uk/jackie/90/29_2.png
HTTP/2 200 
server: nginx
date: Thu, 09 Sep 2021 11:26:33 GMT
content-type: image/jpeg
content-length: 4068
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
x-download-options: noopen
x-permitted-cross-domain-policies: none
referrer-policy: strict-origin-when-cross-origin
x-discourse-route: user_avatars/show
last-modified: Thu, 09 Sep 2021 09:38:05 GMT
content-transfer-encoding: binary
cache-control: max-age=31556952, public, immutable

```

---

<div class="post-metadata">

### Author: ![markersocial](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/markersocial/32/170136_2.png) [@markersocial](https://meta.discourse.org/u/markersocial)
#### Post date: [2021年九月9日 11:51 UTC](https://meta.discourse.org/t/s3-errors-uploading-profile-photos/203063/4 "2021-09-09T11:51:38Z")

</div>

该 URL 结构符合代理头像的特征，这有些令人困惑（Meta 上头像 URL 的实现方式与标准 Discourse 实例不同）。仅凭 curl 输出，我无法确定其中是否有信息能排除 Discourse 代理头像的可能性（我不是这方面的专家）。

根据我以往的经验，如果帖子图片是上传到 S3 的，那么头像通常也会上传到 S3，除非进行了非常定制化的配置。我此前未曾尝试过安全媒体上传或使用不带 CDN 的 S3，因此这些因素或许会带来显著差异。

建议尝试上传一个新头像，并确认能否在本地存储或 S3 存储桶中找到该文件。

我的猜测是，问题可能在于需要将服务器 IP 地址添加到 S3 的白名单中。不过我并非专家，希望有其他朋友能补充意见。

---

<div class="post-metadata">

### Author: ![bigfudge](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@bigfudge](https://meta.discourse.org/u/bigfudge)
#### Post date: [2021年九月9日 11:53 UTC](https://meta.discourse.org/t/s3-errors-uploading-profile-photos/203063/5 "2021-09-09T11:53:04Z")

</div>

问题是，S3 上传对帖子有效，但对头像图片上传无效。我推测，目前拥有头像的用户可能都是在设置 S3 之前上传过图片的，但我无法确认这一点。

---

<div class="post-metadata">

### Author: ![markersocial](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/markersocial/32/170136_2.png) [@markersocial](https://meta.discourse.org/u/markersocial)
#### Post date: [2021年九月9日 12:14 UTC](https://meta.discourse.org/t/s3-errors-uploading-profile-photos/203063/6 "2021-09-09T12:14:08Z")

</div>

这可能会有所帮助：[How are avatars stored and accessed? - #5 by DanielMarquard](https://meta.discourse.org/t/how-are-avatars-stored-and-accessed/53065/5?u=markersocial)

我曾遇到过类似问题：帖子图片正常显示，但个人资料图片无法显示。两者的上传都已成功保存到 S3，但在显示时出现了问题（服务器 IP 被限流或封锁）。

---

<div class="post-metadata">

### Author: ![bigfudge](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@bigfudge](https://meta.discourse.org/u/bigfudge)
#### Post date: [2021年九月10日 08:40 UTC](https://meta.discourse.org/t/s3-errors-uploading-profile-photos/203063/7 "2021-09-10T08:40:29Z")

</div>

可以确认，当我尝试上传头像时，进度条会更新并达到 100%，但随后才显示“错误：访问被拒绝”。

在 JavaScript 控制台中，我收到了以下错误：

```plaintext
https://discourse.psy.plymouth.ac.uk/uploads.json?client_id=0a2569993a6b43d6b5f8c60fdd2c913e
资源加载失败：服务器返回了状态码 422 ()

```

如果直接访问该 URL：

```plaintext
{"errors":["请求的 URL 或资源未找到。"],"error_type":"not_found"}

```

检查 S3 存储桶后发现，"Originals"文件夹中没有任何内容。

这与上传图片到帖子的情况形成对比：后者一切正常，图片确实出现在 S3 存储桶中，并且已生成缩略图并进行了优化。

---

<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: [2021年九月10日 09:31 UTC](https://meta.discourse.org/t/s3-errors-uploading-profile-photos/203063/8 "2021-09-10T09:31:11Z")

</div>

HTTP 状态码 422 显然代表“无法处理的实体”（Unprocessable Entity）。这表明 Discourse 出于某种原因不接受该上传，但也可能是其他问题——我认为 422 基本上意味着“请求格式正确，但请求中的某些内容有误”。

您尝试上传的图片是否是罕见的格式，或者体积巨大？

您是否尝试过将该确切的图片文件直接发布到帖子中？

默认情况下，头像会被缩放到多种尺寸，因此失败可能由此引起，例如如果图片过大，可能会触发内存限制。不过这主要是推测。

我不认为会从 S3 收到 422 状态码，因为在其 API 参考的错误响应列表中并未列出该代码。因此，我认为问题出在 Discourse 端，在尝试上传到 S3 之前就已经发生了。

---

<div class="post-metadata">

### Author: ![bigfudge](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@bigfudge](https://meta.discourse.org/u/bigfudge)
#### Post date: [2021年九月14日 21:03 UTC](https://meta.discourse.org/t/s3-errors-uploading-profile-photos/203063/9 "2021-09-14T21:03:21Z")

</div>

这肯定与头像图片的处理有关。我刚刚用同一张图片（这次使用的是 iOS 移动客户端）再次尝试，在帖子中上传没有问题，但将其用作头像时仍出现“访问被拒绝”的错误。

还有其他使用 S3 安全媒体的用户能复现此错误吗？考虑到帖子上传功能正常，这是否与存储桶权限设置有关？
