简体   繁体   English

使用S3,Rails和Paperclip时隐藏亚马逊网址

[英]Hiding amazon urls when using S3, Rails and Paperclip

I have just set up file uploads to Amazon S3 using Rails 3 and Paperclip. 我刚刚使用Rails 3和Paperclip设置了文件上传到Amazon S3。 All this works amazingly well and is up and running. 所有这一切都非常好,并且正常运行。 There is just one small detail that I would like to sort out. 我想整理一个小细节。 At the moment, the urls are the amazon urls (ie start http://s3.amazonaws.com ) and I would like them to begin with my domain. 目前,网址是亚马逊网址(即启动http://s3.amazonaws.com ),我希望他们从我的域名开始。

I have already added the neccesary CNAME records to my DNS and they are working fine so I can access the files via a subdomain of my domain. 我已经将必要的CNAME记录添加到我的DNS中,并且它们正常工作,因此我可以通过我的域的子域访问这些文件。 The problem is just that the URLs generated by paperclip start with the amazon domain. 问题在于回形针生成的URL始于亚马逊域。 Is there an easy way to change the paperclip config to get round this? 有没有一种简单的方法来更改回形针配置以绕过这个?

Cheers 干杯

看一下Paperclip::Storage::S3

Here's everything you need to hide the Amazon urls of your S3 assets: 以下是隐藏S3资产的Amazon网址所需的一切:

  1. Name your S3 bucket after the domain alias you want to use. 在要使用的域别名后命名您的S3存储桶。 So if you want to access your assets at http://assets.mysite.com/path/to/image.png then you should name your S3 bucket: assets.mysite.com 因此,如果您想访问http://assets.mysite.com/path/to/image.png的资产,那么您应该命名您的S3存储桶: assets.mysite.com

  2. Add a CNAME to your DNS records so that assets.mysite.com is an alias of assets.mysite.com.s3.amazonaws.com (Don't include '.mysite.com' in 'name' field of the DNS record.) 添加CNAME到你的DNS记录,以便assets.mysite.com是一个别名assets.mysite.com.s3.amazonaws.com (不包括“.mysite.com” DNS记录中的‘名称’字段。 )

  3. Setup paperclip to use your new domain alias insetad of the default S3 path: 设置回形针以使用默认S3路径的新域别名insetad:

      has_attached_file :my_file, ... :url => ':s3_alias_url' :s3_host_alias => 'assets.mysite.com', ... 

I usually have different buckets for development, staging and production and I only use the domain alias for the prod bucket. 我通常有不同的桶用于开发,登台和生产,我只使用prod桶的域别名。 So to ensure it works in each environment, my :url setting often like this: 所以为了确保它在每个环境中都有效,我的:url设置通常是这样的:

:url => (ENV['USE_S3_ALIAS'] == 'TRUE' ? ':s3_alias_url' : ':s3_domain_url')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM