-
Notifications
You must be signed in to change notification settings - Fork 2
/
list_buckets.rb
101 lines (84 loc) · 2.08 KB
/
list_buckets.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require 'rubygems'
require 'aws/s3'
require 'yaml'
@access_key_id = ENV['AMAZON_ACCESS_KEY_ID']
@secret_access_key = ENV['AMAZON_SECRET_ACCESS_KEY']
@lines = "----------------------------------------------------------"
def check_settings
unless ENV['AMAZON_ACCESS_KEY_ID']
puts "FATAL: AMAZON_ACCESS_KEY_ID not set, quiting now."
puts "you should set AMAZON_ACCESS_KEY_ID ($export AMAZON_ACCESS_KEY_ID=....)"
exit
end
unless ENV['AMAZON_SECRET_ACCESS_KEY']
puts "FATAL: AMAZON_SECRET_ACCESS_KEY not set, quiting now."
puts "you should set AMAZON_SECRET_ACCESS_KEY ($export AMAZON_SECRET_ACCESS_KEY=....)"
exit
end
end
# Function to stablish connection
def stablish_connection
begin
AWS::S3::Base.establish_connection!(
:access_key_id => @access_key_id,
:secret_access_key => @secret_access_key
)
rescue => exception
puts @lines
puts "There was an error: "
puts exception.to_s
exit
end
puts "Good, Connection Stablished."
return "ok"
end
def bucket_objects(bucket_name)
if bucket = find_bucket(bucket_name)
bucket.objects.each do |b|
puts b.key
end
end
end
def list_buckets
puts "Current buckets:"
buckets = AWS::S3::Bucket.list
buckets.each do |t|
puts @lines
puts "Name: #{t.name}"
puts "Date: #{t.creation_date}"
puts "Objects: "
t.objects.each do |b|
puts b.key
end
puts @lines
puts "\n"
end
end
# Function to find or create a bucket
def find_bucket(bucket_name)
if bucket = AWS::S3::Bucket.find(bucket_name)
puts "Bucket #{bucket_name} found."
bucket
else
puts "The bucket #{bucket_name} could not be found"
nil
end
end
# Function to find or create a bucket
def create_bucket(bucket_name)
return if find_bucket(bucket_name)
begin
puts 'Creating the bucket now.'
AWS::S3::Bucket.create(bucket_name)
rescue
puts "The bucket #{bucket_name} could not be created"
return
end
sleep 1
if find_bucket(bucket_name)
puts "Good, bucket #{bucket_name} created."
end
end
check_settings
stablish_connection
list_buckets