Module: Paperclip::Storage::Cloud_files
- Defined in:
- lib/paperclip/storage/cloud_files.rb
Overview
Rackspace’s Cloud Files service is a scalable, easy place to store files for distribution, and is integrated into the Limelight CDN. You can find out more about it at www.rackspacecloud.com/cloud_hosting_products/files
To install the Cloud Files gem, add the Gemcutter gem source (“gem sources -a gemcutter.org”), then do a “gem install cloudfiles”. For more information, see the github repository at github.com/rackspace/ruby-cloudfiles/
There are a few Cloud Files-specific options for has_attached_file:
-
cloudfiles_credentials
: Takes a path, a File, or a Hash. The path (or File) must point to a YAML file containing theusername
andapi_key
that Rackspace gives you. Rackspace customers using the cloudfiles gem >= 1.4.1 can also set a servicenet variable to true to send traffic over the unbilled internal Rackspace service network. You can ‘environment-space’ this just like you do to your database.yml file, so different environments can use different accounts:development: username: hayley api_key: a7f... test: username: katherine api_key: 7fa... production: username: minter api_key: 87k... servicenet: true auth_url: https://lon.auth.api.rackspacecloud.com/v1.0
This is not required, however, and the file may simply look like this:
username: minter... api_key: 11q...
In which case, those access keys will be used in all environments. You can also put your container name in this file, instead of adding it to the code directly. This is useful when you want the same account but a different container for development versus production.
-
container
: This is the name of the Cloud Files container that will store your files. This container should be marked “public” so that the files are available to the world at large. If the container does not exist, it will be created and marked public. -
path
: This is the path under the container in which the file will be stored. The CDN URL will be constructed from the CDN identifier for the container and the path. This is what you will want to interpolate. Keys should be unique, like filenames, and despite the fact that Cloud Files (strictly speaking) does not support directories, you can still use a / to separate parts of your file name, and they will show up in the URL structure. -
auth_url
: The URL to the authentication endpoint. If blank, defaults to the Rackspace Cloud Files USA endpoint. You can use this to specify things like the Rackspace Cloud Files UK infrastructure, or a non-Rackspace OpenStack Swift installation. Requires 1.4.11 or higher of the Cloud Files gem.
Class Method Summary collapse
Instance Method Summary collapse
- #cloudfiles ⇒ Object
- #cloudfiles_container ⇒ Object
- #container_name ⇒ Object
- #create_container ⇒ Object
- #exists?(style = default_style) ⇒ Boolean
-
#flush_deletes ⇒ Object
:nodoc:.
-
#flush_writes ⇒ Object
:nodoc:.
- #parse_credentials(creds) ⇒ Object
- #read ⇒ Object
-
#to_file(style = default_style) ⇒ Object
(also: #to_io)
Returns representation of the data of the file assigned to the given style, in the format most representative of the current storage.
Class Method Details
.extended(base) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/paperclip/storage/cloud_files.rb', line 47 def self.extended base require 'cloudfiles' @@container ||= {} base.instance_eval do @cloudfiles_credentials = parse_credentials(@options[:cloudfiles_credentials]) @container_name = @options[:container] || [:container_name] || @cloudfiles_credentials[:container] || @cloudfiles_credentials[:container_name] @container_name = @container_name.call(self) if @container_name.is_a?(Proc) @cloudfiles_options = @options[:cloudfiles_options] || {} @@cdn_url = cloudfiles_container.cdn_url @path_filename = ":cf_path_filename" unless @url.to_s.match(/^:cf.*filename$/) @url = @@cdn_url + "/#{URI.encode(@path_filename).gsub(/&/,'%26')}" @path = (Paperclip::Attachment.[:path] == @options[:path]) ? ":attachment/:id/:style/:basename.:extension" : @options[:path] end Paperclip.interpolates(:cf_path_filename) do |, style| .path(style) end end |
Instance Method Details
#cloudfiles ⇒ Object
65 66 67 68 69 70 |
# File 'lib/paperclip/storage/cloud_files.rb', line 65 def cloudfiles @@cf ||= CloudFiles::Connection.new(:username => @cloudfiles_credentials[:username], :api_key => @cloudfiles_credentials[:api_key], :snet => @cloudfiles_credentials[:servicenet], :auth_url => (@cloudfiles_credentials[:auth_url] || "https://auth.api.rackspacecloud.com/v1.0")) end |
#cloudfiles_container ⇒ Object
78 79 80 |
# File 'lib/paperclip/storage/cloud_files.rb', line 78 def cloudfiles_container @@container[@container_name] ||= create_container end |
#container_name ⇒ Object
82 83 84 |
# File 'lib/paperclip/storage/cloud_files.rb', line 82 def container_name @container_name end |
#create_container ⇒ Object
72 73 74 75 76 |
# File 'lib/paperclip/storage/cloud_files.rb', line 72 def create_container container = cloudfiles.create_container(@container_name) container.make_public container end |
#exists?(style = default_style) ⇒ Boolean
91 92 93 |
# File 'lib/paperclip/storage/cloud_files.rb', line 91 def exists?(style = default_style) cloudfiles_container.object_exists?(path(style)) end |
#flush_deletes ⇒ Object
:nodoc:
114 115 116 117 118 119 |
# File 'lib/paperclip/storage/cloud_files.rb', line 114 def flush_deletes #:nodoc: @queued_for_delete.each do |path| cloudfiles_container.delete_object(path) end @queued_for_delete = [] end |
#flush_writes ⇒ Object
:nodoc:
106 107 108 109 110 111 112 |
# File 'lib/paperclip/storage/cloud_files.rb', line 106 def flush_writes #:nodoc: @queued_for_write.each do |style, file| object = cloudfiles_container.create_object(path(style),false) object.write(file) end @queued_for_write = {} end |
#parse_credentials(creds) ⇒ Object
86 87 88 89 |
# File 'lib/paperclip/storage/cloud_files.rb', line 86 def parse_credentials creds creds = find_credentials(creds).stringify_keys (creds[Rails.env] || creds).symbolize_keys end |
#read ⇒ Object
95 96 97 |
# File 'lib/paperclip/storage/cloud_files.rb', line 95 def read self.data end |
#to_file(style = default_style) ⇒ Object Also known as: to_io
Returns representation of the data of the file assigned to the given style, in the format most representative of the current storage.
101 102 103 |
# File 'lib/paperclip/storage/cloud_files.rb', line 101 def to_file style = default_style @queued_for_write[style] || cloudfiles_container.create_object(path(style)) end |