Configuration Paperclip with cloudfront

点点圈 提交于 2019-12-13 07:26:37

问题


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

This is my paperclip config:

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",

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

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'

What is this actually doing?

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?


回答1:


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.

In your config file you have

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

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.

However, on upload, you are going to the bucket (in your credentials) and putting an object in path (so bucket_name\path\to\file.jpg).

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

Yes. Sort of.

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.

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).

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.

(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.)

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

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.

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?

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?

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).

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?

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



来源:https://stackoverflow.com/questions/44806203/configuration-paperclip-with-cloudfront

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!