This repository has been archived by the owner on Jun 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.py
65 lines (50 loc) · 1.9 KB
/
s3.py
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
# -*- coding: utf-8 -*-
import boto
import time
from boto.s3.connection import S3Connection
from prettytable import PrettyTable as _pretty_table
from fabric.api import *
from fabric.colors import green as _green, yellow as _yellow, red as _red
from global_conf import *
@task(default=True)
def list_buckets():
'''List All Buckets'''
start_time = time.time()
conn = S3Connection(ec2_key, ec2_secret)
print(_green("Listing active buckets..."))
buckets = conn.get_all_buckets()
x = _pretty_table(["Name", "Connection"])
for bucket in buckets:
x.add_row([bucket.name, bucket.connection])
print(_yellow(x))
end_time = time.time()
print(_green("Runtime: %f minutes" % ((end_time - start_time) / 60)))
@task()
def create_bucket(bucket_name):
'''Create a bucket'''
start_time = time.time()
conn = S3Connection(ec2_key, ec2_secret)
print(_green("Creating bucket..."))
try:
bucket = conn.create_bucket(bucket_name=bucket_name)
print _green('Bucket "%s" successfully created' % bucket_name)
except Exception, e:
print(_red('Create bucket error: {}'.format(str(e))))
end_time = time.time()
print(_green("Runtime: %f minutes" % ((end_time - start_time) / 60)))
@task()
def delete_bucket(bucket_name):
'''Delete a bucket'''
start_time = time.time()
conn = S3Connection(ec2_key, ec2_secret)
delete = prompt('Are you sure you want to delete this bucket (Y/N): ')
if delete.upper() == 'Y':
try:
bucket = conn.get_bucket(bucket_name=bucket_name)
print(_green("Deleting bucket..."))
conn.delete_bucket(bucket)
print _yellow('Bucket "%s" successfully deleted' % bucket_name)
except Exception, e:
print(_red('Delete bucket error: {}'.format(str(e))))
end_time = time.time()
print(_green("Runtime: %f minutes" % ((end_time - start_time) / 60)))