简体   繁体   English

带有专用CDN的Rails +回形针+ Rackspace CloudFiles

[英]Rails + Paperclips + Rackspace CloudFiles with the Private CDN

I have a Rails application that uses Paperclip to handle uploaded files and we are currently hosted by Rackspace. 我有一个使用Paperclip处理上传文件的Rails应用程序,我们目前由Rackspace托管。

The application is currently hosted on a single server and I am building out a more scalable solution with load balancers, application servers, and a separate database server. 该应用程序当前托管在单个服务器上,我正在使用负载平衡器,应用程序服务器和单独的数据库服务器构建一个更具可扩展性的解决方案。 The last thing I need to do is a solution for the uploaded assets. 我需要做的最后一件事是为上载的资产提供解决方案。 I have tried to use Rackspace's CloudFiles, but it seems the only way to use paperclip and CloudFiles is to have them on the public CDN, which I can't use, a user needs to be authenticate to access the files. 我曾尝试使用Rackspace的CloudFiles,但似乎使用回形针和CloudFiles的唯一方法是将它们放在公共CDN上(我无法使用),用户需要通过身份验证才能访问文件。 Before I turn to Amazon S3, since they have the option for temporary URLs, does know how to use CloudFiles with Paperclip and require authentication to access the files? 在我转向Amazon S3之前,由于它们具有临时URL选项,是否知道如何将CloudFiles与Paperclip结合使用并需要进行身份验证才能访问文件?

Any help, tips, google searches, links, or solutions would be greatly appreciated. 任何帮助,提示,谷歌搜索,链接或解决方案将不胜感激。

As it happens, Cloud Files also supports the generation of temporary URLs, and it appears that Paperclip does allow you to make use of it . 碰巧的是,Cloud Files 还支持临时URL的生成,并且似乎Paperclip确实允许您使用它 Just generate the URL from your Attachment with #expiring_url instead of #url in your views: 只需在您的视图中使用#expiring_url而不是#urlAttachment生成URL:

= image_tag @organization.logo.expiring_url(Time.now.to_i + 100, :original).gsub(/^http:/, "https")

Paperclip will only generate http urls , but since Rackspace's temporary URLs don't use the scheme in their checksums, you can use a gsub call to turn it into an https URL. 回形针只会生成http url ,但是由于Rackspace的临时URL在其校验和中不使用该方案,因此您可以使用gsub调用将其转换为https URL。 Also, notice that the first argument to #expiring_url is an absolute timestamp (in seconds-since-the-epoch). 另外,请注意, #expiring_url的第一个参数是绝对时间戳(以秒为单位)。

Expiring URLs for Rackspace only made it into fog somewhat recently -- v1.18.0 -- so if you're using an older version, you may need to upgrade fog to take advantage of them: Rackspace的URL过期只是在最近才使它变得模糊-v1.18.0-因此,如果您使用的是旧版本,则可能需要升级fog才能利用它们:

bundle upgrade fog

Paperclip also supports generating obfuscated URLs , which looks interesting, but would be less secure, since the server wouldn't expire it. Paperclip还支持生成混淆的URL ,这看起来很有趣,但是安全性较低,因为服务器不会使其失效。

You can add the key like this: 您可以像这样添加密钥:

class Rackspace

  def self.add_temp_url_key
    require 'fog'

    puts "Creating Storage Service"

    begin
      service = Fog::Storage.new(
        :provider => 'rackspace',
        :rackspace_username => ENV['FOG_USERNAME'],
        :rackspace_api_key => ENV['FOG_API_KEY'],
        :rackspace_region => ENV['RACKSPACE_REGION'].to_sym
      )

      service.post_set_meta_temp_url_key(ENV['RACKSPACE_TEMP_URL_KEY'])
      puts "X-Account-Meta-Temp-Url-Key successfully set to #{ENV['RACKSPACE_TEMP_URL_KEY']}"

    rescue => e
      puts "Unable to set X-Account-Meta-Temp-Url-Key - #{e.inspect}"
      puts e.backtrace
    end
  end

end

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

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