Class: DearS3::Client

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/dears3.rb,
lib/dears3/client.rb

Instance Method Summary collapse

Instance Method Details

#bucket_nameObject



31
32
33
# File 'lib/dears3/client.rb', line 31

def bucket_name
  bucket.name
end

#configure_website(index_doc, error_doc) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/dears3/client.rb', line 55

def configure_website index_doc, error_doc
  bucket.configure_website do |cfg|
    cfg.index_document_suffix = index_doc
    cfg.error_document_key = error_doc
  end

  bucket.policy = generate_policy
  # TODO: find more general pattern
  "http://#{ bucket_name }.s3-website-us-east-1.amazonaws.com/"
end

#files_in_bucketObject



73
74
75
# File 'lib/dears3/client.rb', line 73

def files_in_bucket
  @files_in_bucket ||= bucket.objects.map &:key
end

#generate_policyObject



66
67
68
69
70
71
# File 'lib/dears3/client.rb', line 66

def generate_policy
  policy = AWS::S3::Policy.new
  resources = "arn:aws:s3:::#{ bucket_name }/*"
  policy.allow(actions: [:get_object], resources: resources, principals: :any)
  policy
end

#new_bucket?(name) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/dears3/client.rb', line 27

def new_bucket? name
  !s3.buckets[name].exists?
end

#remove_websiteObject



77
78
79
80
# File 'lib/dears3/client.rb', line 77

def remove_website
  bucket.remove_website_configuration
  bucket.url
end

#set_bucket(name) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/dears3/client.rb', line 10

def set_bucket name
  self.bucket = s3.buckets[name]

  if new_bucket? name
    s3.buckets.create(bucket.name, acl: :bucket_owner_full_control)
  end

  bucket.name
end

#validate_bucket_name(name) ⇒ Object



20
21
22
23
24
25
# File 'lib/dears3/client.rb', line 20

def validate_bucket_name name
  return :Invalid if invalid_bucket_name? name
  return :Unavailable if unavailable_bucket_name? name

  nil
end

#walk_and_upload(path, status_proc = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dears3/client.rb', line 41

def walk_and_upload path, status_proc = nil
  entries = Dir.entries path
  entries.each do |entry|
    next if entry == File.basename(__FILE__) || entry[0] == '.' 
    nested_entry = (path == "." ? entry : "#{ path }/#{ entry }")
    if File.directory? nested_entry
      walk_and_upload nested_entry, status_proc
      next
    else
      upload nested_entry, status_proc
    end
  end
end

#with(s3_connection) ⇒ Object



35
36
37
38
39
# File 'lib/dears3/client.rb', line 35

def with s3_connection
  @bucket = nil
  @s3 = s3_connection
  self
end