带有Cloudfront的配置回形针 [英] Configuration Paperclip with cloudfront

查看:95
本文介绍了带有Cloudfront的配置回形针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Cloudfront配置回形针,而我的URL并没有返回正确的路径或域。

I am trying to configure paperclip with cloudfront and my urls aren't coming back with the right path or domain.

这是我的回形针配置:

s3_cr

edentials: {
      access_key_id: ENV.fetch("S3_ACCESS_KEY"),
      secret_access_key: ENV.fetch("S3_SECRET"),
      bucket: ENV.fetch("S3_BUCKET"),
    },

    storage:        :s3,
    url:            ':s3_alias_url',
    s3_host_alias:  "xxxx.cloudfront.com",
    s3_headers:     { "Cache-Control" => "max-age=31557600" },
    s3_protocol:    "https",
    path:          ":rails_root/public/spree/products/:id/:style/:basename.:extension",
    default_url:   "/spree/products/:id/:style/:basename.:extension",
    default_style: "product",

我所有的URL都使用默认的url ...对吗?

All my urls come back using the default url... is this right?

在模型本身中,我有我担心的某些配置可能会覆盖:s3_alias_url

In the model itself, I have some configs that I worry might be overriding the :s3_alias_url. Is that possible?

class Image < Asset
    validate :no_attachment_errors

    has_attached_file :attachment,
                      styles: { mini: '48x48>', small: '100x100>', product: '240x240>', large: '600x600>' },
                      default_style: :product,
                      url: '/spree/products/:id/:style/:basename.:extension',
                      path: ':rails_root/public/spree/products/:id/:style/:basename.:extension'

这是什么

我的主要问题是将图像保存到模型时会发生什么?然后确定网址吗?还是在我们尝试获取附件时确定了它们?我假设当我们将附件与图片相关联时,它会上传到s3吗?但是,当我们尝试获取附件时,我们首先尝试访问Cloudfront CDN?发生了什么事?

My main questions are what happens when we save an image to the model? Are the urls determined then? Or are they determined when we try to fetch the attachment? I assume when we associate an attachment to image, it gets uploaded to s3? But when we try to fetch the attachment, we first try to hit the cloudfront cdn? What is going on?

推荐答案

我看到了一些不涉及Cloudfront的问题。我本人并没有在Cloudfront上使用太多,但是我使用CName为S3对象提供服务,这个概念似乎很相似。

There are a few problems I see that don't involve cloudfront. I haven't used cloudfront much myself, but I have served S3 objects using a CName, and the concept seems to be similar.

在配置文件中,您有

url:            ':s3_alias_url',
s3_host_alias:  "xxxx.cloudfront.com",

这是正确的回形针设置用于AWS。这里发生的是,您的网址正在寻找s3_host_alias作为显示图像时开始路径的位置。

Which is correct in terms of paperclip's set up for use with AWS. What's happening here is that your url is looking for your s3_host_alias as the location to start your path, when you are displaying images.

但是,在上传时,您将


我所有的网址都使用默认网址返回...对吗?

All my urls come back using the default url... is this right?

是。有点。

这不是您想要做的,而是您正在做的。在您的图片模型中,您将 url:设置为 /spree/products/:id/:style/:basename.:extension
,这也正是配置文件中 default_url:中的内容。

It's not what you want to do, but it is what you are doing. In your Image model, you are setting your url: to /spree/products/:id/:style/:basename.:extension and that is also exactly what you have in your default_url: in the config file.

因此,很难说出为什么收到该回复。在大多数情况下,您希望 default_url:是一些没有附加图像的图像。有点像404页面,但用于图片。即使s3发生故障,它也应该位于您知道可以击中的静态路径上(根据我的经验,这仅发生过一次)。

So that makes it hard to tell why you're getting that response. In most cases, you want the default_url: to be some image that shows up if there's no image attached. Sort of like a 404 page, but for images. This should be located at a static path that you know you can hit, even if s3 goes down (which has only ever happened once, in my experience).

如果您更改了此设置,我可以添加更多信息来解决您的特定问题,请告诉我您是否仍在使用默认路径或url。

If you change this, I can add more information to deal with your specific problem, just let me know if you are still hitting the default path, or the url.

(注意:此图像仅在没有附件的情况下显示。在附加图像时,它不会/不应显示,但在您所在的位置不存在例如,如果您从s3中删除在其检查路径中找到的图像。)

(Note: This image only shows up if you have no attachment. It will not / should not show up when you attach an image but it doesn't exist where you are looking. For example, if you delete an image from s3 that was found at the path it's checking.)


我担心可能会覆盖:s3_alias_url。可能吗?

I worry might be overriding the :s3_alias_url. Is that possible?

是的。这就是模型中URL发生的情况,它告诉它检查与默认路径相同的路径。

Yes. That is what is happening with your url in the model, it is telling it to check the same path as the default path.

我还要从您的路径中删除:rails_root 。这将有所帮助,主要是因为rails_root试图进入计算机上的某个位置,并告诉您的s3路径来构建该位置的路径。这可能会变得混乱(例如,一个s3键可能会出现并看起来像/ C:/ MyDocuments / Sites / MySite / public / spree / products // original /...。
它变得很糟糕。)

Also I would remove :rails_root from you're path. It will help, mostly because rails_root is trying to go to a location on your computer, and telling your s3 path to build a path of that location. This can get messy (for example, an s3 key might then show up and look like /C:/MyDocuments/Sites/MySite/public/spree/products//original/.... It gets gross.)


这实际上在做什么?

What is this actually doing?

我也不明白这是指哪一部分,但是如果您告诉我这是指什么,我会很乐意回答。

I do not understand which part this is referring too, but will gladly answer if you tell me what "this" is referring to.


我的主要问题是,当我们将图像保存到模型中时会发生什么?

My main questions are what happens when we save an image to the model?

保存图像时,您需要为s3对象,然后将图像上传到s3存储桶。该键是由您的存储桶和路径组成的,应注意,URL是专门用于检索图像的。上传文件后,URL的更改将更改回形针在寻找图像的位置。

When you save an image, you set a key for an s3 object, and upload an image to your s3 bucket. that key is made up of your bucket and path, and it should be noted that url is specifically used for retrieval of your images. Changes to the URL after a file has been uploaded will change where paperclip is looking for your image.


然后确定网址吗?还是在我们尝试获取附件时确定了它们?

Are the urls determined then? Or are they determined when we try to fetch the attachment?

当您上传图片时创建了网址,但是当您上传图片时引用图像时,您应该能够说出类似 img_tag src = image.attachment 之类的内容,并且应该创建一个供您查看的URL。
(这很难在不知道您的观点的情况下进行解释)。

The Url's are built when you upload an image, however, when you are referencing an image, you should be able to say something like img_tag src="image.attachment" and it should create a url for you to look at. (This is hard to explain without knowing what you have in your view).


我假设我们将附件与图片,将其上传到s3?

但是,当我们尝试获取附件时,我们首先尝试访问
cloudfront cdn吗?发生了什么事?

I assume when we associate an attachment to image, it gets uploaded to s3? Yes But when we try to fetch the attachment, we first try to hit the cloudfront cdn? What is going on?

在上载时使用您的存储桶:在凭据中找到,在下载(或显示)时,它使用从 url: path:

On upload uses your bucket: found in credentials, on download (or display) it uses the url built from url: and path:

这篇关于带有Cloudfront的配置回形针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆