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

时间:2021-07-16 21:07:36

I have just set up file uploads to Amazon S3 using Rails 3 and Paperclip. 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.

我刚刚使用Rails 3和Paperclip设置了文件上传到Amazon S3。所有这些都非常好,并且正常运行。我想整理一个小细节。目前,网址是亚马逊网址(即启动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. The problem is just that the URLs generated by paperclip start with the amazon domain. Is there an easy way to change the paperclip config to get round this?

我已经将必要的CNAME记录添加到我的DNS中,并且它们正常工作,因此我可以通过我的域的子域访问这些文件。问题在于回形针生成的URL始于亚马逊域。有没有一种简单的方法来更改回形针配置以绕过这个?

Cheers

2 个解决方案

#1


5  

Take a look at Paperclip::Storage::S3.

看一下Paperclip :: Storage :: S3。

#2


2  

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

    在要使用的域别名后命名您的S3存储桶。因此,如果您想访问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.)

    在您的DNS记录中添加CNAME,以便assets.mysite.com是assets.mysite.com.s3.amazonaws.com的别名(不要在DNS记录的“名称”字段中包含“.mysite.com”。 )

  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. So to ensure it works in each environment, my :url setting often like this:

我通常有不同的桶用于开发,登台和生产,我只使用prod桶的域别名。所以为了确保它在每个环境中都有效,我的:url设置通常是这样的:

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

#1


5  

Take a look at Paperclip::Storage::S3.

看一下Paperclip :: Storage :: S3。

#2


2  

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

    在要使用的域别名后命名您的S3存储桶。因此,如果您想访问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.)

    在您的DNS记录中添加CNAME,以便assets.mysite.com是assets.mysite.com.s3.amazonaws.com的别名(不要在DNS记录的“名称”字段中包含“.mysite.com”。 )

  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. So to ensure it works in each environment, my :url setting often like this:

我通常有不同的桶用于开发,登台和生产,我只使用prod桶的域别名。所以为了确保它在每个环境中都有效,我的:url设置通常是这样的:

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