Class: CarrierWave::Storage::Fog

Inherits:
Abstract
  • Object
show all
Defined in:
lib/carrierwave/storage/fog.rb

Overview

Stores things using the “fog” gem.

fog supports storing files with AWS, Google, Local and Rackspace

You need to setup some options to configure your usage:

:fog_credentials

host info and credentials for service

:fog_directory

specifies name of directory to store data in, assumed to already exist

:fog_attributes

(optional) additional attributes to set on files

:fog_public

(optional) public readability, defaults to true

:fog_authenticated_url_expiration

(optional) time (in seconds) that authenticated urls

will be valid, when fog_public is false and provider is AWS or Google, defaults to 600
:fog_use_ssl_for_aws

(optional) #public_url will use https for the AWS generated URL]

:fog_aws_accelerate

(optional) #public_url will use s3-accelerate subdomain

instead of s3, defaults to false
:fog_aws_fips

(optional) #public_url will use s3-fips subdomain

instead of s3, defaults to false

AWS credentials contain the following keys:

:aws_access_key_id
:aws_secret_access_key
:region

(optional) defaults to ‘us-east-1’

:region should be one of ['eu-west-1', 'us-east-1', 'ap-southeast-1', 'us-west-1', 'ap-northeast-1', 'eu-central-1']

Google credentials contain the following keys:

:google_storage_access_key_id
:google_storage_secret_access_key

Local credentials contain the following keys:

:local_root

local path to files

Rackspace credentials contain the following keys:

:rackspace_username
:rackspace_api_key

A full example with AWS credentials:

CarrierWave.configure do |config|
  config.fog_credentials = {
    :aws_access_key_id => 'xxxxxx',
    :aws_secret_access_key => 'yyyyyy',
    :provider => 'AWS'
  }
  config.fog_directory = 'directoryname'
  config.fog_public = true
end

Defined Under Namespace

Classes: File

Instance Attribute Summary

Attributes inherited from Abstract

#uploader

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

#identifier, #initialize

Constructor Details

This class inherits a constructor from CarrierWave::Storage::Abstract

Class Method Details

.connection_cacheObject



62
63
64
# File 'lib/carrierwave/storage/fog.rb', line 62

def connection_cache
  @connection_cache ||= {}
end

.eager_loadObject



66
67
68
69
70
71
72
# File 'lib/carrierwave/storage/fog.rb', line 66

def eager_load
  # see #1198. This will hopefully no longer be necessary in future release of fog
  fog_credentials = CarrierWave::Uploader::Base.fog_credentials
  if fog_credentials.present?
    CarrierWave::Storage::Fog.connection_cache[fog_credentials] ||= ::Fog::Storage.new(fog_credentials)
  end
end

Instance Method Details

#cache!(new_file) ⇒ Object

Stores given file to cache directory.

Parameters

new_file (File, IOString, Tempfile)

any kind of file object

Returns

CarrierWave::SanitizedFile

a sanitized file



118
119
120
121
122
# File 'lib/carrierwave/storage/fog.rb', line 118

def cache!(new_file)
  f = CarrierWave::Storage::Fog::File.new(uploader, self, uploader.cache_path)
  f.store(new_file)
  f
end

#clean_cache!(seconds) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/carrierwave/storage/fog.rb', line 146

def clean_cache!(seconds)
  connection.directories.new(
    :key    => uploader.fog_directory,
    :public => uploader.fog_public
  ).files.all(:prefix => uploader.cache_dir).each do |file|
    # generate_cache_id returns key formatted TIMEINT-PID(-COUNTER)-RND
    matched = file.key.match(/(\d+)-\d+-\d+(?:-\d+)?/)
    next unless matched
    time = Time.at(matched[1].to_i)
    file.destroy if time < (Time.now.utc - seconds)
  end
end

#connectionObject



159
160
161
162
163
164
# File 'lib/carrierwave/storage/fog.rb', line 159

def connection
  @connection ||= begin
    options = credentials = uploader.fog_credentials
    self.class.connection_cache[credentials] ||= ::Fog::Storage.new(options)
  end
end

#delete_dir!(path) ⇒ Object

Deletes a cache dir



142
143
144
# File 'lib/carrierwave/storage/fog.rb', line 142

def delete_dir!(path)
  # do nothing, because there's no such things as 'empty directory'
end

#retrieve!(identifier) ⇒ Object

Retrieve a file

Parameters

identifier (String)

unique identifier for file

Returns

CarrierWave::Storage::Fog::File

the stored file



103
104
105
# File 'lib/carrierwave/storage/fog.rb', line 103

def retrieve!(identifier)
  CarrierWave::Storage::Fog::File.new(uploader, self, uploader.store_path(identifier))
end

#retrieve_from_cache!(identifier) ⇒ Object

Retrieves the file with the given cache_name from the cache.

Parameters

cache_name (String)

uniquely identifies a cache file

Raises

CarrierWave::InvalidParameter

if the cache_name is incorrectly formatted.



135
136
137
# File 'lib/carrierwave/storage/fog.rb', line 135

def retrieve_from_cache!(identifier)
  CarrierWave::Storage::Fog::File.new(uploader, self, uploader.cache_path(identifier))
end

#store!(file) ⇒ Object

Store a file

Parameters

file (CarrierWave::SanitizedFile)

the file to store

Returns

CarrierWave::Storage::Fog::File

the stored file



86
87
88
89
90
# File 'lib/carrierwave/storage/fog.rb', line 86

def store!(file)
  f = CarrierWave::Storage::Fog::File.new(uploader, self, uploader.store_path)
  f.store(file)
  f
end