Class: Jackfs::S3Adapter
- Inherits:
-
Object
- Object
- Jackfs::S3Adapter
- Defined in:
- lib/jackfs/adapters/s3_adapter.rb
Constant Summary collapse
- TEMP_PATH =
File.join('tmp','fs_cache')
Instance Attribute Summary collapse
-
#access_key ⇒ Object
Returns the value of attribute access_key.
-
#app_env ⇒ Object
Returns the value of attribute app_env.
-
#app_root ⇒ Object
Returns the value of attribute app_root.
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#secret_key ⇒ Object
Returns the value of attribute secret_key.
Instance Method Summary collapse
- #config_file ⇒ Object
- #establish_connection ⇒ Object
- #find_or_create_bucket ⇒ Object
- #full_temp_path ⇒ Object
- #get(name) ⇒ Object
-
#initialize(app_root, app_env) ⇒ S3Adapter
constructor
A new instance of S3Adapter.
- #store(f, name) ⇒ Object
Constructor Details
#initialize(app_root, app_env) ⇒ S3Adapter
Returns a new instance of S3Adapter.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/jackfs/adapters/s3_adapter.rb', line 16 def initialize(app_root, app_env) @app_root = app_root @app_env = app_env FileUtils.mkdir_p(full_temp_path) yml = YAML.load_file(config_file)[@app_env.to_s] @access_key = yml["access_key"] @secret_key = yml["secret_key"] @bucket = yml["bucket"] # Clean up temp files FileUtils.remove_file(File.join(full_temp_path,'/*'), true) end |
Instance Attribute Details
#access_key ⇒ Object
Returns the value of attribute access_key.
14 15 16 |
# File 'lib/jackfs/adapters/s3_adapter.rb', line 14 def access_key @access_key end |
#app_env ⇒ Object
Returns the value of attribute app_env.
14 15 16 |
# File 'lib/jackfs/adapters/s3_adapter.rb', line 14 def app_env @app_env end |
#app_root ⇒ Object
Returns the value of attribute app_root.
14 15 16 |
# File 'lib/jackfs/adapters/s3_adapter.rb', line 14 def app_root @app_root end |
#bucket ⇒ Object
Returns the value of attribute bucket.
14 15 16 |
# File 'lib/jackfs/adapters/s3_adapter.rb', line 14 def bucket @bucket end |
#secret_key ⇒ Object
Returns the value of attribute secret_key.
14 15 16 |
# File 'lib/jackfs/adapters/s3_adapter.rb', line 14 def secret_key @secret_key end |
Instance Method Details
#config_file ⇒ Object
67 68 69 |
# File 'lib/jackfs/adapters/s3_adapter.rb', line 67 def config_file File.join(@app_root, Jackfs::FileStore::CONFIG_FILE) end |
#establish_connection ⇒ Object
50 51 52 53 54 55 |
# File 'lib/jackfs/adapters/s3_adapter.rb', line 50 def establish_connection AWS::S3::Base.establish_connection!( :access_key_id => @access_key, :secret_access_key => @secret_key ) end |
#find_or_create_bucket ⇒ Object
57 58 59 60 61 |
# File 'lib/jackfs/adapters/s3_adapter.rb', line 57 def find_or_create_bucket establish_connection AWS::S3::Bucket.create(@bucket) unless bucket = AWS::S3::Bucket.find(@bucket) true end |
#full_temp_path ⇒ Object
63 64 65 |
# File 'lib/jackfs/adapters/s3_adapter.rb', line 63 def full_temp_path File.join(@app_root, TEMP_PATH) end |
#get(name) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/jackfs/adapters/s3_adapter.rb', line 38 def get(name) unique_name = UUIDTools::UUID.random_create.to_s # Write Body to generated tmp file open(File.join(full_temp_path, unique_name), 'wb') do |file| AWS::S3::S3Object.stream(name, @bucket) do |chunk| file.write chunk end end # Open and return Temp File open(File.join(full_temp_path,unique_name), 'rb') end |
#store(f, name) ⇒ Object
32 33 34 35 36 |
# File 'lib/jackfs/adapters/s3_adapter.rb', line 32 def store(f, name) find_or_create_bucket AWS::S3::S3Object.store(name, f, @bucket) name end |