bucket_list 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/bucket_list CHANGED
@@ -98,11 +98,11 @@ if options[:upload]
98
98
  if file.class.eql?(Array)
99
99
  file.each do |f|
100
100
  puts "[INFO] Uploading '#{f}' to the '#{bucket_name}' bucket"
101
- bucket_list.upload_file_to_bucket(bucket_name, f)
101
+ bucket_list.upload(bucket_name, f)
102
102
  end
103
103
  else
104
104
  puts "[INFO] Uploading '#{file}' to the '#{bucket_name}' bucket"
105
- bucket_list.upload_file_to_bucket(bucket_name, file)
105
+ bucket_list.upload(bucket_name, file)
106
106
  end
107
107
  end
108
108
  end
data/config/config.yml CHANGED
@@ -1,3 +1,3 @@
1
1
  # bucket_list config file
2
2
 
3
- version: 0.2.0
3
+ version: 0.3.0
@@ -1,9 +1,11 @@
1
1
  require 'aws-sdk'
2
2
  require 'erb'
3
3
  require 'yaml'
4
+ require 'aws_helper'
4
5
 
5
6
  class BucketList
6
7
  attr_accessor :s3, :settings
8
+ include AwsHelper
7
9
 
8
10
  def initialize
9
11
  if keys_available?
@@ -40,7 +42,7 @@ class BucketList
40
42
 
41
43
  def bucket_contains_object?(bucket_name, object_name)
42
44
  bucket_contents = Array.new
43
- bucket = s3.buckets[bucket_name]
45
+ bucket = @s3.buckets[bucket_name]
44
46
  bucket.objects['key']
45
47
  bucket.objects.each do |obj|
46
48
  bucket_contents << obj.key
@@ -49,12 +51,9 @@ class BucketList
49
51
  end
50
52
 
51
53
  def create_bucket(bucket_name)
52
- if !bucket_exists?(bucket_name, false)
54
+ make_request {
53
55
  @s3.buckets.create(bucket_name)
54
- puts "[INFO] Bucket created"
55
- else
56
- puts "[ERROR] Sorry, a bucket named '#{bucket_name}' already exists"
57
- end
56
+ }
58
57
  end
59
58
 
60
59
  def keys_available?
@@ -64,34 +63,17 @@ class BucketList
64
63
  end
65
64
 
66
65
  def delete_bucket(bucket_name)
67
- bucket = s3.buckets[bucket_name]
68
- bucket_deleted = false
69
- timeout = false
70
- attempts = 0
71
- max_attempts = 10
72
- until bucket_deleted || (attempts >= max_attempts)
73
- begin
74
- bucket.delete!
75
- bucket_deleted = true
76
- puts "[INFO] The bucket '#{bucket_name}' has been deleted"
77
- rescue AWS::S3::Errors::NoSuchBucket => error
78
- puts "[ERROR] #{error}"
79
- puts "[INFO] Trying again... (#{attempts.to_i + 1}/#{max_attempts} remaining)"
80
- attempts = attempts + 1
81
- sleep 1
82
- next
83
- rescue AWS::S3::Errors::AccessDenied => error
84
- puts "[ERROR] #{error}"
85
- exit 1
86
- end
87
- end
66
+ bucket = @s3.buckets[bucket_name]
67
+ make_request {
68
+ bucket.delete!
69
+ }
88
70
  end
89
71
 
90
72
  def get_bucket_contents(bucket_name)
91
73
  bucket_contents = Array.new
92
74
  if bucket_exists?(bucket_name)
93
75
  if get_number_of_bucket_contents(bucket_name) > 0
94
- bucket = s3.buckets[bucket_name]
76
+ bucket = @s3.buckets[bucket_name]
95
77
  bucket.objects['key']
96
78
  bucket.objects.each do |obj|
97
79
  bucket_contents << obj.key
@@ -112,7 +94,7 @@ class BucketList
112
94
 
113
95
  def get_number_of_bucket_contents(bucket_name)
114
96
  if bucket_exists?(bucket_name)
115
- bucket = s3.buckets[bucket_name]
97
+ bucket = @s3.buckets[bucket_name]
116
98
  bucket.objects.count
117
99
  else
118
100
  puts "[ERROR] Sorry, a bucket named '#{bucket_name}' does not exist"
@@ -127,22 +109,35 @@ class BucketList
127
109
  my_buckets
128
110
  end
129
111
 
130
- def upload_file_to_bucket(bucket_name, file)
131
- if bucket_exists?(bucket_name)
132
- bucket = s3.buckets[bucket_name]
133
-
134
- basename = File.basename(file)
135
- o = bucket.objects[basename]
136
- o.write(:file => file)
112
+ def upload(bucket_name, file)
113
+ bucket = @s3.buckets[bucket_name]
137
114
 
138
- until bucket_contains_object?(bucket_name, basename) do
139
- sleep 1
115
+ if File.directory?(file)
116
+ Dir["#{file}/*"].each do |file|
117
+ if !File.directory?(file) # No support for sub dirs (yet)
118
+ puts file
119
+ upload_file(bucket, file)
120
+ end
140
121
  end
141
- puts "[INFO] Uploaded '#{file}' to the '#{bucket_name}' bucket"
142
- puts "[INFO] Access file at '#{o.url_for(:read)}'"
143
122
  else
144
- puts "[ERROR] Sorry, a bucket named '#{bucket_name}' does not exist"
123
+ upload_file(bucket, file)
145
124
  end
146
125
  end
147
126
 
127
+ def upload_file(bucket, file)
128
+ basename = File.basename(file)
129
+ o = bucket.objects[basename]
130
+
131
+ make_request {
132
+ o.write(:file => file, :content_type => 'text/plain')
133
+
134
+ until bucket_contains_object?(bucket.name, basename) do
135
+ sleep 1
136
+ end
137
+ }
138
+
139
+ puts "[INFO] Uploaded '#{file}' to the '#{bucket.name}' bucket"
140
+ puts "[INFO] Access file at '#{o.url_for(:read)}'"
141
+ end
142
+
148
143
  end
@@ -0,0 +1,34 @@
1
+ module AwsHelper
2
+ def make_request
3
+ done = false
4
+ attempts = 0
5
+ max_attempts = 10
6
+
7
+ until done || (attempts >= max_attempts)
8
+ begin
9
+ yield
10
+ done = true
11
+ puts "[INFO] Done"
12
+ rescue AWS::S3::Errors::NoSuchBucket => error
13
+ if attempts == 0
14
+ puts "[ERROR] #{error}"
15
+ end
16
+ puts "[INFO] Trying again... (#{attempts.to_i + 1}/#{max_attempts})"
17
+ attempts = attempts + 1
18
+ sleep 1
19
+ next
20
+ rescue AWS::S3::Errors::BucketAlreadyExists => error
21
+ puts "[ERROR] #{error}"
22
+ exit 1
23
+ rescue AWS::S3::Errors::AccessDenied => error
24
+ puts "[ERROR] #{error}"
25
+ exit 1
26
+ end
27
+ end
28
+
29
+ if attempts >= max_attempts
30
+ puts "[ERROR] Max number of attempts reached"
31
+ exit 1
32
+ end
33
+ end
34
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bucket_list
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-01 00:00:00.000000000 Z
12
+ date: 2012-09-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-sdk
@@ -69,6 +69,7 @@ extra_rdoc_files: []
69
69
  files:
70
70
  - lib/bucket_list.rb
71
71
  - lib/bucket_list/app.rb
72
+ - lib/bucket_list/aws_helper.rb
72
73
  - config/config.yml
73
74
  - config/aws_config.yml
74
75
  - bin/bucket_list