Class: Jackfs::S3Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/jackfs/adapters/s3_adapter.rb

Constant Summary collapse

TEMP_PATH =
File.join('tmp','fs_cache')

Instance Attribute Summary collapse

Instance Method Summary collapse

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_keyObject

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_envObject

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_rootObject

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

#bucketObject

Returns the value of attribute bucket.



14
15
16
# File 'lib/jackfs/adapters/s3_adapter.rb', line 14

def bucket
  @bucket
end

#secret_keyObject

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_fileObject



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_connectionObject



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_bucketObject



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_pathObject



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