ehay
ehay

Reputation: 21

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.

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

Any help, tips, google searches, links, or solutions would be greatly appreciated.

Upvotes: 1

Views: 644

Answers (2)

user3565039
user3565039

Reputation: 185

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

Upvotes: 0

Ash Wilson
Ash Wilson

Reputation: 24478

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. Just generate the URL from your Attachment with #expiring_url instead of #url in your views:

= 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. Also, notice that the first argument to #expiring_url is an absolute timestamp (in seconds-since-the-epoch).

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:

bundle upgrade fog

Paperclip also supports generating obfuscated URLs, which looks interesting, but would be less secure, since the server wouldn't expire it.

Upvotes: 1

Related Questions