Class: Backup::Connection::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/backup/connection/s3.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter = false) ⇒ S3

Initializes the S3 connection, setting the values using the S3 adapter



10
11
12
13
14
15
16
17
18
# File 'lib/backup/connection/s3.rb', line 10

def initialize(adapter = false)
  if adapter
    self.adapter            = adapter
    self.procedure          = adapter.procedure
    self.final_file         = adapter.final_file
    self.tmp_path           = adapter.tmp_path.gsub('\ ', ' ')
    load_storage_configuration_attributes
  end
end

Instance Attribute Details

#access_key_idObject

Returns the value of attribute access_key_id.



7
8
9
# File 'lib/backup/connection/s3.rb', line 7

def access_key_id
  @access_key_id
end

#adapterObject

Returns the value of attribute adapter.



7
8
9
# File 'lib/backup/connection/s3.rb', line 7

def adapter
  @adapter
end

#final_fileObject

Returns the value of attribute final_file.



7
8
9
# File 'lib/backup/connection/s3.rb', line 7

def final_file
  @final_file
end

#procedureObject

Returns the value of attribute procedure.



7
8
9
# File 'lib/backup/connection/s3.rb', line 7

def procedure
  @procedure
end

#s3_bucketObject

Returns the value of attribute s3_bucket.



7
8
9
# File 'lib/backup/connection/s3.rb', line 7

def s3_bucket
  @s3_bucket
end

#secret_access_keyObject

Returns the value of attribute secret_access_key.



7
8
9
# File 'lib/backup/connection/s3.rb', line 7

def secret_access_key
  @secret_access_key
end

#tmp_pathObject

Returns the value of attribute tmp_path.



7
8
9
# File 'lib/backup/connection/s3.rb', line 7

def tmp_path
  @tmp_path
end

#use_sslObject

Returns the value of attribute use_ssl.



7
8
9
# File 'lib/backup/connection/s3.rb', line 7

def use_ssl
  @use_ssl
end

Instance Method Details

#bucketObject

Wrapper for the Bucket object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/backup/connection/s3.rb', line 39

def bucket
  begin
    # Find existing bucket:
    bucket = service.buckets.find(s3_bucket)
  rescue ::S3::Error::NoSuchBucket => e
    # Apparently the bucket doesn't exist yet, so create a new one:
    bucket = service.buckets.build(s3_bucket)
    bucket.save
  end
  bucket.retrieve
end

#connectObject

Establishes a connection with Amazon S3 using the credentials provided by the user



27
28
29
# File 'lib/backup/connection/s3.rb', line 27

def connect
  service
end

#destroy(file, bucket_as_string) ⇒ Object

Destroys file from a bucket on Amazon S3



60
61
62
63
# File 'lib/backup/connection/s3.rb', line 60

def destroy(file, bucket_as_string)
  object = bucket.objects.find(file)
  object.destroy
end

#serviceObject

Wrapper for the Service object



32
33
34
35
36
# File 'lib/backup/connection/s3.rb', line 32

def service
  ::S3::Service.new(:access_key_id => access_key_id,
                    :secret_access_key => secret_access_key,
                    :use_ssl => use_ssl)
end

#static_initialize(procedure) ⇒ Object

Sets values from a procedure, rather than from the adapter object



21
22
23
24
# File 'lib/backup/connection/s3.rb', line 21

def static_initialize(procedure)
  self.procedure = procedure
  load_storage_configuration_attributes(true)
end

#storeObject

Initializes the file transfer to Amazon S3 This can only run after a connection has been made using the #connect method



53
54
55
56
57
# File 'lib/backup/connection/s3.rb', line 53

def store
  object = bucket.objects.build(final_file)
  object.content = open(File.join(tmp_path, final_file))
  object.save
end