Class: AwsAdapter
- Inherits:
-
Object
show all
- Includes:
- System
- Defined in:
- lib/s3_adapters/aws_adapter.rb
Constant Summary
collapse
- DEFAULT_REGION =
'us-east-1'
Instance Method Summary
collapse
Methods included from System
clean, db_credentials, hostname, prompt, run, tarzip_folders, unzip_file
Constructor Details
#initialize(config) ⇒ AwsAdapter
Returns a new instance of AwsAdapter.
8
9
10
11
12
|
# File 'lib/s3_adapters/aws_adapter.rb', line 8
def initialize(config)
@config = config
@connected = false
@bucket = nil
end
|
Instance Method Details
#delete(file_name) ⇒ Object
47
48
49
50
|
# File 'lib/s3_adapters/aws_adapter.rb', line 47
def delete(file_name)
ensure_connected
@bucket.objects[file_name].delete
end
|
#ensure_connected ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/s3_adapters/aws_adapter.rb', line 14
def ensure_connected
return if @connected
client = AWS::S3.new(:access_key_id => @config[:access_key_id],
:secret_access_key => @config[:secret_access_key])
begin
create_bucket(client)
@connected = true
rescue Exception => e
puts "Unable to create bucket -- #{e}"
@connected = false
end
end
|
#fetch(file_name) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/s3_adapters/aws_adapter.rb', line 34
def fetch(file_name)
ensure_connected
file = Tempfile.new("temp")
bucket_object = @bucket.objects[file_name]
File.open(file.path, 'wb') do |f|
bucket_object.read do |chunk|
f.write chunk
end
end
return file
end
|
#store(file_name, file) ⇒ Object
28
29
30
31
32
|
# File 'lib/s3_adapters/aws_adapter.rb', line 28
def store(file_name, file)
ensure_connected
bucket_object = @bucket.objects[file_name]
bucket_object.write(Pathname.new(file.path))
end
|