Shrine - 使用 Cloudfront 的派生端点和完整路径 URL
Shrine - Derivation Endpoint and full path URLs with Cloudfront
我使用带有 Rails 的 Shrine 将我的图像直接上传到 S3,然后使用 Cloudfront 提供它们。
我的设置运行良好,但我无法回答 2 个问题:
1) 用户通过 Uppy 上传了一张新图片。他们单击 "Create Image Page",这会创建一个新的 "page" 对象。用户被带到一个空白页面,而图像派生正在使用 Sidekiq 在后台进程中创建。一旦成功创建衍生品并将其提升到 /store,我们如何通过 JS 获得图像 "pop-in"?有没有我们可以接受的回调,或者是否有可能继续尝试通过 JS 找到导数直到它存在?
2) 使用 Derivatives Endpoint 插件,所有图像 URL 都是相对的。有没有办法将其作为来自 Cloudfront/Custom 域的绝对 URLs 提供?这是一个使用 Rails:
的例子
Ruby代码:
<%= image_tag(image.image_file.derivation_url(:banner, 800, 300)) %>
结果HTML:
<img src="/derivations/images/banner/800/300/...">
我怎样才能代替结果 URL:
<img src="http://abc123.cloudfront.net/derivations/images/banner/800/300/...">
这是故意防止 DoS 攻击吗?谢谢。
将主机选项添加到 "derivation_endpoint" 导致返回以下错误 XML:
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>...</RequestId>
<HostId>...</HostId>
</Error>
这可能是我的 Cloudfront 访问设置吗?也许是 CORS。
这是我在 image_uploader.rb
中的 derivation_endpoint 设置
plugin :derivation_endpoint, host: "http://abc123.cloudfront.net", prefix: "derivations", secret_key: ..., upload: true
routes.rb
mount Shrine.presign_endpoint(:cache) => "s3/params"
mount PhotoUploader.derivation_endpoint => "derivations/images"
config/initializers/Shrine.rb
Shrine.plugin :url_options, store: { host: "http://abc123.cloudfront.net" }
由于后台作业将使用衍生品更新记录,最简单的方法是在客户端轮询记录的 "show" 路由(即 GET /images/:id
)。在这种情况下,路由可以通过 respond_to
.
获得替代 JSON 响应
或者,您可以在处理完成后通过 WebSockets 或类似 message_bus.
从后台作业向客户端发送消息
您可以为 derivation_endpoint 插件配置 :host
选项:
Shrine.plugin :derivation_endpoint, host: "https://cloudfront.net"
我使用带有 Rails 的 Shrine 将我的图像直接上传到 S3,然后使用 Cloudfront 提供它们。
我的设置运行良好,但我无法回答 2 个问题:
1) 用户通过 Uppy 上传了一张新图片。他们单击 "Create Image Page",这会创建一个新的 "page" 对象。用户被带到一个空白页面,而图像派生正在使用 Sidekiq 在后台进程中创建。一旦成功创建衍生品并将其提升到 /store,我们如何通过 JS 获得图像 "pop-in"?有没有我们可以接受的回调,或者是否有可能继续尝试通过 JS 找到导数直到它存在?
2) 使用 Derivatives Endpoint 插件,所有图像 URL 都是相对的。有没有办法将其作为来自 Cloudfront/Custom 域的绝对 URLs 提供?这是一个使用 Rails:
的例子Ruby代码:
<%= image_tag(image.image_file.derivation_url(:banner, 800, 300)) %>
结果HTML:
<img src="/derivations/images/banner/800/300/...">
我怎样才能代替结果 URL:
<img src="http://abc123.cloudfront.net/derivations/images/banner/800/300/...">
这是故意防止 DoS 攻击吗?谢谢。
将主机选项添加到 "derivation_endpoint" 导致返回以下错误 XML:
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>...</RequestId>
<HostId>...</HostId>
</Error>
这可能是我的 Cloudfront 访问设置吗?也许是 CORS。
这是我在 image_uploader.rb
中的 derivation_endpoint 设置plugin :derivation_endpoint, host: "http://abc123.cloudfront.net", prefix: "derivations", secret_key: ..., upload: true
routes.rb
mount Shrine.presign_endpoint(:cache) => "s3/params"
mount PhotoUploader.derivation_endpoint => "derivations/images"
config/initializers/Shrine.rb
Shrine.plugin :url_options, store: { host: "http://abc123.cloudfront.net" }
由于后台作业将使用衍生品更新记录,最简单的方法是在客户端轮询记录的 "show" 路由(即
获得替代 JSON 响应GET /images/:id
)。在这种情况下,路由可以通过respond_to
.或者,您可以在处理完成后通过 WebSockets 或类似 message_bus.
从后台作业向客户端发送消息
您可以为 derivation_endpoint 插件配置
:host
选项:Shrine.plugin :derivation_endpoint, host: "https://cloudfront.net"